Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active April 3, 2022 16:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aahnik/2c18af0ee937bb2947873774f069adc4 to your computer and use it in GitHub Desktop.
Save aahnik/2c18af0ee937bb2947873774f069adc4 to your computer and use it in GitHub Desktop.
organize files of loconotion output https://github.com/leoncvlt/loconotion/issues/36
# MIT License
# Aahnik 2021
# a script to organize the files in loconotion output
# also updates html and css files
# unix (linux / mac) style file paths are used in the program,
# will fail inevitably if run on windows
# ditch windows
import logging
import os
structure = {'assets/images': ['png', 'jpg', 'jpeg','bmp','gif','ico'],
'assets/fonts': ['woff','ttf'],
'assets/css': ['css'],
'assets/js': ['js']}
# !! changing this structure, may break other stuff.
logging.info(structure)
mapping = {}
for folder, extensions in structure.items():
for ext in extensions:
mapping[ext] = folder
logging.info(mapping)
all_files = [file for file in os.listdir() if os.path.isfile(file)]
logging.info(all_files)
old_to_new = {}
def rename():
for file in all_files:
ext = file.split('.')[-1]
new_parent_dir = mapping.get(ext)
if new_parent_dir:
new_file = os.path.join(new_parent_dir, file)
if not os.path.isdir(new_parent_dir):
os.makedirs(new_parent_dir)
os.rename(file, new_file)
old_to_new[file] = new_file
logging.info('%s renamed to %s', file, new_file)
def update_code(file_name, old_to_new):
with open(file_name, 'r') as file:
content = file.read()
for old, new in old_to_new.items():
if file_name.endswith('.css'):
new = new.replace('assets', '..')
# relative position of files related to css files
content = content.replace(old, new)
with open(file_name, 'w') as file:
file.write(content)
def main():
rename()
print(old_to_new)
for file in os.listdir():
if file.endswith('.html'):
logging.info(file)
update_code(file, old_to_new)
for file in os.listdir('assets/css'):
if file.endswith('.css'):
logging.info(file)
update_code(f'assets/css/{file}', old_to_new)
if __name__ == '__main__':
print('loconotion organizer')
input(
'This thing will run in your current directory. Are you sure you are in a correct directory ? \n Press [ENTER] to confirm or Ctrl + C to quit')
main()
@jamesdeluk
Copy link

Got an error and it broke my site :(

Traceback (most recent call last):
  File "C:\Users\user\Downloads\loconotion-master\dist\website\locorg.py", line 82, in <module>
    main()
  File "C:\Users\user\Downloads\loconotion-master\dist\website\locorg.py", line 71, in main
    update_code(file, old_to_new)
  File "C:\Users\user\Downloads\loconotion-master\dist\website\locorg.py", line 53, in update_code
    content = file.read()
  File "C:\Program Files\Python39\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 4421: character maps to <undefined>

@aahnik
Copy link
Author

aahnik commented Apr 18, 2021

Hi, @jamesdeluk, have you not read the warning on top? read it. Go to the top of the page, and please read it.

image

windows is not supported in my script

@jamesdeluk
Copy link

Hi, @jamesdeluk, have you not read the warning on top? read it. Go to the top of the page, and please read it.

image

windows is not supported in my script

Sorry mate, I'd just woken up when I saw this and I was too excited to try it!

Please ignore my stupidity 😆

@aahnik
Copy link
Author

aahnik commented Apr 18, 2021

@jamesdeluk, will add windows support in future, but currently loconotion is not being actively developed.

it would be great if the author of loconotion integrates my script with loconotion in future.

❤️

@jamesdeluk
Copy link

Agreed!

Ran it on Linux and it works ;)

I found it doesn't currently work for:
Images: .bmp .gif .ico
Fonts: .ttf

Is this on purpose?

@aahnik
Copy link
Author

aahnik commented Apr 18, 2021

Is this on purpose?
nope. actually I never encountered those files.

you need to edit

structure = {'assets/images': ['png', 'jpg', 'jpeg'],
             'assets/fonts': ['woff'],
             'assets/css': ['css'],
             'assets/js': ['js']}

this portion.

and I updated the script.

structure = {'assets/images': ['png', 'jpg', 'jpeg','bmp','gif','ico'],
             'assets/fonts': ['woff','ttf'],
             'assets/css': ['css'],
             'assets/js': ['js']}

@jamesdeluk
Copy link

I read your # !! changing this structure, may break other stuff. comment this time so didn't try that myself ;)

Fully working, including Ubuntu WSL on Windows. Sadly I've just found Loconotion itself doesn't work fully, hence having to raise an issue on their Git. Hope they reply soon!

@aahnik
Copy link
Author

aahnik commented Apr 18, 2021

I read your # !! changing this structure, may break other stuff. comment this time so didn't try that myself ;)

actually that's true, if you change the folder names.

actually, I made this script in a hurry, so things are not that robust.

Sadly I've just found Loconotion itself doesn't work fully,

loconotion needs a lot of improvements.

but the author is no more interacting, I may submit more PRs if he accepts my first one. or at least responds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment