-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcsv.py
More file actions
468 lines (366 loc) · 13.4 KB
/
webcsv.py
File metadata and controls
468 lines (366 loc) · 13.4 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
v = '0.0.6'
"""
Copyright (C) 2024 Ray (github.com/ryt)
Latest version of the project: https://github.com/ryt/webcsv
"""
import os
import re
import csv
import html
import config
import itertools
import mimetypes
from flask import Flask
from flask import request
from flask import render_template
from flask import send_file, abort
from urllib.parse import quote
app = Flask(__name__)
# -- start: parse config parameters from config.py and set values
# default config values
limitpath = ''
app_path = '/webcsv'
parse_html = False
parse_markdown = False
parse_rst = False
# read & modify config values
if 'limitpath' in config.config:
limitpath = config.config['limitpath'].rstrip('/') + '/'
if 'app_path' in config.config:
app_path = config.config['app_path']
if 'parse_html' in config.config:
parse_html = config.config['parse_html']
if 'parse_markdown' in config.config:
parse_markdown = config.config['parse_markdown']
if 'parse_rst' in config.config:
parse_rst = config.config['parse_rst']
# -- end: parse config parameters
# markdown options
if parse_markdown == True:
from marko.ext.gfm import gfm
# rst options
if parse_rst == True:
from docutils import core
def get_query(param):
"""Get query string param (if exists & has value) or empty string"""
try:
return request.args.get(param) if request.args.get(param) else ''
except:
return ''
def remove_from_start(sub, string):
"""Remove sub from beginning of string if string starts with sub"""
if string.startswith(sub):
return string[len(sub):].lstrip()
else:
return string
def remove_limitpath(path):
"""Remove limitpath from beginning of path if limitpath has value"""
global limitpath
return remove_from_start(limitpath, path) if limitpath else path
def add_limitpath(path):
"""Add limitpath to beginning of path if limitpath has value"""
global limitpath
return f'{limitpath}{path}' if limitpath else path
def sanitize_path(path):
"""Sanitize path for urls: 1. apply limitpath mods, 2. escape &'s and spaces"""
return quote(remove_limitpath(path), safe='/')
sp = sanitize_path
def parse_filter(qfilter):
"""Parse a filter (query) string and convert it into dictionary with keys, values, & attributes"""
filter_dicts = []
filter_instances = qfilter.split(',')
for f in filter_instances:
filter_parts = f.split(':')
filter_key = filter_parts[0]
filter_val = filter_parts[1]
filter_col_num = int(''.join(filter(str.isdigit, filter_key)))
filter_dicts.append({
'key' : filter_key,
'col_num' : filter_col_num,
'val' : filter_val
})
return filter_dicts
def filter_compare(csv_value, search_value):
"""Compares csv_value & search_value and determines if the filters match or not"""
# quoted strings == exact match
if (search_value.startswith('"') and search_value.endswith('"')) or (search_value.startswith("'") and search_value.endswith("'")):
if search_value.strip('\'"') == csv_value:
return True
else:
return False
# non-quoted strings = search
elif search_value in csv_value:
return True
return False
def alphatocol(alpha):
"""alphabets to columns"""
alphas = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
colmap = {}
for i, a in enumerate(alphas):
colmap[a] = i # f'c{i+1}'
return colmap[alpha]
def decimal_sum(vals):
"""sums list of integer and float values while maintaining existing decimal places (or none)"""
s = [str(v) for v in vals]
d = max((len(x.split('.')[1]) for x in s if '.' in x), default=0)
scale = 10 ** d
total = 0
for x in s:
if '.' in x:
w, f = x.split('.')
total += int(w) * scale + int(f.ljust(d, '0'))
else:
total += int(x) * scale
if d == 0:
return f"{total:,}"
w, f = divmod(total, scale)
f = str(f).rjust(d, '0').rstrip('0')
whole = f"{w:,}" # add commas to thousands place (english)
return whole if f == "" else f"{whole}.{f}"
def runformulas(cell, rawcsvcopy):
"""basic formula operations"""
# --- 1. sum from colrow:colrow ---
if '=sum(' in cell:
# e.g. =sum(a1:a5)
match = re.search(r'=sum\(([a-zA-Z]+)([0-9]+):([a-zA-Z]+)([0-9]+)\)', cell)
if match:
start_col = alphatocol(match.group(1))
start_row = int(match.group(2))
end_col = alphatocol(match.group(3))
end_row = int(match.group(4))
get_rows = []
for i, row in enumerate(rawcsvcopy):
if start_row <= i <= end_row:
try:
cell_val = float(row[start_col].strip('$')) # strips $, converts to float
get_rows.append(cell_val)
except Exception:
pass
if i > end_row:
break
sum_rows = decimal_sum(get_rows) # sums & maintains existing decimal places
cell = cell.replace(match.group(0), str(sum_rows))
return str(cell)
def html_return_error(text):
return f'<div class="error">{text}</div>'
def html_render_csv(path):
render = ''
path_mod = remove_limitpath(path)
try:
with open(path, 'r') as file:
getfilter = get_query('filter')
getsort = get_query('sort')
getrun = get_query('run')
html_table = ''
content = file.read()
if getfilter:
filter_insts = parse_filter(getfilter)
filter_ihtml = [f"<b>{f['key']}</b> = <b>{f['val']}</b>" for f in filter_insts]
html_table = ''.join((
f'<div class="top-filter hide-on-hide">Applying filter: {", ".join(filter_ihtml)}.',
f' Filtered rows: ##__filtered_rows__##.</div>'
))
html_table += '<table class="csv-table">\n'
# added {skiinitialspace=True} to fix issue with commas inside quoted cells
csv_reader = csv.reader(content.splitlines(), skipinitialspace=True) # csv reader object from the csv data
csv_reader, rawcsvcopy = itertools.tee(csv_reader) # duplicate into multiple iterators (for multiple consumption)
rawcsvcopy = list(rawcsvcopy) # permanent reusable copy of csv_reader
headers = next(csv_reader)
html_table += '<tr>'
for header in headers:
header = html.escape(header)
html_table += f'<th>{header}</th>'
html_table += '</tr>\n'
filtered_rows = 0
if getsort:
if getsort == 'za':
csv_reader = reversed(list(csv_reader))
for row in csv_reader:
display_row = True
if getfilter:
display_row = False
fi = filter_insts
table_row = '<tr>'
if len(fi) > 1: # multiple filters (AND search)
found_count = 0
for fx in fi:
if 0 <= fx['col_num']-1 < len(row) and filter_compare(row[fx['col_num']-1], fx['val']):
found_count += 1 # add 1 for each found filter
if found_count == len(fi):
display_row = True
else: # single filter
if 0 <= fi[0]['col_num']-1 < len(row) and filter_compare(row[fi[0]['col_num']-1], fi[0]['val']):
display_row = True
for cell in row:
if getrun == 'true':
cell = runformulas(cell, rawcsvcopy)
cell = html.escape(cell)
table_row += f'<td>{cell}</td>'
table_row += '</tr>\n'
else:
table_row = '<tr>'
for cell in row:
if getrun == 'true':
cell = runformulas(cell, rawcsvcopy)
cell = html.escape(cell)
table_row += f'<td>{cell}</td>'
table_row += '</tr>\n'
if display_row:
filtered_rows += 1
html_table += table_row
html_table += '</table>'
render = html_table.replace('##__filtered_rows__##', str(filtered_rows))
except FileNotFoundError:
render = html_return_error(f"The file '{path_mod}' does not exist.")
except Exception as e:
print(e)
render = html_return_error(f"The file '{path_mod}' could not be parsed.")
return render
def plain_render_file(path):
render = ''
path_mod = remove_limitpath(path)
try:
with open(path, 'r') as file:
try:
render = file.read()
except:
render = f"The file '{path_mod}' is not in text format."
except FileNotFoundError:
render = f"The file '{path_mod}' does not exist."
except IOError:
render = f"Error reading the file '{path_mod}'."
return render
def noncsv_render_file(path, ftype):
render = ''
path_mod = remove_limitpath(path)
try:
with open(path, 'r') as file:
try:
if ftype == 'markdown':
render = f'<article class="markdown-body">{gfm(file.read())}</article>'
elif ftype == 'rst':
render = f'<article class="markdown-body">{core.publish_parts(source=file.read(), writer_name="html")["html_body"]}</article>'
elif ftype == 'html':
render = file.read()
# remove tags: doctype, html, head, body
render = re.sub(
r'<!DOCTYPE html>\s*|<html>\s*|</html>\s*|<head>\s*|</head>\s*|<body>\s*|</body>',
'',
render,
flags=re.IGNORECASE,
)
else:
render = f"The file '{path_mod}' is not in supported format."
except:
render = f"The file '{path_mod}' is not in a supported format."
except FileNotFoundError:
render = f"The file '{path_mod}' does not exist."
except IOError:
render = f"Error reading the file '{path_mod}'."
return render
@app.route(app_path, methods=['GET'])
def index(subpath=None):
# if limitpath is set in config, the directory listing view for the client/browser will be limited to that path as the absolute parent
# if app_path is set in config, that path will be used to route index page of the app
global limitpath, app_path
# limitpath = '/usr/local/share/' # for testing
getf = get_query('f')
getshow = get_query('show')
getsort = get_query('sort')
getfilter = get_query('filter')
getdark = get_query('dark')
getrun = get_query('run')
getf_html = remove_limitpath(getf) # limitpath mods for client/browser side view
getf = add_limitpath(getf) # limitpath mods for internal processing
view = {
'app_path' : app_path,
'getsort' : getsort,
'getfilter' : getfilter,
'getdark' : getdark,
'getrun' : getrun,
'dirlist' : False,
}
listfs = []
if os.path.isdir(getf):
view['dirlist'] = True
files = sorted(os.listdir(getf))
parpt = getf.rstrip('/')
if files:
for f in files:
if os.path.isdir(f'{parpt}/{f}'):
listfs.append({
'name' : f'{f}/',
'path' : sp(f'{parpt}/{f}/')
})
else:
listfs.append({
'name' : f,
'path' : sp(f'{parpt}/{f}')
})
else:
# additional non-csv rendering options
# plain
if getshow == 'plain':
view['noncsv'] = True
view['show_plain'] = plain_render_file(getf)
# load
elif getshow == 'load':
if not os.path.isfile(getf):
abort(404)
mime_type, _ = mimetypes.guess_type(getf)
return send_file(getf, mimetype=mime_type or 'application/octet-stream')
# raw
elif getshow == 'raw':
return plain_render_file(getf), 200, { 'Content-Type': 'text/plain' }
else:
# markdown
if parse_markdown == True and getf.endswith('.md'):
view['noncsv'] = True
view['noncsv_markdown'] = noncsv_render_file(getf, 'markdown')
with open('assets/github-markdown.css', 'r') as github_markdown:
view['markdown_css'] = github_markdown.read()
if getdark != 'false':
with open('assets/github-markdown-dark.css', 'r') as github_markdown_dark:
view['markdown_css'] = ' '.join((github_markdown_dark.read(), view['markdown_css']))
# rst
elif parse_rst == True and getf.endswith('.rst'):
view['noncsv'] = True
view['noncsv_rst'] = noncsv_render_file(getf, 'rst')
with open('assets/github-markdown.css', 'r') as github_markdown:
view['rst_css'] = github_markdown.read()
if getdark != 'false':
with open('assets/github-markdown-dark.css', 'r') as github_markdown_dark:
view['rst_css'] = ' '.join((github_markdown_dark.read(), view['rst_css']))
# html
elif parse_html == True and (getf.endswith('.htm') or getf.endswith('.html')):
view['noncsv'] = True
view['noncsv_html'] = noncsv_render_file(getf, 'html')
else:
view['noncsv'] = True
# csv
if getf.endswith('.csv') and getshow != 'plain':
view['csvshow'] = html_render_csv(getf)
view['noncsv'] = False
address = []
addrbuild = ''
if getf_html:
for path in getf_html.strip('/').split('/'):
addrbuild += f'/{path}'
address.append({
'name' : f'{path}',
'path' : sp(f'{addrbuild}'),
'separator' : '/'
})
view['listfs'] = listfs
view['address'] = address
view['getf_html'] = getf_html
view['getf_html_sp'] = sp(getf_html)
view['getrun_query'] = f'&run={getrun}' if getrun else ''
view['getshow_query'] = f'&show={getshow}' if getshow else ''
view['getsort_query'] = f'&sort={getsort}' if getsort else ''
view['getfilter_query'] = f'&filter={getfilter}' if getfilter else ''
view['show_header'] = False if get_query('hide') == 'true' else True
return render_template('template.html', view=view)
if __name__ == '__main__':
app.run(debug=True)