Skip to content

Commit

Permalink
feat: find executable filename from relative paths (#17)
Browse files Browse the repository at this point in the history
should address #4 for finding relative paths

Co-authored-by: Ash Berlin-Taylor <ash_github@firemirror.com>
  • Loading branch information
tsutterley and ashb committed Nov 10, 2022
1 parent 064f9e9 commit 30fa54f
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions sphinxarg/ext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import sys
from argparse import ArgumentParser

Expand Down Expand Up @@ -431,6 +432,25 @@ def _nested_parse_paragraph(self, text):
self.state.nested_parse(StringList(text.split("\n")), 0, content)
return content

def _open_filename(self):
# try open with given path
try:
return open(self.options['filename'])
except OSError:
pass
# try open with abspath
try:
return open(os.path.abspath(self.options['filename']))
except OSError:
pass
# try open with shutil which
try:
return open(shutil.which(self.options['filename']))
except OSError:
pass
# raise exception
raise FileNotFoundError(self.options['filename'])

def run(self):
if 'module' in self.options and 'func' in self.options:
module_name = self.options['module']
Expand All @@ -441,11 +461,7 @@ def run(self):
attr_name = _parts[-1]
elif 'filename' in self.options and 'func' in self.options:
mod = {}
try:
f = open(self.options['filename'])
except OSError:
# try open with abspath
f = open(os.path.abspath(self.options['filename']))
f = self._open_filename()
code = compile(f.read(), self.options['filename'], 'exec')
exec(code, mod)
attr_name = self.options['func']
Expand Down

0 comments on commit 30fa54f

Please sign in to comment.