48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
|
|
import yaml, pathlib
|
||
|
|
|
||
|
|
def load_conf():
|
||
|
|
p = pathlib.Path('conf.yml')
|
||
|
|
if not p.exists():
|
||
|
|
return None
|
||
|
|
with open(p) as fp:
|
||
|
|
return yaml.safe_load(fp.read())
|
||
|
|
|
||
|
|
def read_file(path:pathlib.PosixPath):
|
||
|
|
if not path.exists():
|
||
|
|
print(f"File {path} does not exist...")
|
||
|
|
return None
|
||
|
|
with open(path) as fp:
|
||
|
|
return fp.read()
|
||
|
|
|
||
|
|
def save_file(path:pathlib.PosixPath, contents:str, overwrite=False, mkdirs=False):
|
||
|
|
if path.exists() and not overwrite:
|
||
|
|
print(f"File {path} already exists and overwrite is False...")
|
||
|
|
return
|
||
|
|
|
||
|
|
if not path.parent.exists() and not mkdirs:
|
||
|
|
print(f"Directory {path.parent} does not exists and mkdirs is False...")
|
||
|
|
return
|
||
|
|
|
||
|
|
if mkdirs:
|
||
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
|
||
|
|
with open(path, 'w') as fp:
|
||
|
|
fp.write(contents)
|
||
|
|
|
||
|
|
def ls_dir(path:str):
|
||
|
|
directory = pathlib.Path(path)
|
||
|
|
if not directory.exists() and not directory.is_dir():
|
||
|
|
return None
|
||
|
|
return list(directory.iterdir())
|
||
|
|
|
||
|
|
def get_files_in_subdir(path:str, subdir:str, suffix:str):
|
||
|
|
template = pathlib.Path(path)
|
||
|
|
if template.exists() and template.is_dir():
|
||
|
|
partials_path = template / subdir;
|
||
|
|
if partials_path.exists() and partials_path.is_dir():
|
||
|
|
files = list(partials_path.glob('**/*.' + suffix))
|
||
|
|
partials = [a.stem for a in files]
|
||
|
|
return dict(zip(partials, files))
|
||
|
|
return None
|
||
|
|
|