generated from FAIRmat-NFDI/nomad-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
51 lines (44 loc) · 1.84 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD.
# See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from glob import glob
def get_files(pattern: str, filepath: str, stripname: str = '', deep: bool = True):
"""Get files following the `pattern` with respect to the file `stripname` (usually this
being the mainfile of the given parser) up to / down from the `filepath` (`deep=True` going
down, `deep=False` up)
Args:
pattern (str): targeted pattern to be found
filepath (str): filepath to start the search
stripname (str, optional): name with respect to which do the search. Defaults to ''.
deep (bool, optional): boolean setting the path in the folders to scan (down or up). Defaults to down=True.
Returns:
list: List of found files.
"""
for _ in range(10):
filenames = glob(f'{os.path.dirname(filepath)}/{pattern}')
pattern = os.path.join('**' if deep else '..', pattern)
if filenames:
break
if len(filenames) > 1:
# filter files that match
suffix = os.path.basename(filepath).strip(stripname)
matches = [f for f in filenames if suffix in f]
filenames = matches if matches else filenames
filenames = [f for f in filenames if os.access(f, os.F_OK)]
return filenames