File tree Expand file tree Collapse file tree 1 file changed +21
-5
lines changed
Expand file tree Collapse file tree 1 file changed +21
-5
lines changed Original file line number Diff line number Diff line change 11import os
2+ import shutil
23import sys
34from argparse import ArgumentParser
45
@@ -431,6 +432,25 @@ def _nested_parse_paragraph(self, text):
431432 self .state .nested_parse (StringList (text .split ("\n " )), 0 , content )
432433 return content
433434
435+ def _open_filename (self ):
436+ # try open with given path
437+ try :
438+ return open (self .options ['filename' ])
439+ except OSError :
440+ pass
441+ # try open with abspath
442+ try :
443+ return open (os .path .abspath (self .options ['filename' ]))
444+ except OSError :
445+ pass
446+ # try open with shutil which
447+ try :
448+ return open (shutil .which (self .options ['filename' ]))
449+ except OSError :
450+ pass
451+ # raise exception
452+ raise FileNotFoundError (self .options ['filename' ])
453+
434454 def run (self ):
435455 if 'module' in self .options and 'func' in self .options :
436456 module_name = self .options ['module' ]
@@ -441,11 +461,7 @@ def run(self):
441461 attr_name = _parts [- 1 ]
442462 elif 'filename' in self .options and 'func' in self .options :
443463 mod = {}
444- try :
445- f = open (self .options ['filename' ])
446- except OSError :
447- # try open with abspath
448- f = open (os .path .abspath (self .options ['filename' ]))
464+ f = self ._open_filename ()
449465 code = compile (f .read (), self .options ['filename' ], 'exec' )
450466 exec (code , mod )
451467 attr_name = self .options ['func' ]
You can’t perform that action at this time.
0 commit comments