Skip to content

Commit 30fa54f

Browse files
tsutterleyashb
andauthored
feat: find executable filename from relative paths (#17)
should address #4 for finding relative paths Co-authored-by: Ash Berlin-Taylor <[email protected]>
1 parent 064f9e9 commit 30fa54f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

sphinxarg/ext.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import shutil
23
import sys
34
from 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']

0 commit comments

Comments
 (0)