-
Notifications
You must be signed in to change notification settings - Fork 1
/
jupman_tools.py
1351 lines (1029 loc) · 51.3 KB
/
jupman_tools.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
# this way we don't get warning of other libs in pytest, see https://stackoverflow.com/a/63946841
logging.captureWarnings(True)
import zipfile
from pylatexenc.latexencode import unicode_to_latex
from enum import Enum
import re
import os
import shutil
import inspect
import types
import glob
import stat
import datetime
from nbconvert.preprocessors import Preprocessor
class JupmanFormatter(logging.Formatter):
def format(self, record):
if record.levelno == logging.INFO:
self._style._fmt = " %(message)s"
else:
self._style._fmt = "\n\n %(levelname)s: %(message)s"
return super().format(record)
logger = logging.getLogger('jupman')
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(JupmanFormatter())
logger.addHandler(console_handler)
def fatal(msg, ex=None):
""" Prints error and exits (halts program execution immediatly)
"""
if ex == None:
exMsg = ""
else:
exMsg = " \n %s" % repr(ex)
logger.critical("\n\n FATAL ERROR! %s%s\n\n" % (msg,exMsg))
exit(1)
def error(msg, ex=None):
""" Prints error and reraises exception (printing is useful as sphinx puts exception errors in a separate log)
"""
if ex == None:
exMsg = ""
the_ex = Exception(msg)
else:
exMsg = " \n %s" % repr(ex)
the_ex = ex
logger.error("\n\n FATAL ERROR! %s%s\n\n" % (msg,exMsg))
raise the_ex
def info(msg=""):
logger.info(" %s" % msg)
def warn(msg, ex=None):
logger.warning("\n\n WARNING: %s" % msg)
def debug(msg=""):
logger.debug(" DEBUG=%s" % msg)
def parse_date(ld):
try:
return datetime.datetime.strptime( str(ld), "%Y-%m-%d")
except Exception as e:
raise Exception("NEED FORMAT 'yyyy-mm-dd', GOT INSTEAD: '%s'" % ld, e)
def parse_date_str(ld) -> str:
"""
NOTE: returns a string
"""
return str(parse_date(ld)).replace(' 00:00:00','')
def super_doc_dir():
return os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
def detect_release():
""" Return a release string, calling git to find tags.
If unsuccessful return 'dev'.
"""
try:
from subprocess import check_output
release = check_output(['git', 'describe', '--tags', '--always'])
release = release.decode().strip()
if not '.' in release:
raise Exception
except Exception:
release = 'dev'
return release
def get_version(release):
""" Given x.y.z-something, return x.y
On ill-formed return verbatim.
"""
if '.' in release:
sl = release.split(".")
return '%s.%s' % (sl[0], sl[1])
else:
return release
def expand_JM(source, target, exam_date, conf):
d = parse_date(exam_date)
sourcef = open(source, "r")
s = sourcef.read()
s = s.replace('_JM_{exam.date}', exam_date )
s = s.replace('_JM_{exam.date_human}', d.strftime('%a %d, %b %Y') )
for k in conf.__dict__:
s = s.replace('_JM_{conf.' + k + '}', str(conf.__dict__[k]))
for k in conf.jm.__dict__:
s = s.replace('_JM_{conf.jm.' + k + '}', str(conf.jm.__dict__[k]))
p = re.compile(r'_JM_\{[a-zA-Z][\w\.]*\}')
if p.search(s):
warn("FOUND _JM_ macros which couldn't be expanded!")
warn(" file: %s" % source)
warn("\n ".join(p.findall(s)))
warn("")
destf = open(target, 'w')
destf.write(s)
def _replace_title( nb_node, source_abs_fn, replacement, title_pat=r'(.*)') -> str:
""" Finds the title of a notebook and replaces it with replacement
Returns the old title.
"""
# look for title
pat = re.compile(r'^(\s*#\s+)'+ title_pat)
for cell in nb_node.cells:
if cell.cell_type == "markdown":
ma = pat.search(cell.source)
if ma:
found_title = ma.group(0)
cell.source = re.sub(pat,
replacement,
cell.source)
break
if not ma:
error("Couldn't find title in file: \n %s\nThere should be a markdown cell beginning with text # bla bla Complete pattern: %s" % (source_abs_fn,pat.pattern))
return found_title
class FileKinds(Enum):
SOLUTION = 1
EXERCISE = 2
TEST = 3
OTHER = 4
CHALLENGE_SOLUTION = 5
@staticmethod
def sep(ext):
if ext == 'py':
return '_'
else:
return '-'
@staticmethod
def is_supported_ext(fname, supp_ext):
for ext in supp_ext:
if fname.endswith('.%s' % ext):
return True
return False
@staticmethod
def detect(fname):
""" TODO can't detect EXERCISE
"""
l = fname.split(".")
if len(l) > 0:
ext = l[-1]
else:
ext = ''
sp = FileKinds.sep(ext)
if fname.endswith('%schal%ssol.%s' % (sp, sp, ext)):
return FileKinds.CHALLENGE_SOLUTION
elif fname.endswith('%ssol.%s' % (FileKinds.sep(ext), ext)):
return FileKinds.SOLUTION
elif fname.endswith("_test.py") :
return FileKinds.TEST
else:
return FileKinds.OTHER
@staticmethod
def check_ext(fname, supp_ext):
if not FileKinds.is_supported_ext(fname, supp_ext):
raise Exception("%s extension is not supported. Valid values are: %s" % (fname, supp_ext))
@staticmethod
def exercise(radix, ext, supp_ext):
FileKinds.check_ext(ext,supp_ext)
return radix + "." + ext
@staticmethod
def exercise_from_solution(fname, supp_ext):
FileKinds.check_ext(fname, supp_ext)
ext = fname.split(".")[-1]
return fname.replace(FileKinds.sep(ext) + "sol." + ext, "." + ext)
@staticmethod
def solution(radix, ext, supp_ext):
FileKinds.check_ext(ext, supp_ext)
return radix + FileKinds.sep(ext) + 'sol.' + ext
@staticmethod
def test(radix):
return radix + '_test.py'
def check_paths(path, path_check):
if not isinstance(path, str):
raise ValueError("Path to delete must be a string! Found instead: %s " % type(path))
if len(path.strip()) == 0:
raise ValueError("Provided an empty path !")
if not isinstance(path_check, str):
raise ValueError("Path check to delete must be a string! Found instead: %s" % type(path_check))
if len(path_check.strip()) == 0:
raise ValueError("Provided an empty path check!")
if not path.startswith(path_check):
fatal("FAILED SAFETY CHECK FOR DELETING DIRECTORY %s ! \n REASON: PATH DOES NOT BEGIN WITH %s" % (path, path_check) )
def uproot(path):
""" Returns a relative path from input path to root.
NOTE: IT IS SUPPOSED TO BE RUN FROM A PYTHON FILE RESIDING *AT ROOT LEVEL*
Example:
>>> uproot('_static/img/cc-by.png')
>>> '../../'
>>> uproot('_static')
>>> '../'
"""
if not path:
raise ValueError('Invalid filepath=%s' % path)
ret = os.path.relpath(os.path.abspath(os.getcwd()),
os.path.abspath(path))
if os.path.isfile(path) and ret.endswith('..'):
ret = ret[:-2]
if ret.endswith('..'):
ret = ret + '/'
return ret
def delete_tree(path, path_check):
""" Deletes a directory, checking you are deleting what you really want
path: the path to delete as a string
path_check: the beginning of the path to delete, as a string
"""
info("Cleaning %s ..." % path)
check_paths(path, path_check)
if not os.path.isdir(path):
raise Exception("Provided path is not a directory: %s" % path)
#duplicated check (it's already in check_paths, but just to be sure)
if path.startswith(path_check):
shutil.rmtree(path)
else:
fatal("FAILED SAFETY CHECK FOR DELETING DIRECTORY %s ! \n REASON: PATH DOES NOT START WITH %s" % (path, path_check) )
def delete_file(path, path_check):
""" Deletes a file, checking you are deleting what you really want
path: the path to delete as a string
path_check: the beginning of the path to delete, as a string
"""
info("Cleaning %s ..." % path)
check_paths(path, path_check)
if not os.path.isfile(path):
raise Exception("Provided path is not a file: %s" % path)
#duplicated check (it's already in check_paths, but just to be sure)
if path.startswith(path_check):
os.remove(path)
else:
fatal("FAILED SAFETY CHECK FOR DELETING FILE %s ! \n REASON: PATH DOES NOT START WITH %s" % (path, path_check) )
def tag_start(tag):
return '#' + tag
def tag_end(tag):
return '#/' + tag
def tag_regex(string, must_begin=True, preserve_line=False):
""" Takes a non-regex string and return a regex string which ignores extra
spaces in s and newline after
must_begin : if True, provided string must be at the beginning of code / cell
preserve_line : if True, characters following the tag until the end of the line are ignored
@since 3.3
"""
if len(string) == 0:
raise ValueError("Expect a non-empty string !")
# so we do not get spaces escaped, which I find horrible default behaviour:
# https://stackoverflow.com/questions/32419837/why-re-escape-escapes-space
escaped = [re.escape(x) for x in string.split()]
removed_spaces = r'\s+'.join(escaped)
begin_char= r'^' if must_begin else ''
if preserve_line:
preservel = r'(.*?)\n'
else:
preservel = r'()'
return r"(?s)(%s\s*%s)%s(.*)" % (begin_char, removed_spaces, preservel)
def ignore_spaces(string, must_begin=True):
""" Takes a non-regex string and return a regex string which ignores extra
spaces in s and newline after
must_begin : if True, provided string must be at the beginning of code / cell
@deprecated: use tag_regex instead
"""
warn("DEPRECATED: jupman_tools.ignore_spaces is deprecated, use tag_regex instead")
return tag_regex(string, must_begin)
def multi_replace(text, d):
""" Takes a dictionary pattern -> substitution and applies all substitutions to text
"""
s = text
for key in d:
s = re.sub(key, d[key], s)
return s
def span_pattern(tag):
""" NOTE: Doesn't eat the blank afterwards, has lookahead
@since 3.3
"""
s = r"%s(.*?)%s" % (start_tag_pattern(tag).pattern, end_tag_pattern(tag).pattern)
return re.compile(s, flags=re.DOTALL)
def start_tag_pattern(tag):
""" NOTE: start tag always eats the blank afterwards, end tag doesn't (has lookahead)
@since 3.3
"""
return re.compile(r"#%s\s" % tag, flags=re.DOTALL)
def end_tag_pattern(tag):
""" NOTE: start tag always eats the blank afterwards, end tag doesn't (has lookahead)
@since 3.3
"""
ec = tag_end(tag)[-1]
s = r"%s((%s$)|(%s(?=\s)))" % (tag_end(tag)[:-1], ec, ec)
return re.compile(s, flags=re.DOTALL)
def single_tag_pattern(tag):
""" NOTE: Doesn't eat the blank afterwards, has lookahead
@since 3.3
"""
ec = tag_start(tag)[-1]
s = r"%s((%s$)|(%s(?=\s)))" % (tag_start(tag)[:-1], ec, ec)
return re.compile(s, flags=re.DOTALL)
def init(jupman, conf={}):
"""Initializes the system, does patching, etc
Should be called in conf.py at the beginning of setup()
@since 3.2
"""
logging.getLogger().setLevel(logging.INFO)
if not conf:
warn("globals() not passed to jmt.init call, please add it!")
else:
info("release: %s" % conf['release'])
if os.environ.get('GOOGLE_ANALYTICS'):
info("Found GOOGLE_ANALYTICS environment variable")
conf['html_theme_options']['analytics_id'] = os.environ.get('GOOGLE_ANALYTICS')
else:
info('No GOOGLE_ANALYTICS environment variable was found, skipping it')
# hooks into ExecutePreprocessor.preprocess to execute our own filters.
# Method as in https://github.com/spatialaudio/nbsphinx/issues/305#issuecomment-506748814
def _from_notebook_node(self, nb, resources, **kwargs):
debug('patched nbsphinx from_notebook_node')
for f in jupman.preprocessors:
nb, resources = f.preprocess(nb, resources=resources)
return nbsphinx_from_notebook_node(self, nb, resources=resources, **kwargs)
import nbsphinx
nbsphinx_from_notebook_node = nbsphinx.Exporter.from_notebook_node
nbsphinx.Exporter.from_notebook_node = _from_notebook_node
def make_stripped_cell_id(cid):
""" Given a solution id, creates a predictable new one
@since 3.3
"""
#See https://nbformat.readthedocs.io/en/latest/format_description.html#cell-ids
mx = 64
postfix = "-stripped" # note: - is legal
cand = cid + postfix
if len(cand) > mx:
return cid[:len(cid)-len(postfix)] + postfix
else:
return cand
class JupmanPreprocessor(Preprocessor):
""" @since 3.2 """
def __init__(self, jupman):
""" @since 3.2 """
super().__init__()
self.jupman = jupman
def preprocess(self, nb, resources):
""" @since 3.2 """
"""Careful path *includes* part of docname:
resources:
{
'metadata': {
'path': '/home/da/Da/prj/jupman/prj/jupyter-example'
},
'nbsphinx_docname': 'jupyter-example/jupyter-example-sol',
'nbsphinx_save_notebook': '/home/da/Da/prj/jupman/prj/_build/html/.doctrees/nbsphinx/jupyter-example/jupyter-example-sol.ipynb',
'output_files_dir': '../_build/html/.doctrees/nbsphinx',
'unique_key': 'jupyter-example_jupyter-example-sol'
}
"""
rel_dir, partial_fn = os.path.split(resources['nbsphinx_docname'])
source_abs_fn = os.path.join(resources['metadata']['path'], partial_fn + '.ipynb')
if self.jupman._is_to_preprocess(nb, source_abs_fn):
relpath = os.path.relpath(source_abs_fn, os.path.abspath(os.getcwd()))
info("JupmanPreprocessor: parsing %s" % relpath )
try:
self.jupman.validate_tags(source_abs_fn)
except Exception as ex:
logger.warning("Failed Jupman tags validation! %s", ex)
return self.jupman._sol_nb_to_ex(nb, source_abs_fn,website=True), resources
else:
return nb, resources
def replace_py_rel(code, filepath):
""" Takes code to be copied into zips and removes unneeded relative imports
"""
upr = uproot(filepath).replace('.',r'\.')
if len(re.findall(r'sys\..+', code)) > 1:
repl = r'import sys\n'
else:
repl = ''
ret = re.sub(r'import\s+sys\s*;?\s*\nsys\.path.append\([\'\"]((%s))[\'\"]\)\s*;?\s*' % upr,
repl,
code)
return ret
def replace_md_rel(code, filepath):
""" Takes markdown to be copied into zips and removes relative paths
"""
upr = uproot(filepath).replace('.',r'\.')
ret = re.sub(r'(\[.*?\]\()(%s)(.*?\))' % upr,
r"\1\3",
code)
ret = replace_html_rel(ret, filepath)
return ret
def replace_html_rel(code, filepath):
""" NOTE: supported syntax is very rigid!
"""
upr = uproot(filepath).replace('.',r'\.')
ret = re.sub(r'(<a\s+)(target=\"_blank\"\s+)*(href=\")(%s)(.*?\"(\s+.*?=\".*?\")*>)' % upr,
r"\1\2\3\5",
code)
ret = re.sub(r'(<img\s+)(alt=\".*\"\s+)*(src=\")(%s)(.*?\"(\s+.*?=\".*?\")*>)' % upr,
r"\1\2\3\5",
ret)
ret = re.sub(r'(<script\s+src=\")(%s)(.+?.js\")' % upr,
r"\1\3",
ret)
ret = re.sub(r'(<style>[\s\n]*\@import[\s\n]+"?)(%s)(.+.css"?)' % upr,
r"\1\3",
ret)
return ret
def replace_ipynb_rel(nb_node, filepath, website=False):
""" MODIFIES nb_node without returning it !
"""
for cell in nb_node.cells:
if cell.cell_type == "code":
cell.source = replace_py_rel(cell.source, filepath)
if not website:
for output in cell.outputs:
if 'data' in output:
if "text/html" in output.data:
output.data["text/html"] = replace_html_rel(output["data"]["text/html"], filepath)
elif cell.cell_type == "markdown":
# markdown cells: fix rel urls
if not website:
cell.source = replace_md_rel(cell.source, filepath)
elif cell.cell_type == "raw" and \
'raw_mimetype' in cell.metadata and cell.metadata['raw_mimetype'] == 'text/html':
if not website:
cell.source = replace_html_rel(cell.source, filepath)
else:
#TODO latex ?
pass
class Jupman:
""" Holds Jupman-specific configuration for Sphinx build
"""
def __init__(self):
self.subtitle = "TODO CHANGE jm.subtitle"""
self.course = "TODO CHANGE jm.course"
self.degree = "TODO CHANGE jm,degree"
self.filename = 'jupman' # The filename without the extension
""" 'filename' IS *VERY* IMPORTANT !!!!
IT IS PREPENDED IN MANY GENERATED FILES
AND IT SHOULD ALSO BE THE SAME NAME ON READTHEDOCS
(like i.e. jupman.readthedocs.org) """
self.chapter_files = ['jupman.py', 'my_lib.py', '_static/img/cc-by.png',
'_static/js/jupman.js', # these files are injected when you call jupman.init()
'_static/css/jupman.css',
'_static/js/toc.js',
'_static/js/pytutor-embed.bundle.min.js.zip' ]
""" Common files for exercise and exams as paths. Paths are intended relative to the project root. Globs like /**/* are allowed."""
self.chapter_patterns = ['*/']
self.chapter_exclude_patterns = ['[^_]*/','exams/', 'project/']
self.ipynb_show_solution = "Show solution"
self.ipynb_hide_solution = "Hide solution"
self.ipynb_show_answer = "Show answer"
self.ipynb_hide_answer = "Hide answer"
self.ipynb_solutions = "SOLUTIONS"
self.ipynb_exercises = "EXERCISES"
""" words used in ipynb files - you might want to translate these in your language. Use plurals."""
self.write_solution_here = tag_regex("# write here", must_begin=False, preserve_line=True)
""" the string is not just a translation, it's also a command that when
building the exercises removes the content after it in the code cell it is
contained in. """
self.solution = tag_regex("# SOLUTION")
""" #NOTE: the string is not just a translation, it's also a command
that when building the exercises completely removes the content of the cell
it is contained in (solution comment included)."""
self.markdown_answer = tag_regex('**ANSWER**:')
"""NOTE: the string is not just a translation, it's also a command
that when building the exercises removes the content after it in
the markdown cell it is contained in.
"""
self.zip_ignored = ['__pycache__', '**.ipynb_checkpoints', '.pyc', '.cache', '.pytest_cache', '.vscode',]
self.formats = ["html", "epub", "latex"]
self.build = "_build"
# Output directory. Not versioned.
self.generated='_static/generated'
# Directory where to put zips. Versioned.
# NOTE: this is *outside* build directory
self.manuals = {
"student": {
"name" : "Jupman", # TODO put manual name, like "Scientific Programming"
"audience" : "studenti",
"args" : "",
"output" : ""
}
}
self.manual = 'student'
self.raise_exc = "jupman-raise"
self.strip = "jupman-strip"
self.preprocess = "jupman-preprocess"
self.purge = "jupman-purge"
self.purge_io = "jupman-purge-io"
self.purge_input = "jupman-purge-input"
self.purge_output = "jupman-purge-output"
self.raise_exc_code = "raise Exception('TODO IMPLEMENT ME !')"
""" WARNING: this string can end end up in a .ipynb json, so it must be a valid JSON string ! Be careful with the double quotes and \n !!
"""
self.solution_tags = [self.raise_exc, self.strip]
""" Code cells containing these tags are considered solutions
@since 3.3
"""
self.span_tags = [self.raise_exc, self.strip, self.purge]
""" Tags which enclose a span of text
@since 3.3
"""
self.directive_tags = [self.preprocess, self.purge, self.purge_input, self.purge_output, self.purge_io]
""" Code cells containing these tags are not considered a solution.
@since 3.3
"""
self.distrib_ext = ['py', 'ipynb']
""" Supported distribution extensions
"""
self.preprocessors = [JupmanPreprocessor(self)]
""" Notebook preprocessors. Default one is JupmanPreprocessor.
@since 3.2
"""
def _purge_tags(self, text):
""" Purges text according to directives, and removes all other Jupman tags. Only the tags, not their content!
WARNING: in other words, this function IS *NOT* SUFFICIENT
to completely clean exercises from solutions !!!
@since 3.3
"""
ret = text
if self.purge_input in text or self.purge_io in text:
return ''
#NOTE: span_pattern doesn't eat the blank afterwards (has lookahead)
ret = re.sub(span_pattern(self.purge), '', ret)
# so longer come first
all_tags = sorted(set(self.solution_tags + self.directive_tags), reverse=True)
for tag in all_tags:
if tag in self.span_tags:
#NOTE: start tag always eats the blank afterwards, end tag doesn't (has lookahead)
ret = re.sub(start_tag_pattern(tag), '\n', ret)
ret = re.sub(end_tag_pattern(tag), '', ret)
else:
#NOTE: single_tag_pattern doesn't eat the blank afterwards (has lookahead)
ret = re.sub(single_tag_pattern(tag), '', ret)
return ret
def is_zip_ignored(self, fname):
import pathspec
spec = pathspec.PathSpec.from_lines('gitwildmatch', self.zip_ignored)
return spec.match_file(fname)
def get_exercise_folders(self):
ret = []
for p in self.chapter_patterns:
for r in glob.glob(p):
if r not in ret:
ret.append(r)
for p in self.chapter_exclude_patterns:
for r in glob.glob(p):
if r in ret:
ret.remove(r)
return ret
def get_exam_student_folder(self, ld):
parse_date(ld)
return '%s-%s-FIRSTNAME-LASTNAME-ID' % (self.filename,ld)
def is_code_sol(self, solution_text):
""" Returns True if a cell contains any elements to be stripped in a solution
"""
return self.sol_to_ex_code(solution_text, parse_directives=False).strip() != solution_text.strip()
def is_to_strip(self, solution_text):
""" Returns True if a cell contains any elements to strip
@since 3.3
"""
return self.sol_to_ex_code(solution_text, parse_directives=True).strip() != solution_text.strip()
def sol_to_ex_code(self, solution_text, filepath=None, parse_directives=True):
ret = solution_text
if parse_directives:
if self.purge_input in solution_text or self.purge_io in solution_text:
return ''
ret = re.sub(span_pattern(self.purge), '', ret)
for tag in sorted(self.directive_tags, reverse=True):
ret = re.sub(single_tag_pattern(tag), '', ret)
if re.match(self.solution, ret.strip()):
return ''
ret = re.sub(span_pattern(self.raise_exc),
self.raise_exc_code,
ret)
ret = re.sub(span_pattern(self.strip), '', ret)
ret = re.sub(self.write_solution_here, r'\1\2\n\n', ret)
if filepath:
ret = replace_py_rel(ret, filepath)
return ret
def validate_tags(self, fname):
""" Validates jupman tags in file fname and return the number of solution tags found.
"""
ret = 0
if fname.endswith('.ipynb'):
import nbformat
nb_node = nbformat.read(fname, nbformat.NO_CONVERT)
for cell in nb_node.cells:
if cell.cell_type == "code":
ret += self.validate_code_tags(cell.source, fname)
elif cell.cell_type == "markdown":
ret += self.validate_markdown_tags(cell.source, fname)
elif fname.endswith('.py'):
with open(fname) as f:
ret += self.validate_code_tags(f.read(), f)
else:
raise ValueError('File format not supported for %s' % fname)
return ret
def validate_code_tags(self, text, fname):
""" Validates text which was read from file fname:
- raises ValueError on mismatched tags
- returns the number of solution tags found
"""
tag_starts = {}
tag_ends = {}
ret = 0
for tag in self.span_tags:
tag_starts[tag] = len(re.compile(tag_start(tag) + r'\s').findall(text))
tag_ends[tag] = len(re.compile(end_tag_pattern(tag)).findall(text))
if tag in self.solution_tags:
ret += tag_starts[tag]
for tag in tag_starts:
if tag not in tag_ends or tag_starts[tag] != tag_ends[tag] :
raise ValueError("Missing final tag %s in %s" % (tag_end(tag), fname) )
for tag in tag_ends:
if tag not in tag_starts or tag_starts[tag] != tag_ends[tag] :
raise ValueError("Missing initial tag %s in %s" % (tag_start(tag), fname) )
ret += len(re.compile(self.write_solution_here).findall(text))
ret += len(re.compile(self.solution).findall(text))
return ret
def validate_markdown_tags(self, text, fname):
return len(re.compile(self.markdown_answer).findall(text))
def _copy_test(self, source_abs_fn, source_fn, dest_fn):
with open(source_abs_fn, encoding='utf-8') as source_f:
data= multi_replace(source_f.read(), {
r'from\s+(.+)_sol\s+import\s+(.*)' : r'from \1 import \2',
r'import\s+(.+)_sol((\s*)|,)':r'import \1\2',
})
data = replace_py_rel(data, source_abs_fn)
info(' Writing (patched) %s' % dest_fn)
with open(dest_fn, 'w', encoding='utf-8') as dest_f:
dest_f.write(data)
def _copy_other(self, source_abs_fn, source_fn, dest_fn, new_root = ''):
if source_abs_fn.endswith('.py') :
with open(source_abs_fn, encoding='utf-8') as source_f:
data = source_f.read()
data = replace_py_rel(data, source_abs_fn)
info(' Writing (patched) %s' % dest_fn)
with open(dest_fn, 'w', encoding='utf-8') as dest_f:
dest_f.write(data)
elif source_abs_fn.endswith('.ipynb') :
import nbformat
# note: for weird reasons nbformat does not like the sol_source_f
nb_node = nbformat.read(source_abs_fn, nbformat.NO_CONVERT)
replace_ipynb_rel(nb_node, source_abs_fn)
info(' Writing (patched) %s' % dest_fn)
nbformat.write(nb_node, dest_fn)
else:
info(" Writing %s " % dest_fn)
shutil.copy(source_abs_fn, dest_fn)
def _copy_sols(self, source_fn, source_abs_fn, dest_fn):
if source_fn.endswith('.py'):
with open(source_abs_fn) as sol_source_f:
text = sol_source_f.read()
found_total_purge = self.purge_input in text or self.purge_output in text or self.purge_io in text or ''
if found_total_purge:
raise ValueError("Found %s in python file %s, but it is only allowed in notebooks!" % (found_total_purge, source_fn))
text = replace_py_rel(text, source_abs_fn)
text = self._purge_tags(text)
with open(dest_fn, 'w') as solution_dest_f:
info(" Writing (patched) %s " % dest_fn)
solution_dest_f.write(text)
elif source_fn.endswith('.ipynb'):
# py cells: strip jupman tags, fix rel urls
import nbformat
# note: for weird reasons nbformat does not like the sol_source_f
nb_node = nbformat.read(source_abs_fn, nbformat.NO_CONVERT)
replace_ipynb_rel(nb_node, source_abs_fn)
for cell in nb_node.cells:
if cell.cell_type == "code":
if self.purge_output in cell.source or self.purge_io in cell.source:
cell.outputs = []
if (self.purge_input in cell.source and self.purge_output in cell.source) \
or self.purge_io in cell.source:
cell.metadata['nbsphinx'] = 'hidden'
cell.source = self._purge_tags(cell.source)
nbformat.write(nb_node, dest_fn)
else: # solution format not supported
info("Writing %s" % source_fn)
shutil.copy(source_abs_fn, dest_fn)
def _sol_nb_to_ex(self, nb, source_abs_fn, website=False ):
""" Takes a solution notebook object and modifies it to strip solutions
@since 3.2
"""
from nbformat.v4 import new_raw_cell
def before_cell(n, cell_type):
if cell_type == 'code':
show = self.ipynb_show_solution
hide = self.ipynb_hide_solution
sol_class = 'jupman-sol-code'
elif cell_type == 'markdown':
show = self.ipynb_show_answer
hide = self.ipynb_hide_answer
sol_class = 'jupman-sol-question'
else:
warn("NO LABEL FOUND FOR cell_type %s, using default ones!" % cell_type)
show = self.ipynb_show_solution
hide = self.ipynb_hide_solution
sol_class = 'jupman-sol-code'
s = """<a class="jupman-sol jupman-sol-toggler" """
s += """\n onclick="jupman.toggleSolution(this);" """
s += """\n data-jupman-show="%s" data-jupman-hide="%s">%s</a>""" % (show, hide, show)
s += """<div class="jupman-sol %s" style="display:none"> """ % sol_class
ret = new_raw_cell()
ret.metadata.format = "text/html"
ret.source = s
return ret
def after_cell():
ret = new_raw_cell()
ret.metadata.format = "text/html"
ret.source = """</div>"""
return ret
import copy
replace_ipynb_rel(nb, source_abs_fn, website)
if not website:
if FileKinds.detect(source_abs_fn) == FileKinds.CHALLENGE_SOLUTION:
_replace_title(nb,
source_abs_fn,
r"# \2",
title_pat=r'(.*?)\s+(%s)' % self.ipynb_solutions)
else:
_replace_title(nb,
source_abs_fn,
r"# \2 %s" % self.ipynb_exercises)
# look for tags
sh_cells = nb.cells[:]
nb.cells = []
cell_counter = 0
for cell in sh_cells:
stripped_cell = copy.deepcopy(cell)
if "id" in cell:
stripped_cell["id"] = make_stripped_cell_id(cell["id"])
if cell.cell_type == "code":
if self.is_to_strip(cell.source):
if self.purge_output in cell.source or self.purge_io in cell.source:
stripped_cell.outputs = []
if (self.purge_input in cell.source and self.purge_output in cell.source) \
or self.purge_io in cell.source:
stripped_cell.metadata['nbsphinx'] = 'hidden'
stripped_cell.source = self.sol_to_ex_code(cell.source,source_abs_fn)
if website:
if self.purge_input in cell.source or self.purge_io in cell.source:
#weird stuff: https://github.com/jupyter/nbconvert/blob/42cfece9ed07232c3c440ad0768b6a76f667fe47/nbconvert/preprocessors/tagremove.py#L98
#NOTE: this MUST be ONLY for website as transient is not even an nbformat valid field !
if getattr(stripped_cell, 'transient', None):
stripped_cell.transient['remove_source'] = True
else:
stripped_cell.transient = {
'remove_source': True
}
if self.is_code_sol(cell.source) \
and not (self.purge_input in cell.source or self.purge_io in cell.source):
nb.cells.append(before_cell(cell_counter, cell.cell_type))
cell.source = self._purge_tags(cell.source)
nb.cells.append(cell)
nb.cells.append(after_cell())
nb.cells.append(stripped_cell)
else:
nb.cells.append(cell)