projects
/
talweg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix mistake; perfs not as good as expected. Why?
[talweg.git]
/
reports
/
ipynb_generator.py
diff --git
a/reports/ipynb_generator.py
b/reports/ipynb_generator.py
old mode 100644
(file)
new mode 100755
(executable)
index
a89ec40
..
456fc22
--- a/
reports/ipynb_generator.py
+++ b/
reports/ipynb_generator.py
@@
-1,4
+1,6
@@
-import sys, os, re, logging
+#!/usr/bin/env python
+
+import sys, os, re
# Languages mapping as used by markdown/pandoc
shortname2language = dict(
# Languages mapping as used by markdown/pandoc
shortname2language = dict(
@@
-15,7
+17,7
@@
shortname2language = dict(
tex='Tex',
)
tex='Tex',
)
-def read(text, argv=sys.argv[
2
:]):
+def read(text, argv=sys.argv[
3
:]):
lines = text.splitlines()
# First read all include statements
for i in range(len(lines)):
lines = text.splitlines()
# First read all include statements
for i in range(len(lines)):
@@
-25,7
+27,6
@@
def read(text, argv=sys.argv[2:]):
include_text = f.read()
lines[i] = include_text
text = '\n'.join(lines)
include_text = f.read()
lines[i] = include_text
text = '\n'.join(lines)
- logging.info('******* text after include:\n{}'.format(text))
# Run Mako
mako_kwargs = {}
# Run Mako
mako_kwargs = {}
@@
-44,13
+45,10
@@
def read(text, argv=sys.argv[2:]):
from mako.template import Template
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=[os.curdir])
from mako.template import Template
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=[os.curdir])
- text = text.encode('utf-8')
+
#
text = text.encode('utf-8')
temp = Template(text=text, lookup=lookup, strict_undefined=True)
temp = Template(text=text, lookup=lookup, strict_undefined=True)
- logging.info('******* mako_kwargs: {}'.format(str(mako_kwargs)))
text = temp.render(**mako_kwargs)
text = temp.render(**mako_kwargs)
- logging.info('******* text after mako:\n{}'.format(text))
-
# Parse the cells
lines = text.splitlines()
cells = []
# Parse the cells
lines = text.splitlines()
cells = []
@@
-65,11
+63,7
@@
def read(text, argv=sys.argv[2:]):
if shortname:
# Check if code is to be typeset as static
# Markdown code (e.g., shortname=py-t)
if shortname:
# Check if code is to be typeset as static
# Markdown code (e.g., shortname=py-t)
- logging.info('******* found shortname {}'
- .format(shortname))
astext = shortname[-2:] == '-t'
astext = shortname[-2:] == '-t'
- logging.info('******* cell: astext={} shortname={}'
- .format(astext, shortname))
if astext:
# Markdown
shortname = shortname[:-2]
if astext:
# Markdown
shortname = shortname[:-2]
@@
-83,7
+77,6
@@
def read(text, argv=sys.argv[2:]):
inside = 'codecell'
cells.append(['codecell', fullname, []])
else:
inside = 'codecell'
cells.append(['codecell', fullname, []])
else:
- logging.info('******* cell: markdown')
# Markdown cell
inside = 'markdown'
cells.append(['markdown', 'text', ['\n']])
# Markdown cell
inside = 'markdown'
cells.append(['markdown', 'text', ['\n']])
@@
-103,9
+96,6
@@
def read(text, argv=sys.argv[2:]):
# Add an ending ``` of code
cells[i][2].append('```\n')
cells[i][2] = '\n'.join(cells[i][2])
# Add an ending ``` of code
cells[i][2].append('```\n')
cells[i][2] = '\n'.join(cells[i][2])
- # TODO: optional logging
- import pprint
- logging.info('******* cell data structure:\b%s' % pprint.pformat(cells))
return cells
def write(cells):
return cells
def write(cells):
@@
-130,24
+120,20
@@
def write(cells):
def driver():
"""Compile a document and its variables."""
try:
def driver():
"""Compile a document and its variables."""
try:
-
filenam
e = sys.argv[1]
- with open(
filenam
e, 'r') as f:
+
inputfil
e = sys.argv[1]
+ with open(
inputfil
e, 'r') as f:
text = f.read()
text = f.read()
+ # Assuming file extension .gj (generate Jupyter); TODO: less strict
+ outputfile = inputfile[:-3]+'.ipynb' if (len(sys.argv)<=2 or sys.argv[2]=='-') \
+ else sys.argv[2]
except (IndexError, IOError) as e:
except (IndexError, IOError) as e:
- print('Usage: %s
filename
' % (sys.argv[0]))
+ print('Usage: %s
inputfile [outputfile|- [Mako args]]
' % (sys.argv[0]))
print(e)
sys.exit(1)
print(e)
sys.exit(1)
- cells = read(text, argv=sys.argv[
2
:])
+ cells = read(text, argv=sys.argv[
3
:])
filestr = write(cells)
filestr = write(cells)
- # Assuming file extension .gj (generate Jupyter); TODO: less strict
- filename = filename[:-3] + '.ipynb'
- with open(filename, 'w') as f:
+ with open(outputfile, 'w') as f:
f.write(filestr)
if __name__ == '__main__':
f.write(filestr)
if __name__ == '__main__':
- logfile = 'tmp.log'
- if os.path.isfile:
- os.remove(logfile)
- logging.basicConfig(format='%(message)s', level=logging.DEBUG,
- filename=logfile)
driver()
driver()