Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active April 3, 2022 16:26
Show Gist options
  • 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()
@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