|
7 | 7 |
|
8 | 8 | def get_absolute_path_of_file(file_name, dir_name=None): |
9 | 9 | """ |
10 | | - Filenames in our .json config could be absolute or relative to the |
11 | | - current working dir. This method tries to find the valid, full file path. |
| 10 | + This method tries to setup an absolute pathname. |
| 11 | + Tt doesn't actually check if the file exists. |
| 12 | + If dir_name is provided, it is used as a prefix. |
| 13 | + If dir_name is None, the function is equivalent |
| 14 | + to os.path.abspath(file_name). |
12 | 15 |
|
13 | | - :param file_name: |
14 | | - :param dir_name: prefix, for example, the dirname of our .json file. |
15 | | - :return: absolute path name of file if found, otherwise None. |
| 16 | + :param file_name: str, the file name to convert to absolute path. |
| 17 | + :param dir_name: str or None, optional prefix directory. |
| 18 | + :return: absolute path name |
| 19 | + :raises ValueError if file_name is empty or None. |
16 | 20 | """ |
17 | | - if not file_name: |
18 | | - raise ValueError("Empty file_name.") |
19 | | - file = None |
20 | | - if os.path.isabs(file_name): |
21 | | - if os.path.isfile(file_name): |
22 | | - file = file_name |
23 | | - elif dir_name is not None: |
24 | | - joined = os.path.join(dir_name, file_name) |
25 | | - if os.path.isfile(joined): |
26 | | - file = joined |
27 | | - elif os.getcwd() is not None: |
28 | | - joined = os.path.join(os.getcwd(), file_name) |
29 | | - if os.path.isfile(joined): |
30 | | - file = joined |
31 | | - return file |
| 21 | + if file_name is None: |
| 22 | + raise ValueError("file_name is None.") |
| 23 | + if not isinstance(file_name, str): |
| 24 | + raise TypeError("file_name is not a string.") |
| 25 | + if file_name.strip() == "": |
| 26 | + raise ValueError("file_name is empty.") |
| 27 | + fname = file_name.strip() |
| 28 | + if dir_name is not None: |
| 29 | + if not isinstance(dir_name, str): |
| 30 | + raise TypeError("dir_name is not a string.") |
| 31 | + if dir_name.strip() == "": |
| 32 | + raise ValueError("dir_name is empty.") |
| 33 | + dir_name = dir_name.strip() |
| 34 | + fname = os.path.join(dir_name, fname) |
| 35 | + return os.path.abspath(fname) |
0 commit comments