This repository has been archived by the owner on Aug 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
111 lines (87 loc) · 3.28 KB
/
generate.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
""" CS4HS Website Generator
AUTHOR: Jack Morgan
REQUIRES: Python >= 3.4.1
"""
CURRENT_DIRECTORY = '.'
OUTPUT_DIRECTORY = './output/'
TEXT_FOLDER = './text/'
FOLDERS_TO_COPY = ['css', 'files', 'img', 'js']
"""Check and install dependencies"""
import pip
# Update pip if needed and install dependencies
pip.main(['install', 'pip>=7.0.3'])
pip.main(['install', 'jinja2>=2.7.3'])
import os
import os.path
import shutil
import argparse
from jinja2 import Environment, FileSystemLoader
class WebsiteGenerator:
"""Object for generating CS4HS website"""
def __init__(self):
# Load files from this folder and templates folder
self.env = Environment(loader=FileSystemLoader([CURRENT_DIRECTORY, 'templates/']))
def render_html(self, template):
"""Return a rendered template"""
return self.env.get_template(template).render()
def write_html(html, file):
"""Render each file to output folder"""
file_name = os.path.join(OUTPUT_DIRECTORY, file)
try:
with open(file_name, 'w', encoding='utf8') as output_file:
output_file.write(html)
print('Created {}'.format(file))
os.chmod(file_name, 0o644)
except:
print("Cannot write {0}".format(file))
def copy_files():
"""Copy all required files to destination folder"""
for folder in FOLDERS_TO_COPY:
src_folder = os.path.join(CURRENT_DIRECTORY, folder)
dest_folder = os.path.join(OUTPUT_DIRECTORY, folder)
if os.path.exists(dest_folder):
shutil.rmtree(dest_folder)
shutil.copytree(src_folder, dest_folder)
os.chmod(dest_folder, 0o2775)
apply_file_permissions_to_folder(dest_folder)
print("Copied {} folder".format(folder))
def apply_file_permissions_to_folder(folder_name):
for root, folders, files in os.walk(folder_name):
for folder in folders:
folder_path = os.path.join(root, folder)
os.chmod(folder_path, 0o2775)
for file_name in files:
file_path = os.path.join(root, file_name)
os.chmod(file_path, 0o644)
def command_line_args():
"""Setup arg parser, and add required argument handling. Return
namespace generated by parser arguments
"""
argsparser = argparse.ArgumentParser(description='CS4HS Generator Argument')
argsparser.add_argument('--pre-conference', '-p',
dest='pre_conference',
action='store_true',
help='Creates only index page for pre-conference')
return argsparser.parse_args()
def main():
"""Create template engine and process all HTML files
in the top directory"""
cmd_args = command_line_args()
website_generator = WebsiteGenerator()
if cmd_args.pre_conference:
files = ['pre-index.html']
else:
files = os.listdir(TEXT_FOLDER)
files.remove('pre-index.html')
# Render all HTML files in top directory
for file in files:
if file.endswith('.html'):
file_path = os.path.join(TEXT_FOLDER, file)
html = website_generator.render_html(file_path)
if cmd_args.pre_conference:
write_html(html, 'index.html')
else:
write_html(html, file)
copy_files()
if __name__ == "__main__":
main()