-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv2web.py
executable file
·153 lines (132 loc) · 4.92 KB
/
csv2web.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
#!/usr/bin/env python3
#
# Copyright (C) 2014 Elliott Sales de Andrade
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import csv
import os
try:
os.mkdir(os.path.join('web', 'Nominal_Rolls'))
except OSError:
pass
for dirpath, dirnames, filenames in os.walk('Nominal Rolls'):
if dirpath == 'Nominal Rolls':
with open('web/index.html', 'rt') as f:
text = f.readlines()
with open('web/index.html', 'wt') as f:
for line in text:
f.write(line)
if '## Browse' in line:
break
for name in dirnames:
path = os.path.join(dirpath.replace(' ', '_'),
name.replace(' ', '_'))
f.write(' * [' + name + '](' + path + ')\n')
index = os.path.join('web',
dirpath.replace(' ', '_'),
'index.html')
with open(index, 'wt') as f:
f.write('---\n')
f.write('title: ' + dirpath.split('/')[-1] + '\n')
f.write('---\n')
f.write('\n')
has_files = False
for name in sorted(dirnames):
if not has_files:
f.write('<ul class="fa-ul">\n')
has_files = True
path = name.replace(' ', '_')
f.write(' <li><i class="fa fa-fw fa-folder"></i>')
f.write(' <a href="')
f.write(path)
f.write('">')
f.write(name)
f.write('</a></li>\n')
for name in sorted(filenames):
if name[-3:] != 'csv':
continue
if not has_files:
f.write('<ul class="fa-ul">\n')
has_files = True
path = name[:-3].replace(' ', '_') + 'html'
f.write(' <li><i class="fa fa-fw fa-file-text-o"></i>')
f.write(' <a href="')
f.write(path)
f.write('">')
f.write(name[:-4])
f.write('</a></li>\n')
if has_files:
f.write('</ul>\n')
else:
f.write('Nothing available here!\n')
for name in dirnames:
path = os.path.join('web',
dirpath.replace(' ', '_'),
name.replace(' ', '_'))
try:
os.mkdir(path)
except OSError:
# already exists
pass
for name in filenames:
if name[-3:] == 'csv':
orig_path = os.path.join(dirpath, name)
ncols = 0
first = None
lines = []
with open(orig_path, 'rt') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if not first and not row[1]:
first = row[5:]
continue
ncols = max(ncols, len(row))
clean_row = [x.replace('|', '|') for x in row]
lines.append(row)
new_path = os.path.join('web',
dirpath.replace(' ', '_'),
name.replace(' ', '_'))
new_path = new_path[:-3]
new_path += 'md'
with open(new_path, 'wt') as f:
f.write('---\n')
f.write('title: ' + name[:-4] + '\n')
f.write('wide: wide\n')
f.write('---\n')
f.write('\n')
f.write('# ' + name[:-4] + '\n')
f.write('\n')
if first:
f.write('## Cover Page Information\n')
f.write('{% raw %}\n')
f.write('<br>\n'.join(first))
f.write('\n')
f.write('{% endraw %}\n')
if not ncols:
continue
f.write('## Tables\n')
f.write('{% raw %}\n')
f.write('| Page | Bounds ' + ('| ' * (ncols - 5)) + '|\n')
f.write('| --- ' * (ncols - 3) + '|\n')
for row in lines:
f.write('| ')
# Page and bounds
f.write(row[0])
f.write(' | ')
f.write('<br>'.join(row[1:5]))
f.write(' | ')
# Cells
f.write(' | '.join(row[5:]))
f.write(' |\n')
f.write('{% endraw %}\n')