-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate.py
More file actions
60 lines (45 loc) · 1.5 KB
/
Copy pathgenerate.py
File metadata and controls
60 lines (45 loc) · 1.5 KB
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
from templates import *
from bs4 import BeautifulSoup
class TableGenerator():
def __init__(self, template, source, elements, output):
self.template = template
self.source = source
self.element = elements
self.output = output
def read_file(self):
file = open(self.source, 'r')
self.content = file.read()
def fetch_records(self):
soup = BeautifulSoup(self.content, 'lxml')
self.records = soup.find_all(elements['parent'])
def parse_records(self):
self.context = {'table': []}
for record in self.records:
row = {}
for key in elements['child']:
try:
row[key] = record.find(key).get_text()
except AttributeError:
print("No value found")
row['key'] = " "
self.context['table'].append(row)
def render(self):
self.rendered = get_template("index.html").render(self.context)
def write(self):
file = open(output, "w")
file.write(self.rendered)
file.close
source = 'departures.xml'
output = 'output/index.html'
template = 'index.html'
parent = 'record'
child = ['flight', 'destination', 'std', 'status']
elements = {'parent': parent, 'child': child }
generator = TableGenerator(template, source, elements, output)
# xml read/parse
generator.read_file()
generator.fetch_records()
generator.parse_records()
# html template parse/write
generator.render()
generator.write()