-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstruct.py
88 lines (64 loc) · 2.2 KB
/
struct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# MIT License
# Aahnik 2021
# a script to organize the files in loconotion output
# also updates html and css files
import logging
import os
import sys
import shutil
structure = {'assets/images': ['png', 'jpg', 'jpeg','bmp','gif','ico', 'svg'],
'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 = new_parent_dir + '/' + file # windows mod
new_file = os.path.join(new_parent_dir, file) # base
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', encoding="utf-8") 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', encoding="utf-8") as file:
file.write(content)
def main():
# Get directory name
mydir= './assets'
try:
shutil.rmtree(mydir)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
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')
main()