From 02bd1770b0c92624938949f4ed58f15e1e39f547 Mon Sep 17 00:00:00 2001 From: Timothy Poon Date: Mon, 25 Nov 2024 15:04:22 +0000 Subject: [PATCH] Migrate to importlib in script2rst --- doc/script2rst.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/doc/script2rst.py b/doc/script2rst.py index b05148e46..325b79c01 100644 --- a/doc/script2rst.py +++ b/doc/script2rst.py @@ -1,5 +1,6 @@ import sys import io +import importlib_resources import contextlib import os @@ -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 @@ -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: @@ -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: @@ -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 @@ -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() @@ -150,9 +149,9 @@ 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) @@ -160,5 +159,5 @@ def stdoutIO(stdout=None): frst.write(' '*3+l+'\n') out = '' - +