Files
calcurves/inventory.py
T
2026-05-07 16:51:51 +02:00

151 lines
4.5 KiB
Python

import sys
import time
import re
from pathlib import Path
from glob import glob
from frappy_psi.calcurve import CalCurve
SPECIAL_EXT = {'.md', '.txt', '.py'}
lookup = '~/git', '~', '/sq_sw/linse'
sea_pat = re.compile(r'\s*(lsc_sensor|mom_sensor|stick_sensors)\s+(\S+)\s+(\S+)\s+(?:.*-(sensor|curve)\s)?(\S+)?')
frappy_pat = re.compile(r'''[^#]*calcurve\s*=\s*('[^']+'|"[^"]+")''')
def find_dir(dirpath):
for base in lookup:
result = (Path(base) / dirpath).expanduser()
if result.is_dir():
return result
raise ValueError(f'{dirpath} not found')
sea_dir = find_dir('sea/tcl')
cal_dir = find_dir('calcurves')
frappy_cfgs = []
for dirs in 'frappy/cfg', 'frappycfg':
try:
frappy_cfgs.append(find_dir(dirs))
except ValueError:
pass
SPECIAL = {'raw', 'manual', 'vacuum', 'code', 'clone', 'undefined', '-sensorname'}
STD = set(Path(p).name.replace('.std', '') for p in glob(str(cal_dir / '*.std')))
FAKECRV = STD | SPECIAL
OPT = {'lsc_sensor': ' -sensor ', 'mom_sensor': '-curve '}
moms = {}
class Collector:
def __init__(self):
self.result = {}
def lsc_sensor(self, file, _1, _2, kwd, calcurve):
if calcurve not in SPECIAL and kwd in (None, 'sensor'):
self.result.setdefault(calcurve, set()).add(file)
def mom_sensor(self, file, _1, _2, kwd, calcurve):
if calcurve not in FAKECRV and kwd == 'curve':
self.result.setdefault(calcurve, set()).add(file)
def stick_sensors(self, file, calcurve1, calcurve2, _3, _4):
for calcurve in (calcurve1, calcurve2):
if calcurve.startswith('code=') or calcurve in SPECIAL:
continue
self.result.setdefault(calcurve, set()).add(Path(file).name)
def collect_sea(self, pattern):
"""treat lines starting with lsc_sensor, mom_sensor and stick_sensors"""
for file in glob(str(sea_dir / pattern)):
with open(file) as f:
for line in f:
match = sea_pat.match(line)
if match:
getattr(self, match.group(1), None)(file, *match.groups()[1:])
def collect_frappy(self, pattern):
"""treat lines containing 'calcurve = "<sensor>"
also single quote is allowed, and no hash before
"""
for frappy_cfg in frappy_cfgs:
for file in glob(str(frappy_cfg / pattern)):
with open(file) as f:
for line in f:
match = frappy_pat.match(line)
if match:
calcurve = match.group(1)[1:-1]
self.result.setdefault(calcurve, set()).add(file)
col = Collector()
col.collect_sea('*.config')
col.collect_sea('*.stick')
col.collect_sea('*.addon')
col.collect_frappy('*.py')
col.collect_frappy('*/*.py')
if len(sys.argv) > 1:
single_file = sys.argv[1]
all_calibs = {Path(single_file).stem.lower(): cal_dir / single_file}
else:
single_file = None
all_calibs = {Path(f).stem.lower(): f for f in glob(str(cal_dir / '*.*'))}
print(all_calibs)
unused = dict(all_calibs)
found = {}
notfound = {}
for calib, files in col.result.items():
file = all_calibs.get(calib.lower())
if file:
unused.pop(calib.lower(), None)
found[calib] = (file, files)
else:
notfound[calib] = files
print('\n--- found:')
for calib, (file, files) in found.items():
if calib in STD:
continue
try:
CalCurve(calib)
head = f'{Path(file).name}:'
except Exception as e:
head = f'{Path(file).name} {e!r}:'
filenames = set(Path(p).name for p in files)
print(head, ' '.join(filenames))
if notfound and not single_file:
print('\n--- not found:')
for calib, files in notfound.items():
print(f'{calib}:', ' '.join(Path(p).name for p in files))
print('\n--- external:')
with open('external.txt') as f:
for line in f:
comment, _, calibs = line.partition(':')
reduced = list(filter(None, (unused.pop(v.lower(), None) for v in calibs.split())))
if reduced:
print(f'{comment}:', ' '.join(Path(v).name for v in reduced))
print('\n--- unused:')
unused_dict = {}
for file in unused.values():
path = Path(file)
if path.suffix in SPECIAL_EXT:
continue
day = time.strftime("%Y-%m-%d", time.localtime(path.stat().st_mtime))
unused_dict.setdefault(day, []).append(path.name)
for day, names in sorted(unused_dict.items()):
print(f'{day}:', ' '.join(names))
print('\nfound', len(found), 'notfound', len(notfound), 'unused', len(unused))