89 lines
2.0 KiB
Python
89 lines
2.0 KiB
Python
import glob
|
|
import os
|
|
|
|
|
|
class Path:
|
|
def __init__(self, value):
|
|
self.__path = value
|
|
self.__namespace = None
|
|
|
|
parts = value.split(':')
|
|
|
|
if len(parts) != 2:
|
|
return
|
|
|
|
if len(parts[0]) < 3:
|
|
return
|
|
|
|
self.__namespace = parts[0]
|
|
self.__path = parts[1]
|
|
|
|
|
|
def get_path(self):
|
|
return self.__path
|
|
|
|
|
|
def remove_root(self):
|
|
while len(self.__path) > 0 and self.__path[0] == '/':
|
|
self.__path = self.__path [1:]
|
|
|
|
if len(self.__path) == 0:
|
|
self.__path= '.'
|
|
|
|
return self
|
|
|
|
|
|
def expand_path(self, **kwargs):
|
|
relative_to = None
|
|
|
|
if 'relative_to' in kwargs:
|
|
relative_to = kwargs['relative_to']
|
|
|
|
if isinstance(relative_to, dict):
|
|
if self.__namespace is not None:
|
|
relative_to = relative_to[self.__namespace]
|
|
else:
|
|
relative_to = None
|
|
|
|
paths = glob.glob(self.__path, root_dir=relative_to, recursive=True)
|
|
if len(paths) == 0 and '*' not in self.__path:
|
|
if relative_to is not None:
|
|
return [os.path.join(relative_to, self.__path)]
|
|
return [self.__path]
|
|
|
|
if relative_to is not None:
|
|
abs_paths = []
|
|
|
|
for p in paths:
|
|
abs_paths.append(os.path.join(relative_to, p))
|
|
|
|
return abs_paths
|
|
|
|
return paths
|
|
|
|
|
|
def expand_all(paths, **kwargs):
|
|
relative_to = None
|
|
|
|
if 'relative_to' in kwargs:
|
|
relative_to = kwargs['relative_to']
|
|
|
|
result = []
|
|
for p in paths:
|
|
if not isinstance(p, Path):
|
|
p = Path(p)
|
|
r = p.expand_path(relative_to=relative_to)
|
|
result.extend(r)
|
|
|
|
return result
|
|
|
|
|
|
def get_namespace(self, **kwargs):
|
|
if self.__namespace is not None:
|
|
return self.__namespace
|
|
|
|
if 'default' in kwargs:
|
|
return kwargs['default']
|
|
|
|
return None
|