|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018 Informatics Matters Ltd. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import print_function |
| 18 | +import os |
| 19 | +from . import utils |
| 20 | + |
| 21 | +# Files are normally located in sub-directories of the pipeline module path. |
| 22 | +# For example a pipeline module 'pipeline_a.py' that expects to use a file |
| 23 | +# or SDF picker would place its files in the directory |
| 24 | +# 'pipelines/demo/pipeline_a'. |
| 25 | + |
| 26 | + |
| 27 | +def pick(filename, directory=None): |
| 28 | + """Returns the named file. If directory is not specified the file is |
| 29 | + expected to be located in a sub-directory whose name matches |
| 30 | + that of the calling module otherwise the file is expected to be found in |
| 31 | + the named directory. |
| 32 | +
|
| 33 | + :param filename: The file, whose path is required. |
| 34 | + :type filename: ``str`` |
| 35 | + :param directory: An optional directory. |
| 36 | + If not provided it is calculated automatically. |
| 37 | + :type directory: ``str`` |
| 38 | + :return: The full path to the file, or None if it does not exist |
| 39 | + :rtype: ``str`` |
| 40 | + """ |
| 41 | + if directory is None: |
| 42 | + directory = utils.get_undecorated_calling_module() |
| 43 | + # Remove the CWD and the anticipated '/' from the front of the module |
| 44 | + directory = directory[len(os.getcwd()) + 1:] |
| 45 | + |
| 46 | + file_path = os.path.join(directory, filename) |
| 47 | + return file_path if os.path.isfile(file_path) else None |
| 48 | + |
| 49 | + |
| 50 | +def pick_sdf(filename, directory=None): |
| 51 | + """Returns a full path to the chosen SDF file. The supplied file |
| 52 | + is not expected to contain a recognised SDF extension, this is added |
| 53 | + automatically. |
| 54 | + If a file with the extension `.sdf.gz` or `.sdf` is found the path to it |
| 55 | + (excluding the extension) is returned. If this fails, `None` is returned. |
| 56 | +
|
| 57 | + :param filename: The SDF file basename, whose path is required. |
| 58 | + :type filename: ``str`` |
| 59 | + :param directory: An optional directory. |
| 60 | + If not provided it is calculated automatically. |
| 61 | + :type directory: ``str`` |
| 62 | + :return: The full path to the file without extension, |
| 63 | + or None if it does not exist |
| 64 | + :rtype: ``str`` |
| 65 | + """ |
| 66 | + if directory is None: |
| 67 | + directory = utils.get_undecorated_calling_module() |
| 68 | + # Remove the CWD and the anticipated '/' from the front of the module |
| 69 | + directory = directory[len(os.getcwd()) + 1:] |
| 70 | + |
| 71 | + file_path = os.path.join(directory, filename) |
| 72 | + if os.path.isfile(file_path + '.sdf.gz'): |
| 73 | + return file_path + '.sdf.gz' |
| 74 | + elif os.path.isfile(file_path + '.sdf'): |
| 75 | + return file_path + '.sdf' |
| 76 | + # Couldn't find a suitable SDF file |
| 77 | + return None |
0 commit comments