-
Notifications
You must be signed in to change notification settings - Fork 391
/
test_contentsmanager.py
1821 lines (1359 loc) · 55.6 KB
/
test_contentsmanager.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
# coding: utf-8
import os
import re
import time
import pytest
import itertools
import logging
import shutil
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
from tornado.web import HTTPError
from jupytext.compare import compare
import jupytext
from jupytext.cli import jupytext as jupytext_cli
from jupytext.jupytext import writes, write, read
from jupytext.compare import compare_notebooks
from jupytext.header import header_to_metadata_and_cell
from jupytext.formats import read_format_from_metadata, auto_ext_from_metadata
from jupytext.kernels import kernelspec_from_language
from .utils import (
list_notebooks,
requires_sphinx_gallery,
requires_pandoc,
skip_if_dict_is_not_ordered,
)
def test_create_contentsmanager():
jupytext.TextFileContentsManager()
def test_rename(tmpdir):
org_file = str(tmpdir.join("notebook.ipynb"))
new_file = str(tmpdir.join("new.ipynb"))
jupytext.write(new_notebook(), org_file)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.rename_file("notebook.ipynb", "new.ipynb")
assert os.path.isfile(new_file)
assert not os.path.isfile(org_file)
def test_rename_inconsistent_path(tmpdir):
org_file = str(tmpdir.join("notebook_suffix.ipynb"))
new_file = str(tmpdir.join("new.ipynb"))
jupytext.write(
new_notebook(metadata={"jupytext": {"formats": "_suffix.ipynb"}}), org_file
)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# Read notebook, and learn about its format
cm.get("notebook_suffix.ipynb")
with pytest.raises(HTTPError):
cm.rename_file("notebook_suffix.ipynb", "new.ipynb")
assert not os.path.isfile(new_file)
assert os.path.isfile(org_file)
def test_pair_unpair_notebook(tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_md = "notebook.md"
nb = new_notebook(
metadata={
"kernelspec": {
"display_name": "Python3",
"language": "python",
"name": "python3",
}
},
cells=[
new_code_cell(
"1 + 1",
outputs=[
{
"data": {"text/plain": ["2"]},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result",
}
],
)
],
)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
assert not os.path.isfile(str(tmpdir.join(tmp_md)))
# pair notebook
nb["metadata"]["jupytext"] = {"formats": "ipynb,md"}
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
assert os.path.isfile(str(tmpdir.join(tmp_md)))
# reload and get outputs
nb2 = cm.get(tmp_md)["content"]
compare_notebooks(nb, nb2)
# unpair and save as md
del nb["metadata"]["jupytext"]
cm.save(model=dict(type="notebook", content=nb), path=tmp_md)
nb2 = cm.get(tmp_md)["content"]
# we get no outputs here
compare_notebooks(nb, nb2, compare_outputs=False)
assert len(nb2.cells[0]["outputs"]) == 0
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="66"))
def test_load_save_rename(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_rmd = "notebook.Rmd"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,Rmd"
cm.root_dir = str(tmpdir)
# open ipynb, save Rmd, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_rmd)
nb_rmd = cm.get(tmp_rmd)
compare_notebooks(nb_rmd["content"], nb, "Rmd")
# save ipynb
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# rename ipynb
cm.rename(tmp_ipynb, "new.ipynb")
assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
assert os.path.isfile(str(tmpdir.join("new.ipynb")))
assert os.path.isfile(str(tmpdir.join("new.Rmd")))
# delete one file, test that we can still read and rename it
cm.delete("new.Rmd")
assert not os.path.isfile(str(tmpdir.join("new.Rmd")))
model = cm.get("new.ipynb", content=False)
assert "last_modified" in model
cm.save(model=dict(type="notebook", content=nb), path="new.ipynb")
assert os.path.isfile(str(tmpdir.join("new.Rmd")))
cm.delete("new.Rmd")
cm.rename("new.ipynb", tmp_ipynb)
assert os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert not os.path.isfile(str(tmpdir.join(tmp_rmd)))
assert not os.path.isfile(str(tmpdir.join("new.ipynb")))
assert not os.path.isfile(str(tmpdir.join("new.Rmd")))
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="magic"))
def test_save_load_paired_md_notebook(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_md = "notebook.md"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# open ipynb, save with cm, reopen
nb = jupytext.read(nb_file)
nb.metadata["jupytext"] = {"formats": "ipynb,md"}
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
nb_md = cm.get(tmp_md)
compare_notebooks(nb_md["content"], nb, "md")
assert nb_md["content"].metadata["jupytext"]["formats"] == "ipynb,md"
@requires_pandoc
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize(
"nb_file",
list_notebooks("ipynb", skip="(functional|Notebook with|flavors|invalid|305)"),
)
def test_save_load_paired_md_pandoc_notebook(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_md = "notebook.md"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# open ipynb, save with cm, reopen
nb = jupytext.read(nb_file)
nb.metadata["jupytext"] = {"formats": "ipynb,md:pandoc"}
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
nb_md = cm.get(tmp_md)
compare_notebooks(nb_md["content"], nb, "md:pandoc")
assert nb_md["content"].metadata["jupytext"]["formats"] == "ipynb,md:pandoc"
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("py_file", list_notebooks("percent"))
def test_pair_plain_script(py_file, tmpdir, caplog):
tmp_py = "notebook.py"
tmp_ipynb = "notebook.ipynb"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# open py file, pair, save with cm
nb = jupytext.read(py_file)
nb.metadata["jupytext"]["formats"] = "ipynb,py:hydrogen"
cm.save(model=dict(type="notebook", content=nb), path=tmp_py)
assert "'Include Metadata' is off" in caplog.text
assert os.path.isfile(str(tmpdir.join(tmp_py)))
assert os.path.isfile(str(tmpdir.join(tmp_ipynb)))
# Make sure we've not changed the script
with open(py_file) as fp:
script = fp.read()
with open(str(tmpdir.join(tmp_py))) as fp:
script2 = fp.read()
compare(script2, script)
# reopen py file with the cm
nb2 = cm.get(tmp_py)["content"]
compare_notebooks(nb2, nb)
assert nb2.metadata["jupytext"]["formats"] == "ipynb,py:hydrogen"
# remove the pairing and save
del nb.metadata["jupytext"]["formats"]
cm.save(model=dict(type="notebook", content=nb), path=tmp_py)
# reopen py file with the cm
nb2 = cm.get(tmp_py)["content"]
compare_notebooks(nb2, nb)
assert "formats" not in nb2.metadata["jupytext"]
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_load_save_rename_nbpy(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_nbpy = "notebook.nb.py"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,.nb.py"
cm.root_dir = str(tmpdir)
# open ipynb, save nb.py, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
nbpy = cm.get(tmp_nbpy)
compare_notebooks(nbpy["content"], nb)
# save ipynb
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# rename nbpy
cm.rename(tmp_nbpy, "new.nb.py")
assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert not os.path.isfile(str(tmpdir.join(tmp_nbpy)))
assert os.path.isfile(str(tmpdir.join("new.ipynb")))
assert os.path.isfile(str(tmpdir.join("new.nb.py")))
# rename to a non-matching pattern
with pytest.raises(HTTPError):
cm.rename_file(tmp_nbpy, "suffix_missing.py")
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("script", list_notebooks("python", skip="light"))
def test_load_save_py_freeze_metadata(script, tmpdir):
tmp_nbpy = "notebook.py"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# read original file
with open(script) as fp:
text_py = fp.read()
# write to tmp_nbpy
with open(str(tmpdir.join(tmp_nbpy)), "w") as fp:
fp.write(text_py)
# open and save notebook
nb = cm.get(tmp_nbpy)["content"]
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
with open(str(tmpdir.join(tmp_nbpy))) as fp:
text_py2 = fp.read()
compare(text_py2, text_py)
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_load_save_rename_notebook_with_dot(nb_file, tmpdir):
tmp_ipynb = "1.notebook.ipynb"
tmp_nbpy = "1.notebook.py"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,py"
cm.root_dir = str(tmpdir)
# open ipynb, save nb.py, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
nbpy = cm.get(tmp_nbpy)
compare_notebooks(nbpy["content"], nb)
# save ipynb
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# rename py
cm.rename(tmp_nbpy, "2.new_notebook.py")
assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert not os.path.isfile(str(tmpdir.join(tmp_nbpy)))
assert os.path.isfile(str(tmpdir.join("2.new_notebook.ipynb")))
assert os.path.isfile(str(tmpdir.join("2.new_notebook.py")))
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_load_save_rename_nbpy_default_config(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_nbpy = "notebook.nb.py"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,.nb.py"
cm.root_dir = str(tmpdir)
# open ipynb, save nb.py, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
nbpy = cm.get(tmp_nbpy)
compare_notebooks(nbpy["content"], nb)
# open ipynb
nbipynb = cm.get(tmp_ipynb)
compare_notebooks(nbipynb["content"], nb)
# save ipynb
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# rename notebook.nb.py to new.nb.py
cm.rename(tmp_nbpy, "new.nb.py")
assert not os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert not os.path.isfile(str(tmpdir.join(tmp_nbpy)))
assert os.path.isfile(str(tmpdir.join("new.ipynb")))
assert os.path.isfile(str(tmpdir.join("new.nb.py")))
# rename new.ipynb to notebook.ipynb
cm.rename("new.ipynb", tmp_ipynb)
assert os.path.isfile(str(tmpdir.join(tmp_ipynb)))
assert os.path.isfile(str(tmpdir.join(tmp_nbpy)))
assert not os.path.isfile(str(tmpdir.join("new.ipynb")))
assert not os.path.isfile(str(tmpdir.join("new.nb.py")))
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_load_save_rename_non_ascii_path(nb_file, tmpdir):
tmp_ipynb = u"notebôk.ipynb"
tmp_nbpy = u"notebôk.nb.py"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,.nb.py"
tmpdir = u"" + str(tmpdir)
cm.root_dir = tmpdir
# open ipynb, save nb.py, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
nbpy = cm.get(tmp_nbpy)
compare_notebooks(nbpy["content"], nb)
# open ipynb
nbipynb = cm.get(tmp_ipynb)
compare_notebooks(nbipynb["content"], nb)
# save ipynb
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# rename notebôk.nb.py to nêw.nb.py
cm.rename(tmp_nbpy, u"nêw.nb.py")
assert not os.path.isfile(os.path.join(tmpdir, tmp_ipynb))
assert not os.path.isfile(os.path.join(tmpdir, tmp_nbpy))
assert os.path.isfile(os.path.join(tmpdir, u"nêw.ipynb"))
assert os.path.isfile(os.path.join(tmpdir, u"nêw.nb.py"))
# rename nêw.ipynb to notebôk.ipynb
cm.rename(u"nêw.ipynb", tmp_ipynb)
assert os.path.isfile(os.path.join(tmpdir, tmp_ipynb))
assert os.path.isfile(os.path.join(tmpdir, tmp_nbpy))
assert not os.path.isfile(os.path.join(tmpdir, u"nêw.ipynb"))
assert not os.path.isfile(os.path.join(tmpdir, u"nêw.nb.py"))
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1])
def test_outdated_text_notebook(nb_file, tmpdir):
# 1. write py ipynb
tmp_ipynb = u"notebook.ipynb"
tmp_nbpy = u"notebook.py"
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "py,ipynb"
cm.outdated_text_notebook_margin = 0
cm.root_dir = str(tmpdir)
# open ipynb, save py, reopen
nb = jupytext.read(nb_file)
cm.save(model=dict(type="notebook", content=nb), path=tmp_nbpy)
model_py = cm.get(tmp_nbpy, load_alternative_format=False)
model_ipynb = cm.get(tmp_ipynb, load_alternative_format=False)
# 2. check that time of ipynb <= py
assert model_ipynb["last_modified"] <= model_py["last_modified"]
# 3. wait some time
time.sleep(0.5)
# 4. touch ipynb
with open(str(tmpdir.join(tmp_ipynb)), "a"):
os.utime(str(tmpdir.join(tmp_ipynb)), None)
# 5. test error
with pytest.raises(HTTPError):
cm.get(tmp_nbpy)
# 6. test OK with
cm.outdated_text_notebook_margin = 1.0
cm.get(tmp_nbpy)
# 7. test OK with
cm.outdated_text_notebook_margin = float("inf")
cm.get(tmp_nbpy)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1])
def test_reload_notebook_after_jupytext_cli(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
cm = jupytext.TextFileContentsManager()
cm.outdated_text_notebook_margin = 0
cm.root_dir = str(tmpdir)
# write the paired notebook
nb = jupytext.read(nb_file)
nb.metadata.setdefault("jupytext", {})["formats"] = "py,ipynb"
cm.save(model=dict(type="notebook", content=nb), path="notebook.py")
assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_nbpy)
# run jupytext CLI
jupytext_cli([tmp_nbpy, "--to", "ipynb", "--update"])
# test reload
nb1 = cm.get("notebook.py")["content"]
nb2 = cm.get("notebook.ipynb")["content"]
compare_notebooks(nb, nb1)
compare_notebooks(nb, nb2)
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("percent"))
def test_load_save_percent_format(nb_file, tmpdir):
tmp_py = "notebook.py"
with open(nb_file) as stream:
text_py = stream.read()
with open(str(tmpdir.join(tmp_py)), "w") as stream:
stream.write(text_py)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# open python, save
nb = cm.get(tmp_py)["content"]
del nb.metadata["jupytext"]["notebook_metadata_filter"]
cm.save(model=dict(type="notebook", content=nb), path=tmp_py)
# compare the new file with original one
with open(str(tmpdir.join(tmp_py))) as stream:
text_py2 = stream.read()
# do we find 'percent' in the header?
header = text_py2[: -len(text_py)]
assert any(["percent" in line for line in header.splitlines()])
# Remove the YAML header
text_py2 = text_py2[-len(text_py) :]
compare(text_py2, text_py)
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_julia"))
def test_save_to_percent_format(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_jl = "notebook.jl"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.preferred_jupytext_formats_save = "jl:percent"
nb = jupytext.read(nb_file)
nb["metadata"]["jupytext"] = {"formats": "ipynb,jl"}
# save to ipynb and jl
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# read jl file
with open(str(tmpdir.join(tmp_jl))) as stream:
text_jl = stream.read()
# Parse the YAML header
metadata, _, _, _ = header_to_metadata_and_cell(text_jl.splitlines(), "#")
assert metadata["jupytext"]["formats"] == "ipynb,jl:percent"
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_save_using_preferred_and_default_format_170(nb_file, tmpdir):
nb = read(nb_file)
# Way 0: preferred_jupytext_formats_save, no prefix + default_jupytext_formats
tmp_py = str(tmpdir.join("python/notebook.py"))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.preferred_jupytext_formats_save = "py:percent"
cm.default_jupytext_formats = "ipynb,python//py"
# save to ipynb and py
cm.save(model=dict(type="notebook", content=nb), path="notebook.ipynb")
# read py file
nb_py = read(tmp_py)
assert nb_py.metadata["jupytext"]["text_representation"]["format_name"] == "percent"
# Way 1: preferred_jupytext_formats_save + default_jupytext_formats
tmp_py = str(tmpdir.join("python/notebook.py"))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.preferred_jupytext_formats_save = "python//py:percent"
cm.default_jupytext_formats = "ipynb,python//py"
# save to ipynb and py
cm.save(model=dict(type="notebook", content=nb), path="notebook.ipynb")
# read py file
nb_py = read(tmp_py)
assert nb_py.metadata["jupytext"]["text_representation"]["format_name"] == "percent"
# Way 2: default_jupytext_formats
tmp_py = str(tmpdir.join("python/notebook.py"))
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.default_jupytext_formats = "ipynb,python//py:percent"
# save to ipynb and py
cm.save(model=dict(type="notebook", content=nb), path="notebook.ipynb")
# read py file
nb_py = read(tmp_py)
assert nb_py.metadata["jupytext"]["text_representation"]["format_name"] == "percent"
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_open_using_preferred_and_default_format_174(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("python/notebook.py"))
tmp_py2 = str(tmpdir.join("other/notebook.py"))
os.makedirs(str(tmpdir.join("other")))
shutil.copyfile(nb_file, tmp_ipynb)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.default_jupytext_formats = "ipynb,python//py:percent"
cm.default_notebook_metadata_filter = "all"
cm.default_cell_metadata_filter = "all"
# load notebook
model = cm.get("notebook.ipynb")
# save to ipynb and py
cm.save(model=model, path="notebook.ipynb")
assert os.path.isfile(tmp_py)
os.remove(tmp_ipynb)
# read py file
model2 = cm.get("python/notebook.py")
compare_notebooks(model2["content"], model["content"])
# move py file to the another folder
shutil.move(tmp_py, tmp_py2)
model2 = cm.get("other/notebook.py")
compare_notebooks(model2["content"], model["content"])
cm.save(model=model, path="other/notebook.py")
assert not os.path.isfile(tmp_ipynb)
assert not os.path.isfile(str(tmpdir.join("other/notebook.ipynb")))
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="many hash"))
def test_kernelspec_are_preserved(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
shutil.copyfile(nb_file, tmp_ipynb)
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
cm.default_jupytext_formats = "ipynb,py"
cm.default_notebook_metadata_filter = "-all"
# load notebook
model = cm.get("notebook.ipynb")
model["content"].metadata["kernelspec"] = {
"display_name": "Kernel name",
"language": "python",
"name": "custom",
}
# save to ipynb and py
cm.save(model=model, path="notebook.ipynb")
assert os.path.isfile(tmp_py)
# read ipynb
model2 = cm.get("notebook.ipynb")
compare_notebooks(model2["content"], model["content"])
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_save_to_light_percent_sphinx_format(nb_file, tmpdir):
tmp_ipynb = "notebook.ipynb"
tmp_lgt_py = "notebook.lgt.py"
tmp_pct_py = "notebook.pct.py"
tmp_spx_py = "notebook.spx.py"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
nb = jupytext.read(nb_file)
nb["metadata"]["jupytext"] = {
"formats": "ipynb,.pct.py:percent,.lgt.py:light,.spx.py:sphinx"
}
# save to ipynb and three python flavors
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# read files
with open(str(tmpdir.join(tmp_pct_py))) as stream:
assert read_format_from_metadata(stream.read(), ".py") == "percent"
with open(str(tmpdir.join(tmp_lgt_py))) as stream:
assert read_format_from_metadata(stream.read(), ".py") == "light"
with open(str(tmpdir.join(tmp_spx_py))) as stream:
assert read_format_from_metadata(stream.read(), ".py") == "sphinx"
model = cm.get(path=tmp_pct_py)
compare_notebooks(model["content"], nb)
model = cm.get(path=tmp_lgt_py)
compare_notebooks(model["content"], nb)
model = cm.get(path=tmp_spx_py)
# (notebooks not equal as we insert %matplotlib inline in sphinx)
model = cm.get(path=tmp_ipynb)
compare_notebooks(model["content"], nb)
@skip_if_dict_is_not_ordered
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_pair_notebook_with_dot(nb_file, tmpdir):
# Reproduce issue #138
tmp_py = "file.5.1.py"
tmp_ipynb = "file.5.1.ipynb"
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
nb = jupytext.read(nb_file)
nb["metadata"]["jupytext"] = {"formats": "ipynb,py:percent"}
# save to ipynb and three python flavors
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
assert os.path.isfile(str(tmpdir.join(tmp_ipynb)))
# read files
with open(str(tmpdir.join(tmp_py))) as stream:
assert read_format_from_metadata(stream.read(), ".py") == "percent"
model = cm.get(path=tmp_py)
assert model["name"] == "file.5.1.py"
compare_notebooks(model["content"], nb)
model = cm.get(path=tmp_ipynb)
assert model["name"] == "file.5.1.ipynb"
compare_notebooks(model["content"], nb)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1])
def test_preferred_format_allows_to_read_others_format(nb_file, tmpdir):
# 1. write py ipynb
tmp_ipynb = u"notebook.ipynb"
tmp_nbpy = u"notebook.py"
cm = jupytext.TextFileContentsManager()
cm.preferred_jupytext_formats_save = "py:light"
cm.root_dir = str(tmpdir)
# load notebook and save it using the cm
nb = jupytext.read(nb_file)
nb["metadata"]["jupytext"] = {"formats": "ipynb,py"}
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# Saving does not update the metadata, as 'save' makes a copy of the notebook
# assert nb['metadata']['jupytext']['formats'] == 'ipynb,py:light'
# Set preferred format for reading
cm.preferred_jupytext_formats_read = "py:percent"
# Read notebook
model = cm.get(tmp_nbpy)
# Check that format is explicit
assert model["content"]["metadata"]["jupytext"]["formats"] == "ipynb,py:light"
# Check contents
compare_notebooks(model["content"], nb)
# Change save format and save
model["content"]["metadata"]["jupytext"]["formats"] == "ipynb,py"
cm.preferred_jupytext_formats_save = "py:percent"
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# Read notebook
model = cm.get(tmp_nbpy)
compare_notebooks(model["content"], nb)
# Check that format is explicit
assert model["content"]["metadata"]["jupytext"]["formats"] == "ipynb,py:percent"
def test_preferred_formats_read_auto(tmpdir):
tmp_py = u"notebook.py"
with open(str(tmpdir.join(tmp_py)), "w") as script:
script.write(
"""# cell one
1 + 1
"""
)
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.preferred_jupytext_formats_read = "auto:percent"
cm.root_dir = str(tmpdir)
# load notebook
model = cm.get(tmp_py)
# check that script is opened as percent
assert (
"percent"
== model["content"]["metadata"]["jupytext"]["text_representation"][
"format_name"
]
)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb"))
def test_save_in_auto_extension_global(nb_file, tmpdir):
# load notebook
nb = jupytext.read(nb_file)
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = "notebook.ipynb"
tmp_script = "notebook" + auto_ext
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,auto"
cm.preferred_jupytext_formats_save = "auto:percent"
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == "percent"
# reload and compare with original notebook
model = cm.get(path=tmp_script)
# saving should not create a format entry #95
assert "formats" not in model["content"].metadata.get("jupytext", {})
compare_notebooks(model["content"], nb)
def test_global_auto_pairing_works_with_empty_notebook(tmpdir):
nb = new_notebook()
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
tmp_auto = str(tmpdir.join("notebook.auto"))
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,auto"
cm.preferred_jupytext_formats_save = "auto:percent"
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path="notebook.ipynb")
# check that only the ipynb representation exists
assert os.path.isfile(tmp_ipynb)
assert not os.path.isfile(tmp_py)
assert not os.path.isfile(tmp_auto)
assert "notebook.ipynb" not in cm.paired_notebooks
model = cm.get(path="notebook.ipynb")
compare_notebooks(model["content"], nb)
# add language information to the notebook
nb.metadata["language_info"] = {
"codemirror_mode": {"name": "ipython", "version": 3},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3",
}
# save again
cm.save(model=dict(type="notebook", content=nb), path="notebook.ipynb")
# check that ipynb + py representations exists
assert os.path.isfile(tmp_ipynb)
assert os.path.isfile(tmp_py)
assert not os.path.isfile(tmp_auto)
assert len(cm.paired_notebooks["notebook.ipynb"]) == 2
# add a cell in the py file
with open(tmp_py, "a") as fp:
fp.write("# %%\n2+2\n")
nb2 = cm.get(path="notebook.ipynb")["content"]
assert len(nb2.cells) == 1
assert nb2.cells[0].source == "2+2"
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb"))
def test_save_in_auto_extension_global_with_format(nb_file, tmpdir):
# load notebook
nb = jupytext.read(nb_file)
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = "notebook.ipynb"
tmp_script = "notebook" + auto_ext
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,auto:percent"
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == "percent"
# reload and compare with original notebook
model = cm.get(path=tmp_script)
# saving should not create a format entry #95
assert "formats" not in model["content"].metadata.get("jupytext", {})
compare_notebooks(model["content"], nb)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb"))
def test_save_in_auto_extension_local(nb_file, tmpdir):
# load notebook
nb = jupytext.read(nb_file)
nb.metadata.setdefault("jupytext", {})["formats"] = "ipynb,auto:percent"
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = "notebook.ipynb"
tmp_script = "notebook" + auto_ext
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# check that text representation exists, and is in percent format
with open(str(tmpdir.join(tmp_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == "percent"
# reload and compare with original notebook
model = cm.get(path=tmp_script)
compare_notebooks(model["content"], nb)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb"))
def test_save_in_pct_and_lgt_auto_extensions(nb_file, tmpdir):
# load notebook
nb = jupytext.read(nb_file)
auto_ext = auto_ext_from_metadata(nb.metadata)
tmp_ipynb = "notebook.ipynb"
tmp_pct_script = "notebook.pct" + auto_ext
tmp_lgt_script = "notebook.lgt" + auto_ext
# create contents manager with default load format as percent
cm = jupytext.TextFileContentsManager()
cm.default_jupytext_formats = "ipynb,.pct.auto,.lgt.auto"
cm.preferred_jupytext_formats_save = ".pct.auto:percent,.lgt.auto:light"
cm.root_dir = str(tmpdir)
# save notebook
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# check that text representation exists in percent format
with open(str(tmpdir.join(tmp_pct_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == "percent"
# check that text representation exists in light format
with open(str(tmpdir.join(tmp_lgt_script))) as stream:
assert read_format_from_metadata(stream.read(), auto_ext) == "light"
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="(magic|305)"))
def test_metadata_filter_is_effective(nb_file, tmpdir):
nb = jupytext.read(nb_file)
tmp_ipynb = "notebook.ipynb"
tmp_script = "notebook.py"
# create contents manager
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmpdir)
# save notebook to tmpdir
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# set config
cm.default_jupytext_formats = "ipynb,py"
cm.default_notebook_metadata_filter = "jupytext,-all"
cm.default_cell_metadata_filter = "-all"
# load notebook
nb = cm.get(tmp_ipynb)["content"]
assert nb.metadata["jupytext"]["cell_metadata_filter"] == "-all"
assert nb.metadata["jupytext"]["notebook_metadata_filter"] == "jupytext,-all"
# save notebook again
cm.save(model=dict(type="notebook", content=nb), path=tmp_ipynb)
# read text version
nb2 = jupytext.read(str(tmpdir.join(tmp_script)))
# test no metadata
assert set(nb2.metadata.keys()) <= {"jupytext", "kernelspec"}
for cell in nb2.cells:
assert not cell.metadata