add program for inventory
check with calc curves are used in sea and frappy configs - looks for "calcurve = '<sensor>'" in frappy configs - looks for lsc_sensor / mom?sensor and stick_sensors in sea cfg files
This commit is contained in:
117
inventory.py
Normal file
117
inventory.py
Normal file
@ -0,0 +1,117 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
from glob import glob
|
||||
from frappy_psi.calcurve import CalCurve
|
||||
|
||||
|
||||
lookup = '~/git', '~'
|
||||
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_cfg = find_dir('frappy/cfg')
|
||||
|
||||
|
||||
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, []).append(file)
|
||||
|
||||
def mom_sensor(self, file, _1, _2, kwd, calcurve):
|
||||
if calcurve not in FAKECRV and kwd == 'curve':
|
||||
self.result.setdefault(calcurve, []).append(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, []).append(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 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, []).append(file)
|
||||
|
||||
|
||||
col = Collector()
|
||||
|
||||
col.collect_sea('*.config')
|
||||
col.collect_sea('*.stick')
|
||||
col.collect_sea('*.addon')
|
||||
col.collect_frappy('*.py')
|
||||
col.collect_frappy('*/*.py')
|
||||
|
||||
all_calibs = {Path(f).stem.lower(): f for f in glob(str(cal_dir / '*.*'))}
|
||||
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}:'
|
||||
print(head, ' '.join(Path(p).name for p in files))
|
||||
|
||||
if notfound:
|
||||
print('\n--- not found:')
|
||||
for calib, files in notfound.items():
|
||||
print(f'{calib}:', ' '.join(Path(p).name for p in files))
|
||||
|
||||
print('\n--- unused:')
|
||||
print(' '.join(Path(p).name for p in unused.values()))
|
||||
|
||||
print('\nfound', len(found), 'notfound', len(notfound), 'unused', len(unused))
|
||||
|
Reference in New Issue
Block a user