Skip to content

Commit

Permalink
Migrate to importlib in script2rst
Browse files Browse the repository at this point in the history
  • Loading branch information
ptim0626 committed Nov 25, 2024
1 parent ad523b2 commit 02bd177
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions doc/script2rst.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import io
import importlib_resources
import contextlib
import os

Expand All @@ -11,11 +12,9 @@
'subclassptyscan.py']

if len(sys.argv) == 1:
import pkg_resources

for script in scripts:
scr = pkg_resources.resource_filename('ptypy', tutorial_dir+script)
if not os.path.exists(scr):
scr = imortlib_resources.files('ptypy') / (tutorial_dir+script)
if not scr.exists():
print('Using backup tutorial for %s' % script)
scr = '../tutorial/'+script
#subprocess.call(['python',sys.argv[0]+' '+scr]) # doesn't work
Expand Down Expand Up @@ -50,13 +49,13 @@ def stdoutIO(stdout=None):
frst.write("""
.. note::
This tutorial was generated from the python source
:file:`[ptypy_root]/tutorial/%(fname)s` using :file:`ptypy/doc/%(this)s`.
:file:`[ptypy_root]/tutorial/%(fname)s` using :file:`ptypy/doc/%(this)s`.
You are encouraged to modify the parameters and rerun the tutorial with::
$ python [ptypy_root]/tutorial/%(fname)s
""" % {'fname': os.path.split(script_name)[-1], 'this': sys.argv[0]})

was_comment = True

while True:
Expand Down Expand Up @@ -86,7 +85,7 @@ def stdoutIO(stdout=None):
frst.write(' '+line2[1:].strip()+'\n')
frst.write('\n')
continue

if line.startswith('"""'):
frst.write('.. parsed-literal::\n\n')
while True:
Expand All @@ -95,11 +94,11 @@ def stdoutIO(stdout=None):
break
frst.write(' ' + line2)
continue

decorator = False
indent = False
for key in indent_keys:
if line.startswith(key):
if line.startswith(key):
indent = True
break

Expand All @@ -125,12 +124,12 @@ def stdoutIO(stdout=None):
pt = fpy.tell()
exec(func+'\n')
continue

wline = line.strip()
if not wline:
frst.write('\n')
continue

with stdoutIO() as sout:
exec(wline)
out = sout.getvalue()
Expand All @@ -150,15 +149,15 @@ def stdoutIO(stdout=None):
if was_comment:
wline = '\n::\n\n'+wline
was_comment = False

frst.write(wline+'\n')

#print out
if out.strip():
print(out)
for l in out.split('\n'):
frst.write(' '*3+l+'\n')
out = ''



0 comments on commit 02bd177

Please sign in to comment.