-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateFolder.py
165 lines (130 loc) · 4.78 KB
/
CreateFolder.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import config
from bs4 import BeautifulSoup
from shutil import make_archive
from distutils.dir_util import copy_tree
path = config.file_path
def main():
working = get_work_dir()
folders = create_folders(working)
if input("Copy files from examples? [y/n] ") == 'y':
copy_files(folders, working)
# There's only text to replace if we're using examples
if input("Replace all placeholder text in files? [y/n] ") == 'y':
replace_html(folders)
else:
populate_folders(folders)
if input("Zip files? [y/n]: ") == 'y':
zip_folders(folders)
def get_work_dir():
# Get the current subfolder you will be working in
loc = input("Folder to create subfolders in: ")
working = path + '/' + loc
if not os.path.exists(working):
os.makedirs(working)
return working
def create_folders(working):
# Create specified number of folders in previously defined
# subfolder if it does not already exist.
created = []
working += '/Practice ' + get_digit_suffix(working)
for i in range(1, int(input("How many assignments? ")) + 1):
current = working + '.' + str(i)
created.append(current + '/')
if not os.path.exists(current):
os.makedirs(current)
return created
def get_digit_suffix(working):
# Search working for digit suffix in current directory
# Allows for more directories with 2-digit suffixes
# to be operated on
suffix = ""
for c in reversed(working):
if c.isdigit():
suffix += c
else:
break
return suffix[::-1]
def get_src_folders(working, assignments):
# Get the example folders inside working directory to get
# copy contents for new folders
src_folders = []
working += '/' + get_digit_suffix(working)
for i in range(1, assignments + 1):
current = working + '.' + str(i)
src_folders.append(current)
return src_folders
def copy_files(folders, working):
# Copy contents from one directory (left) to a new location (right)
sources = get_src_folders(working, len(folders))
for i in range(len(sources)):
copy_tree(sources[i], folders[i])
def replace_html(folders):
# Iterates through all copied files and replace text accordingly
for folder in folders:
# Find all HTML files in directory and read them
for subdir, dirs, files in os.walk(folder):
for filename in files:
file_path = subdir + os.sep + filename
if file_path.endswith(".html"):
html = read_html(file_path)
soup = edit_html_text(html)
overwrite_html(file_path, soup)
def read_html(file_name):
# Return the html from the file passed in
with open(file_name, "r") as f:
return f.read()
def overwrite_html(file_name, soup):
with open(file_name, "w") as f:
f.write(soup.prettify(formatter='html5'))
f.close()
def edit_html_text(html):
# Create a soup and sift through it
soup = BeautifulSoup(html, "html.parser")
for tags in soup.find_all():
for child in tags.descendants:
# This checks if nav tag and will prevent the text from changing if it is
if child.find_parent(config.SKIP_TAGS) is None:
if child.parent.name not in config.IGNORE_TAGS and child.string is not None:
child.string = lorem(get_word_count(child.string))
if soup.find(config.CUSTOM_EDIT):
foot = soup.footer
foot.clear()
foot.insert(0, config.FOOTER_CUSTOM_TEXT)
return soup
def get_word_count(s):
# Return the number of words in a string denoted by spaces
return len(s.split(" "))
def lorem(n):
# Return that same number of words from the LOREM_IPSUM placeholder text
return " ".join(config.LOREM_IPSUM.split(" ")[0:n])
# TODO: Replace current file open using with
def populate_folders(created):
# If a HTML file isn't already in a folder, provide a default one
for i in created:
if not os.path.exists(i + "index.html"):
f = open(i + "index.html", "w")
write_html(f)
# TODO: Rework template HTML to use BS4 rather than strings
def write_html(f):
# Default HTML
f.write("<!DOCTYPE html>\n")
f.write('<html lang="en">')
f.write("\n")
f.write("\n<head>\n")
f.write(" <title> </title>\n")
f.write(' <meta charset="utf-8">')
f.write("\n</head>\n")
f.write("\n")
f.write("<body>\n")
f.write("</body>\n")
f.write("\n")
f.write("</html>\n")
f.close()
def zip_folders(created):
# Zip all created folders
for i in range(0, len(created)):
created[i] = created[i][:-1]
make_archive(created[i], 'zip', root_dir=created[i])
if __name__ == '__main__':
main()