file2html.html


001| '''File-to-HTML Functions.
001| 
001| commands:
001|     py              - python script to HTML page
001|     bin             - Binary File Dump HTML page
001|     tab             - TAB file to HTML page
001|     tab2            - TAB file to HTML page (rotated)
001|     csv             - CSV file to HTML page
001|     csv2            - CSV file to HTML page (rotated)
001| 
001|     utf8            - Copy ASCII file to UTF-8 file
001| '''

002| ####################################################################################################
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from logger import logger, info, debug, trace
007| from files import datafile, textfile, linefile, tabfile, csvfile
008| from goodies import KWARG
009| from webpage import EOL, WebPage
010| from webpage import TabPageHtml, TabPageListHtml, BinDumpPage
011| from py2html import python_to_html
012| ####################################################################################################
013| BasePath = r'C:\CJS\prj\Python\app'
014| DataPath = path.join(BasePath, 'dat')
015| ImgsPath = path.join(BasePath, 'img')
016| ProjPath = r'C:\CJS\prj\Python\ws'
017| HtmlPath = r'C:\CJS\www\root\pub\python'
018| TempPath = r'C:\CJS\www\root\tmp'
019| DocsPath = r'C:\Users\chris\Documents'
020| Log = logger('FILE2HTML')
021| 
022| CMND = 'utf8'
023| ARGS = {
024|     #'nbrs': 'd', 'nw': '9',
025|     #'data': 'x', 'dw': '4', 'bd':'le',
026|     #'cols': '24', 'size': '2',
027|     'cp':'cp1252',
028|     'iname': 'history.html',
029|     'ipath': DocsPath,
030|     'oname': 'history_utf8.html',
031|     'opath': DocsPath,
032|     'fname': 'file2html.out',
033|     'fpath': BasePath,
034| }
035| 
036| 
037| ##================================================================================================##
038| def do_asc_2_utf8 (**kwargs):
039|     ipath = KWARG('ipath', BasePath, str, kwargs)
040|     iname = KWARG('iname', None, str, kwargs)
041|     opath = KWARG('opath', TempPath, str, kwargs)
042|     oname = KWARG('oname', None, str, kwargs)
043|     codepage = KWARG('cp', 'cp1250', str, kwargs)
044|     Log.info('ASC2UTF8:: %s' % str(list(kwargs.keys())))
045|     Log.info('ipath="%s"' % ipath)
046|     Log.info('iname="%s"' % iname)
047|     Log.info('opath="%s"' % opath)
048|     Log.info('oname="%s"' % oname)
049|     Log.info('codepage="%s"' % codepage)
050|     Log.info()
051|     ifilename = path.join(ipath, iname)
052|     ofilename = path.join(opath, oname)
053|     Log.info('in-file: %s' % ifilename)
054|     Log.info('out-file: %s' % ofilename)
055|     Log.info()
056|     # Load text file...
057|     fi = textfile(ifilename)
058|     fi.load(encoding=codepage)
059|     # Write UTF-8 file...
060|     fo = textfile(ofilename)
061|     fo.data = fi.data
062|     fo.save()
063|     #
064|     Log.info()
065|     return 'Done'
066| 
067| ##================================================================================================##
068| def do_sort_file (**kwargs):
069|     ipath = KWARG('ipath', BasePath, str, kwargs)
070|     iname = KWARG('iname', None, str, kwargs)
071|     opath = KWARG('opath', TempPath, str, kwargs)
072|     oname = KWARG('oname', None, str, kwargs)
073|     Log.info('SORT:: %s' % str(list(kwargs.keys())))
074|     Log.info('ipath="%s"' % ipath)
075|     Log.info('iname="%s"' % iname)
076|     Log.info('opath="%s"' % opath)
077|     Log.info('oname="%s"' % oname)
078|     Log.info()
079|     ifilename = path.join(ipath, iname)
080|     ofilename = path.join(opath, oname)
081|     Log.info('in-file: %s' % ifilename)
082|     Log.info('out-file: %s' % ofilename)
083|     Log.info()
084|     # Load text file...
085|     fi = linefile(ifilename)
086|     fi.load()
087|     # Write UTF-8 file...
088|     fo = linefile(ofilename)
089|     fo.data = sorted(fi.data)
090|     fo.save()
091|     #
092|     Log.info()
093|     return 'Done'
094| 
095| 
096| ##================================================================================================##
097| def tab_to_html (tab_name, html_name, maxrows=-1, use_csv=False):
098|     '''Create an HTML output file from a TAB-delimited input file.'''
099|     Log.info('Tab2Html')
100|     Log.info('use-csv: %d' % use_csv)
101|     Log.info()
102|     # Get data...
103|     fobj = csvfile(tab_name,maxrows=maxrows) if use_csv else tabfile(tab_name,maxrows=maxrows)
104|     fobj.load()
105|     Log.info('read: %s' % tab_name)
106|     Log.info('hdrs: %d' % len(fobj.hdrs))
107|     Log.info('rows: %d' % len(fobj.data))
108|     Log.info()
109|     # HTML Page...
110|     pg = TabPageHtml(html_name, fobj)
111|     pg.save(html_name, log=Log)
112|     Log.info()
113| ##================================================================================================##
114| def tab_to_html_list (tab_name, html_name, maxrows=-1, use_csv=False):
115|     '''Create an HTML output file from a TAB-delimited input file. (rotated)'''
116|     Log.info('Tab2HtmlList')
117|     Log.info('use-csv: %d' % use_csv)
118|     Log.info()
119|     # Get data...
120|     fobj = csvfile(tab_name,maxrows=maxrows) if use_csv else tabfile(tab_name,maxrows=maxrows)
121|     fobj.load()
122|     Log.info('read: %s' % tab_name)
123|     Log.info('hdrs: %d' % len(fobj.hdrs))
124|     Log.info('rows: %d' % len(fobj.data))
125|     Log.info()
126|     # HTML Page...
127|     pg = TabPageListHtml(html_name, fobj, maxrows)
128|     pg.save(html_name, log=Log)
129|     Log.info()
130| ##================================================================================================##
131| def do_tab_2_html (*args):
132|     ipath = KWARG('ipath', BasePath, str, kwargs)
133|     iname = KWARG('iname', 'tab2html.tab', str, kwargs)
134|     opath = KWARG('opath', TempPath, kwargs)
135|     oname = KWARG('oname', 'tab2html.html', str, kwargs)
136|     maxrows = KWARG('rows', '-1', int, kwargs)
137|     Log.info('TAB2HTML:: %s' % str(list(kwargs.keys())))
138|     Log.info('ipath="%s"' % ipath)
139|     Log.info('iname="%s"' % iname)
140|     Log.info('opath="%s"' % opath)
141|     Log.info('oname="%s"' % oname)
142|     Log.info('max-rows: %d' % maxrows)
143|     Log.info()
144|     ifilename = path.join(ipath, iname)
145|     ofilename = path.join(opath, oname)
146|     Log.info('data-file: %s' % ifilename)
147|     Log.info('html-file: %s' % ofilename)
148|     Log.info()
149|     # TAB to HTML...
150|     tab_to_html(iname, oname, maxrows=maxrows, use_csv=False)
151|     Log.info()
152|     return 'Done'
153| ##================================================================================================##
154| def do_tab_2_html_list (*args):
155|     '''Rotate table; only show a few rows (as cols).'''
156|     ipath = KWARG('ipath', BasePath, str, kwargs)
157|     iname = KWARG('iname', 'tab2html.tab', str, kwargs)
158|     opath = KWARG('opath', TempPath, str, kwargs)
159|     oname = KWARG('oname', 'tab2html.html', str, kwargs)
160|     maxrows = KWARG('rows', '-1', int, kwargs)
161|     Log.info('TAB2HTML-LIST:: %s' % str(list(kwargs.keys())))
162|     Log.info('ipath="%s"' % ipath)
163|     Log.info('iname="%s"' % iname)
164|     Log.info('opath="%s"' % opath)
165|     Log.info('oname="%s"' % oname)
166|     Log.info('max-rows: %d' % maxrows)
167|     Log.info()
168|     ifilename = path.join(ipath, iname)
169|     ofilename = path.join(opath, oname)
170|     Log.info('data-file: %s' % ifilename)
171|     Log.info('html-file: %s' % ofilename)
172|     Log.info()
173|     # TAB to HTML (rotated)...
174|     tab_to_html_list(iname, oname, maxrows=maxrows, use_csv=False)
175|     Log.info()
176|     return 'Done'
177| ##================================================================================================##
178| def do_csv_2_html (*args):
179|     ipath = KWARG('ipath', BasePath, str, kwargs)
180|     iname = KWARG('iname', 'tab2html.csv', str, kwargs)
181|     opath = KWARG('opath', TempPath, str, kwargs)
182|     oname = KWARG('oname', 'tab2html.html', str, kwargs)
183|     maxrows = KWARG('rows', '-1', int, kwargs)
184|     Log.info('CSV2HTML:: %s' % str(list(kwargs.keys())))
185|     Log.info('ipath="%s"' % ipath)
186|     Log.info('iname="%s"' % iname)
187|     Log.info('opath="%s"' % opath)
188|     Log.info('oname="%s"' % oname)
189|     Log.info()
190|     ifilename = path.join(ipath, iname)
191|     ofilename = path.join(opath, oname)
192|     Log.info('data-file: %s' % ifilename)
193|     Log.info('html-file: %s' % ofilename)
194|     Log.info()
195|     # CSV to HTML...
196|     tab_to_html(iname, oname, maxrows=maxrows, use_csv=True)
197|     Log.info()
198|     return 'Done'
199| ##================================================================================================##
200| def do_csv_2_html_list (*args):
201|     '''Rotate table; only show a few rows (as cols).'''
202|     ipath = KWARG('ipath', BasePath, str, kwargs)
203|     iname = KWARG('iname', 'tab2html.csv', str, kwargs)
204|     opath = KWARG('opath', TempPath, str, kwargs)
205|     oname = KWARG('oname', 'tab2html.html', str, kwargs)
206|     maxrows = KWARG('rows', '-1', int, kwargs)
207|     Log.info('CSV2HTML-LIST:: %s' % str(list(kwargs.keys())))
208|     Log.info('ipath="%s"' % ipath)
209|     Log.info('iname="%s"' % iname)
210|     Log.info('opath="%s"' % opath)
211|     Log.info('oname="%s"' % oname)
212|     Log.info()
213|     ifilename = path.join(ipath, iname)
214|     ofilename = path.join(opath, oname)
215|     Log.info('data-file: %s' % ifilename)
216|     Log.info('html-file: %s' % ofilename)
217|     Log.info()
218|     # CSV to HTML (rotated)...
219|     tab_to_html_list(iname, oname, maxrows=maxrows, use_csv=True)
220|     Log.info()
221|     return 'Done'
222| 
223| ##================================================================================================##
224| def do_python_2_html (**kwargs):
225|     ipath = KWARG('ipath', BasePath, str, kwargs)
226|     iname = KWARG('iname', 'py2html.py', str, kwargs)
227|     opath = KWARG('opath', TempPath, str, kwargs)
228|     oname = KWARG('oname', 'py2html.html', str, kwargs)
229|     Log.info('PY2HTML:: %s' % str(list(kwargs.keys())))
230|     Log.info('ipath="%s"' % ipath)
231|     Log.info('iname="%s"' % iname)
232|     Log.info('opath="%s"' % opath)
233|     Log.info('oname="%s"' % oname)
234|     Log.info()
235|     ifilename = path.join(ipath, iname)
236|     ofilename = path.join(opath, oname)
237|     Log.info('python-file: %s' % ifilename)
238|     Log.info('html-file: %s' % ofilename)
239|     Log.info()
240|     # Python to HTML...
241|     python_to_html(ifilename, ofilename, log=Log)
242|     Log.info()
243|     return 'Done'
244| 
245| ##================================================================================================##
246| def do_bin_2_html (**kwargs):
247|     ipath = KWARG('ipath', BasePath, str, kwargs)
248|     iname = KWARG('iname', '401k.png', str, kwargs)
249|     opath = KWARG('opath', TempPath, str, kwargs)
250|     oname = KWARG('oname', 'bin2html.html', str, kwargs)
251|     Log.info('BIN2HTML:: %s' % str(list(kwargs.keys())))
252|     Log.info('ipath="%s"' % ipath)
253|     Log.info('iname="%s"' % iname)
254|     Log.info('opath="%s"' % opath)
255|     Log.info('oname="%s"' % oname)
256|     Log.info()
257|     ifilename = path.join(ipath, iname)
258|     ofilename = path.join(opath, oname)
259|     Log.info('data-file: %s' % infilename)
260|     Log.info('html-file: %s' % outfilename)
261|     Log.info()
262|     # Input File...
263|     fo = datafile(infilename)
264|     fo.load()
265|     # HTML Page...
266|     pg = BinDumpPage(infilename, bs, **kwargs)
267|     pg.save(outfilename, log=Log)
268|     Log.info()
269|     return 'Done'
270| 
271| ##================================================================================================##
272| def do_test (**kwargs):
273|     fpath = KWARG('fpath', BasePath, str, kwargs)
274|     fname = KWARG('fname', 'test.out', str, kwargs)
275|     Log.info('TEST:: %s' % str(list(kwargs.keys())))
276|     Log.info('fpath="%s"' % fpath)
277|     Log.info('fname="%s"' % fname)
278|     Log.info()
279|     #
280|     #
281|     Log.info()
282|     return 'Done'
283| ####################################################################################################
284| def dispatch (cmd, **kwargs):
285|     Log.info('command: %s' % cmd)
286|     for kw in kwargs:
287|         Log.info('argument[%s]: %s' % (kw,kwargs[kw]))
288|     Log.info()
289|     if cmd == 'py': return do_python_2_html(**kwargs)
290|     if cmd == 'bin': return do_bin_2_html(**kwargs)
291|     if cmd == 'tab': return do_tab_2_html(**kwargs)
292|     if cmd == 'csv': return do_csv_2_html(**kwargs)
293|     if cmd == 'tab2': return do_tab_2_html_list(**kwargs)
294|     if cmd == 'csv2': return do_csv_2_html_list(**kwargs)
295|     if cmd == 'utf8': return do_asc_2_utf8(**kwargs)
296|     if cmd == 'sort': return do_sort_file(**kwargs)
297|     if cmd == 'test': return do_test(**kwargs)
298|     return 'Nothing to do!'
299| ####################################################################################################
300| if __name__ == '__main__':
301|     print('autorun: %s' % argv[0])
302|     Log.start(path.join(BasePath,'file2html.log'))
303|     Log.level(info())
304|     Log.info(argv[0])
305|     Log.info()
306|     cmd = argv[1] if 1 < len(argv) else CMND
307|     kwargs = ARGS
308|     for a in argv[2:]:
309|         nv = a.split('=', 1)
310|         kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
311|         Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
312|     try:
313|         print('CMD: %s' % cmd)
314|         print()
315|         obj = dispatch(cmd, **kwargs)
316|         print()
317|         print(obj)
318|         Log.info()
319|         Log.info(obj)
320|     except:
321|         etype, evalue, tb = exc_info()
322|         ts = extract_tb(tb)
323|         Log.error()
324|         Log.error('%s: %s' % (etype.__name__,str(evalue)))
325|         Log.error()
326|         for t in ts[-5:]:
327|             Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
328|             Log.error(' %s' % t[3])
329|             Log.error()
330|         raise
331|     finally:
332|         Log.end()
333| ####################################################################################################
334| '''eof'''
335|