rewrite import_shots and cleanup

This commit is contained in:
ChristopheSeux
2024-04-10 16:15:57 +02:00
parent dae9d55494
commit 2960bab63d
15 changed files with 1068 additions and 269 deletions
+38 -8
View File
@@ -5,6 +5,9 @@ import platform
import sys
import unicodedata
from pathlib import Path
from os.path import expandvars
import json
import glob
def install_module(module_name, package_name=None):
@@ -109,13 +112,15 @@ def read_file(path):
yaml = install_module('yaml')
try:
data = yaml.safe_load(txt)
except Exception:
except Exception as e:
print(e)
print(f'Could not load yaml file {path}')
return
elif path.suffix.lower() == '.json':
try:
data = json.loads(txt)
except Exception:
except Exception as e:
print(e)
print(f'Could not load json file {path}')
return
else:
@@ -142,11 +147,36 @@ def open_file(filepath, env=None, select=False):
subprocess.Popen(cmd, env=env)
def parse(string, template):
template = re.sub(r'{index:(.+?)}', r'(?P<index>.+)', template)
reg = re.sub(r'{(.+?)}', r'(?P<_\1>.+)', template)
# def parse(string, template):
# template = re.sub(r'{index:(.+?)}', r'(?P<index>.+)', template)
# reg = re.sub(r'{(.+?)}', r'(?P<_\1>.+)', template)
values = list(re.search(reg, string).groups())
keys = re.findall(r'{(.+?)}', template) + ['index']
# values = list(re.search(reg, string).groups())
# keys = re.findall(r'{(.+?)}', template) + ['index']
return dict(zip(keys, values))
# return dict(zip(keys, values))
def parse(template, string):
reg = re.sub(r'{[^}]*}|\.', lambda m: m.group() if m.group().startswith('{') else r'\.', template)
reg = re.sub(r'{(.+?)}', r'(?P<\1>.*)', reg)
#print(string, template, reg)
if result := re.match(reg, string):
return result.groupdict()
def find_last(template, **kargs):
#print(find_last, template, kargs)
pattern = Path(template).as_posix()
pattern = expandvars(pattern)
for key, value in kargs.items():
pattern = re.sub(r'\{%s\}'%key, value, pattern)
pattern = re.sub(r'{\w+:(\d{2})d}', lambda x : '?' * int(x.groups()[0]), pattern)
pattern = re.sub(r'{.*?}', '*', pattern)
print(pattern)
filepaths = glob.glob(pattern)
if filepaths:
return Path(max(filepaths))