-
-
Save aahnik/2c18af0ee937bb2947873774f069adc4 to your computer and use it in GitHub Desktop.
# 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, 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.
❤️
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?
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']}
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!
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
Sorry mate, I'd just woken up when I saw this and I was too excited to try it!
Please ignore my stupidity 😆