Initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import importlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import unicodedata
|
||||
from pathlib import Path
|
||||
|
||||
def install_module(module_name, package_name=None):
|
||||
'''Install a python module with pip or return it if already installed'''
|
||||
try:
|
||||
module = importlib.import_module(module_name)
|
||||
except ModuleNotFoundError:
|
||||
print(f'Installing Module {module_name} ....')
|
||||
|
||||
subprocess.call([sys.executable, '-m', 'ensurepip'])
|
||||
subprocess.call([sys.executable, '-m', 'pip', 'install', package_name or module_name])
|
||||
|
||||
module = importlib.import_module(module_name)
|
||||
|
||||
return module
|
||||
|
||||
def import_module_from_path(path):
|
||||
from importlib import util
|
||||
|
||||
try:
|
||||
path = Path(path)
|
||||
spec = util.spec_from_file_location(path.stem, str(path))
|
||||
mod = util.module_from_spec(spec)
|
||||
|
||||
spec.loader.exec_module(mod)
|
||||
|
||||
return mod
|
||||
|
||||
except Exception as e:
|
||||
print(f'Cannot import file {path}')
|
||||
print(e)
|
||||
|
||||
def norm_str(string, separator='_', format=str.lower, padding=0):
|
||||
string = str(string)
|
||||
string = string.replace('_', ' ')
|
||||
string = string.replace('-', ' ')
|
||||
string = re.sub('[ ]+', ' ', string)
|
||||
string = re.sub('[ ]+\/[ ]+', '/', string)
|
||||
string = string.strip()
|
||||
|
||||
if format:
|
||||
string = format(string)
|
||||
|
||||
# Padd rightest number
|
||||
string = re.sub(r'(\d+)(?!.*\d)', lambda x : x.group(1).zfill(padding), string)
|
||||
|
||||
string = string.replace(' ', separator)
|
||||
string = unicodedata.normalize('NFKD', string).encode('ASCII', 'ignore').decode("utf-8")
|
||||
|
||||
return string
|
||||
|
||||
def read_file(path):
|
||||
'''Read a file with an extension in (json, yaml, yml, txt)'''
|
||||
|
||||
exts = ('.json', '.yaml', '.yml', '.txt')
|
||||
|
||||
if not path:
|
||||
print('Try to read empty file')
|
||||
|
||||
path = Path(path)
|
||||
if not path.exists():
|
||||
print('File not exist', path)
|
||||
return
|
||||
|
||||
if path.suffix not in exts:
|
||||
print(f'Cannot read file {path}, extension must be in {exts}')
|
||||
return
|
||||
|
||||
txt = path.read_text()
|
||||
data = None
|
||||
|
||||
if path.suffix.lower() in ('.yaml', '.yml'):
|
||||
yaml = install_module('yaml')
|
||||
try:
|
||||
data = yaml.safe_load(txt)
|
||||
except Exception:
|
||||
print(f'Could not load yaml file {path}')
|
||||
return
|
||||
elif path.suffix.lower() == '.json':
|
||||
try:
|
||||
data = json.loads(txt)
|
||||
except Exception:
|
||||
print(f'Could not load json file {path}')
|
||||
return
|
||||
else:
|
||||
data = txt
|
||||
|
||||
return data
|
||||
Reference in New Issue
Block a user