2021-08-17 01:33:43 +02:00
|
|
|
import pathlib
|
|
|
|
|
|
|
|
|
|
|
|
def is_none_or_empty(item):
|
|
|
|
return item == None or (hasattr(item, '__iter__') and len(item) == 0)
|
|
|
|
|
|
|
|
|
|
|
|
def get_absolute_path(filepath):
|
|
|
|
return str(pathlib.Path(filepath).expanduser().absolute())
|
2021-08-22 00:32:23 +02:00
|
|
|
|
|
|
|
|
2021-08-23 06:00:54 +02:00
|
|
|
def resolve_absolute_path(filepath, strict=False):
|
|
|
|
return pathlib.Path(filepath).expanduser().absolute().resolve(strict=strict)
|
|
|
|
|
|
|
|
|
2021-08-22 00:32:23 +02:00
|
|
|
def get_from_dict(dictionary, *args):
|
|
|
|
'''null-aware get from a nested dictionary
|
|
|
|
Returns: dictionary[args[0]][args[1]]... or None if any keys missing'''
|
|
|
|
current = dictionary
|
|
|
|
for arg in args:
|
|
|
|
if not hasattr(current, '__iter__') or not arg in current:
|
|
|
|
return None
|
|
|
|
current = current[arg]
|
|
|
|
return current
|
|
|
|
|
|
|
|
|
|
|
|
def merge_dicts(priority_dict, default_dict):
|
|
|
|
merged_dict = priority_dict.copy()
|
|
|
|
for k, v in default_dict.items():
|
|
|
|
if k not in priority_dict:
|
|
|
|
merged_dict[k] = default_dict[k]
|
|
|
|
return merged_dict
|