-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagenetor.py
71 lines (60 loc) · 2.76 KB
/
pagenetor.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
# /usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import os
import glob
import json
from jinja2 import Environment, FileSystemLoader
class PageGenerator():
def __init__(self):
self.test_vars = self.load_configuration()
self.project_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'projects',
self.test_vars.get('project_dir'))
self.data_folder = os.path.join(self.project_root, 'input_data')
self.template_folder = os.path.join(self.project_root, 'templates')
self.output_folder = os.path.join(self.project_root, 'output')
self.template_file_name = self.test_vars.get('template_file_name')
self.data_expression = self.test_vars.get('data_expression')
def load_configuration(self):
""" loads configuration variables from .properties file """
config = ConfigParser.ConfigParser()
config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.properties')
config.read(config_file)
return dict(config.defaults())
def run(self):
""" runs page object generation """
if not os.path.exists(self.output_folder):
os.makedirs(self.output_folder)
for entry in glob.glob(os.path.join(self.data_folder, self.data_expression)):
f = open(entry)
text = json.loads(f.read())
f.close()
self.create_page_objects(text)
def create_page_objects(self, data):
""" initiates page object generation for each entry in json array """
for page in data['pages']:
self.create_page(page)
def create_page(self, data):
""" generates one page object file """
env = Environment(loader=FileSystemLoader(self.template_folder), trim_blocks=True, lstrip_blocks=True)
template = env.get_template(self.template_file_name)
template_vars = {'class_name': self.get_class_name(data['name']), 'page': data}
output = template.render(template_vars)
formatted_output = output.encode('utf8').strip()
file_name = data['name'] + self.get_output_file_type()
result_html = open(os.path.join(self.output_folder, file_name), 'w')
result_html.write(formatted_output)
result_html.close()
def get_output_file_type(self):
""" returns file type of output page object files """
file_name = '.' + self.template_file_name.split('.')[-2]
return file_name
def get_class_name(self, name):
""" converts file name to class name (camel-case) """
name_list = name.split('_')
file_name = ''
for item in name_list:
file_name += item.capitalize()
return file_name
generator = PageGenerator()
generator.run()