-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworknotes.py
535 lines (498 loc) · 18.8 KB
/
worknotes.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 31 11:09:57 2015
@author: koehler
"""
from __future__ import unicode_literals
import items
def find_category(item):
"""
Determines item category from item type and content
Args
----
item : arbitrary
Arbitrary valid item
Returns
-------
cat : str
The determined item category
"""
import numpy
try:
from matplotlib.figure import Figure as MPL_Figure
except ImportError:
MPL_Figure = None
from os import path
if type(item) in [str, unicode]:
if path.splitext(item)[1] in ['.pdf', '.jpg', '.png', '.jpeg']:
cat = 'figure'
elif item.strip()[:2] == "$$" and item.strip()[-2:] == "$$":
cat = 'equation'
elif item.strip()[:2] == "* ":
cat = 'list'
elif item.strip()[:2] == "# ":
cat = 'enumerate'
elif len(item.split("\n")) == 2 and \
len(item.split("\n")[1]) >= 3 and \
item.split("\n")[1][:3] == '---':
cat = 'slide'
else:
cat = 'text'
elif type(item) == MPL_Figure and MPL_Figure != None:
cat = 'figure'
elif type(item) == list or type(item) == numpy.ndarray:
cat = 'table'
elif type(item) in [float, numpy.float64, int, numpy.int64]:
cat = 'value'
else:
cat = None
raise TypeError("Category of item not recognized: %s"%type(item))
return cat
class Worknote(items.NoteContainer):
"""
Class That allows to drop comments in figures into a presentation while
interactively working with python
Args
----
workdir : str
represents project name and working directory that will be created
title : str
Title of document
author : str
Author name
date : str
Date of document
subtitle : str
Document subtitle
"""
def __init__(self, workdir=None, title='', author='', date='',
subtitle='', load_if_used=True, **kwargs):
super(Worknote, self).__init__(**kwargs)
self.head['Beamer'] = """
\\documentclass{beamer}
\\mode<presentation>
{
\\usetheme{Boadilla}
%\\usetheme{Pittsburgh}
%\\setbeamercovered{transparent}
}
\\setbeamertemplate{footline}[frame number]
\\setbeamertemplate{navigation symbols}{}
\\usepackage[english]{babel}
\\usepackage[utf8]{inputenc}
%%%METADATA%%%
\\begin{document}
%%%TITLEPAGE%%%
"""
self.foot['Beamer'] = "\\end{document}"
self.head['Report'] = """
\\documentclass{article}
\\usepackage[english]{babel}
\\usepackage[utf8]{inputenc}
\\usepackage{graphicx}
\\usepackage{a4wide}
\\usepackage{color}
%%%METADATA%%%
\\begin{document}
%%%TITLEPAGE%%%
"""
self.foot['Report'] = "\\end{document}"
self.head['Markdown'] = '%%%METADATA%%%'
self.foot['Markdown'] = ''
self.metadata = Metadata()
self.set_metadata(title, author, date, subtitle)
self.workdir = None
self.set_workdir(workdir, load_if_used=load_if_used)
def add_item(self, item, index=[], **kwargs):
"""
Insert the item at the given index.
Args
----
index : list
A valid index assignment
item : NoteItem
A valid NoteItem (or subclass) object
"""
if type(item) == items.Slide:
super(Worknote, self).add_item(item, index=index)
else:
self[index[0:1]].add_item(item, index=index[1:])
def __call__(self, item, cat=None, index=[], **kwargs):
"""
Adds an item to the last slide
Args
----
items : various
item to add to slide, can be str, fig, ...
cat : str
Category of item. If none is given, it will be determined
through the type function
index : int or str or iterable or []
Index must be either an integer index, an iterable list of integer
indices or an index notation of the style 'i:j:k' where indices are
separated by colons. If index an empty list, the item will be
appended to the last element.
**kwargs : keyword arguments
Args like figsize etc
"""
if cat == None:
cat = find_category(item)
if cat in ['figure', 'figurepage'] and self.workdir is None:
print 'Cannot add figure until working directory is set'
return
item = items.TYPES[cat](item, workdir=self.workdir, **kwargs)
index = items.parse_index(index)
if cat == 'figurepage':
item = items.Slide("").add_item(item)
index = index[0:1]
elif cat == 'slide':
index = index[0:1]
self.add_item(item, index)
def build(self, style='Beamer'):
"""
Generate output in a given style
Argument style is currently unused and default Beamer
Args
----
style : str
Build format (default = 'Beamer'). Options are:
* 'Beamer' - Build Beamer.tex and generate Beamer.pdf
* 'Beamer.tex' - Build Beamer.tex
* 'Report' - Build Report.pdf
* 'Report.tex' - Build Report.tex
* 'Markdown' - Build Report.md
* More to come!
"""
from os import path
import codecs
build_pdf = True
if style[-4:] == '.tex':
build_pdf = False
style = style[:-4]
if style in ['Beamer', 'Report']:
f_out = codecs.open(path.join(self.workdir, style+".tex"), 'w',
encoding='utf-8')
elif style in ['Markdown']:
f_out = codecs.open(path.join(self.workdir, "Report.md"), 'w',
encoding='utf-8')
build_pdf = False
else:
print 'Style "{:s}" is not a supported output style'.format(style)
return
f_out.write(self.get_text(style=style))
f_out.close()
if build_pdf:
print "Building pdf"
from subprocess import call
import os
FNULL = open(os.devnull, 'w')
build = call(["pdflatex", style+".tex"],
cwd=self.workdir,
stdout=FNULL)
build = call(["pdflatex", style+".tex"],
cwd=self.workdir,
stdout=FNULL)
if build == 0:
print "Building sucessful: %s"%path.join(self.workdir, style+".pdf")
else:
print "Errors encountered during build"
print "Check %s for problems"%path.join(self.workdir, style+".tex")
def set_workdir(self, workdir, load_if_used=True):
"""
Set the working directory. If load_if_used is True or there are no
items in the current notes, any worknotes present in the directory will
automatically be loaded.
Args
----
workdir : str
Path of the working directory to use. If the last directory does
not exist, it will be created.
"""
from os.path import exists, join, expanduser, expandvars
from os import listdir, remove, rmdir, mkdir
if not workdir is None:
if self.workdir is None:
self.workdir = expanduser(expandvars(workdir))
if not exists(self.workdir):
try:
mkdir(self.workdir)
except OSError:
print "ERROR: Unable to create working directory"
else:
if not len(listdir(self.workdir)) == 0:
if exists(join(self.workdir, 'notedata.worknote')):
if load_if_used:
self.load(verbosity=1)
else:
print self.workdir, 'is already in use, cleaning...'
files = listdir(self.workdir)
for fn in files:
fnpath = join(self.workdir, fn)
remove(fnpath)
print 'Done.'
else:
pass
else:
print 'WARNING: No working directory set'
print '\tUnable to save or add figures'
self.workdir = None
def save(self):
"""
Save the worknotes to the working directory
"""
import cPickle
from os.path import join
with open(join(self.workdir,
'notedata.worknote'), 'wb') as outfile:
cPickle.dump(self.head, outfile, cPickle.HIGHEST_PROTOCOL)
cPickle.dump(self.foot, outfile, cPickle.HIGHEST_PROTOCOL)
cPickle.dump(self.items, outfile, cPickle.HIGHEST_PROTOCOL)
cPickle.dump(self.metadata, outfile, cPickle.HIGHEST_PROTOCOL)
def load(self, workdir=None, verbosity=0):
"""
Load the worknotes from a working directory
Args
----
workdir : str
The directory to load from. Can be passed as None to use the
workdir previously set (e.g. using set_workdir or during init).
verbosity : int
Select output verbosity. Defaults to 0 (= no output)
"""
import cPickle
from os.path import join, exists
if self.workdir is None:
if workdir is None:
raise OSError('No working directory given')
self.set_workdir(workdir)
if verbosity > 0:
print 'Loading existing worknote from "%s"...'%self.workdir
with open(join(self.workdir,
'notedata.worknote'), 'rb') as infile:
self.head = cPickle.load(infile)
self.foot = cPickle.load(infile)
self.items = cPickle.load(infile)
self.metadata = cPickle.load(infile)
self.remove_orphaned_figures()
def set_metadata(self, title="", author="", date="", subtitle=""):
"""
Set the metadata used to generate a title page, if any is present.
Set any field to an empty string ('') to remove it from output.
Pass None for any field to keep current value.
Args
----
title : str
author : str
date : str
"""
self.metadata.set_metadata(title=title, author=author, date=date,
subtitle=subtitle)
def get_text(self, style='Beamer'):
"""
Returns the ASCII tex string
"""
if style not in self.head:
style = 'default'
text = ""
text += self.head[style]
text = text.replace('%%%METADATA%%%', self.metadata.get_metadata(style))
if not len(self.metadata) == 0:
text = text.replace('%%%TITLEPAGE%%%', self.metadata.get_titlepage(style))
else:
text = text.replace('%%%TITLEPAGE%%%', '')
for item in self.items:
text += item.get_text(style)
text += self.foot[style]
return text
def __str__(self):
text = u"Worknote: " + str(self.metadata)
for i in xrange(len(self.items)):
text += "\n%d %s"%(i, self.items[i].__str__())
return text
def remove(self, index = []):
"""
Remove the item at the given index.
Args
----
index : int or str or iterable
Index must be either an integer index, an iterable list of integer
indices or an index notation of the style 'i:j:k' where indices are
separated by colons
"""
item = self.pop(index)
if type(item) == items.Figure:
if item.exists_fig_file():
item.remove_fig_file()
elif type(item) == items.Slide:
for subitem in item.items:
if type(subitem) == items.Figure:
if subitem.exists_fig_file():
subitem.remove_fig_file()
self.reindex_fig_files()
def move(self, src_index, dest_index):
"""
Move the object at src_index to the dest_index
Args
----
src_index : int or str or iterable
The source index. Index must be either an integer index, an
iterable list of integer indices or an index notation of the
style 'i:j:k' where indices are separated by colons
dest_index : int or str or iterable
The destination index
"""
import items
src_index = items.parse_index(src_index)
dest_index = items.parse_index(dest_index)
if type(self[src_index]) == items.Slide:
dest_index = dest_index[0:1]
elif type(self[src_index]) in [items.ListItem, items.EnumItem]:
dest_index = dest_index[0:3]
else:
dest_index = dest_index[0:2]
item = self.pop(src_index)
self.add_item(item, dest_index)
self.reindex_fig_files()
def clean_workdir(self, verbosity = 1):
"""
Clean up unneeded files in the working directory. This will remove
any file except those referenced by figures and notedata.worknote
"""
from os import listdir
from os import remove
from os.path import join
do_not_delete = ['notedata.worknote', 'Beamer.tex', 'Beamer.pdf',
'Report.tex', 'Report.pdf', 'Report.md']
files = listdir(self.workdir)
fig_files = []
for slide in self.items:
for item in slide.items:
if type(item) == items.Figure:
fig_files.append(item.data)
for fn in fig_files:
files.remove(fn)
for fn in do_not_delete:
if fn in files:
files.remove(fn)
if verbosity > 0:
print 'Removing', len(files), 'files from "' + self.workdir + '"...'
for fn in files:
remove(join(self.workdir, fn))
def reindex_fig_files(self):
new_indices = []
index = 1
#We determine the new index order:
for slide in self.items:
for item in slide.items:
if type(item) == items.Figure:
new_indices.append([item, index])
index += 1
#Reindexing pass 1 shifts everything to temporary filenames:
for figure, new_index in new_indices:
figure.move_fig_file(new_index*-1)
#Reindexing pass 2 produces the final filenames:
for figure, new_index in new_indices:
figure.move_fig_file(new_index)
def remove_orphaned_figures(self):
indices = []
for slide_index in range(0, len(self.items)):
for item_index in range(0, len(self.items[slide_index].items)):
if type(self.items[slide_index].items[item_index]) == items.Figure:
if not self.items[slide_index].items[item_index].exists_fig_file():
indices.append([slide_index, item_index])
if len(indices) > 0:
print 'Removing', len(indices), 'orphaned figures...'
for index in indices:
self.remove(index)
self.save()
class Metadata(object):
"""
Class to handle metadata
Args
----
title : str
author : str
date : str
subtitle : str
"""
def __init__(self, title='', author='', date='', subtitle=''):
self.metadata = {}
self.formatter = {}
self.formatter['title'] = {}
self.formatter['title']['Beamer'] = '\\title{%s}\n'
self.formatter['title']['Report'] = '\\title{%s}\n'
self.formatter['title']['Markdown'] = '# %s\n'
self.formatter['subtitle'] = {}
self.formatter['subtitle']['Beamer'] = '\\subtitle{%s}\n'
self.formatter['subtitle']['Report'] = '\\subtitle{%s}\n'
self.formatter['subtitle']['Markdown'] = ' %s\n'
self.formatter['date'] = {}
self.formatter['date']['Beamer'] = '\\date{%s}\n'
self.formatter['date']['Report'] = '\\date{%s}\n'
self.formatter['date']['Markdown'] = ' %s\n\n'
self.formatter['author'] = {}
self.formatter['author']['Beamer'] = '\\author{%s}\n'
self.formatter['author']['Report'] = '\\author{%s}\n'
self.formatter['author']['Markdown'] = ' %s\n'
self.titlepage_generator = {}
self.titlepage_generator['Beamer'] = "\\frame[plain]{\\titlepage}\n"
self.titlepage_generator['Report'] = "\\maketitle\n"
self.titlepage_generator['Markdown'] = ""
self.supported_metadata = {}
self.supported_metadata['Beamer'] = ['title', 'author', 'date', 'subtitle']
self.supported_metadata['Report'] = ['title', 'author', 'date']
self.supported_metadata['Markdown'] = ['title', 'subtitle', 'author', 'date']
self.set_metadata(title=title, author=author, date=date,
subtitle=subtitle)
def get_metadata(self, style):
"""
Returns a proper formated metadata string
"""
metadata_str = ""
for metadata in self.supported_metadata[style]:
if self.metadata[metadata]:
metadata_str += self.formatter[metadata][style]%self.metadata[metadata]
return metadata_str
def get_titlepage(self, style):
"""
Returns a properly formated titelpage string
"""
return self.titlepage_generator[style]
def set_metadata(self, title="", author="", date="", subtitle=""):
"""
Set the metadata used to generate a title page, if any is present.
Set any field to an empty string ('') to remove it from output.
Pass None for any field to keep current value.
Args
----
title : str
author : str
date : str
subtitle : str
"""
if not 'title' in self.metadata: #initialize
self.metadata['title'] = ''
if not 'author' in self.metadata:
self.metadata['author'] = ''
if not 'date' in self.metadata:
self.metadata['date'] = ''
if not 'subtitle' in self.metadata:
self.metadata['subtitle'] = ''
if title:
self.metadata['title'] = items.set_unicode(title)
if author:
self.metadata['author'] = items.set_unicode(author)
if date:
self.metadata['date'] = items.set_unicode(date)
if subtitle:
self.metadata['subtitle'] = items.set_unicode(subtitle)
def __len__(self):
len = 0
for key in self.metadata:
if self.metadata[key]:
len += 1
return len
def __str__(self):
if 'title' in self.metadata:
return self.metadata['title']
return ""