Skip to content

Commit fc16728

Browse files
author
Alan Christie
committed
- Adds a pick_smi() to file_utils
1 parent 8c916a7 commit fc16728

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/python/pipelines_utils/file_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,36 @@ def pick_csv(filename, directory=None):
114114
return file_path + '.csv'
115115
# Couldn't find a suitable CSV file
116116
return None
117+
118+
119+
def pick_smi(filename, directory=None):
120+
"""Returns a full path to the chosen SMI file. The supplied file
121+
is not expected to contain a recognised SMI extension, this is added
122+
automatically.
123+
If a file with the extension `.smi.gz` or `.smi` is found the path to it
124+
(excluding the extension) is returned. If this fails, `None` is returned.
125+
126+
:param filename: The SMI file basename, whose path is required.
127+
:type filename: ``str``
128+
:param directory: An optional directory.
129+
If not provided it is calculated automatically.
130+
:type directory: ``str``
131+
:return: The full path to the file without extension,
132+
or None if it does not exist
133+
:rtype: ``str``
134+
"""
135+
if directory is None:
136+
directory = utils.get_undecorated_calling_module()
137+
# If the 'cwd' is not '/output' (which indicates we're in a Container)
138+
# then remove the CWD and the anticipated '/'
139+
# from the front of the module
140+
if os.getcwd() not in ['/output']:
141+
directory = directory[len(os.getcwd()) + 1:]
142+
143+
file_path = os.path.join(directory, filename)
144+
if os.path.isfile(file_path + '.smi.gz'):
145+
return file_path + '.smi.gz'
146+
elif os.path.isfile(file_path + '.smi'):
147+
return file_path + '.smi'
148+
# Couldn't find a suitable SMI file
149+
return None

0 commit comments

Comments
 (0)