generalConfig, config: use pathlib
- switch to pathlib - represent multiple confdirs as list of Paths internally, not string with pathsep Change-Id: I1418e561641e27cd904af0762be056cd66ee1919 Reviewed-on: https://forge.frm2.tum.de/review/c/secop/frappy/+/34464 Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de> Tested-by: Jenkins Automated Tests <pedersen+jenkins@frm2.tum.de> Reviewed-by: Alexander Zaft <a.zaft@fz-juelich.de> Reviewed-by: Markus Zolliker <markus.zolliker@psi.ch>
This commit is contained in:
@ -20,6 +20,7 @@
|
||||
# *****************************************************************************
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from collections import Counter
|
||||
|
||||
@ -128,8 +129,7 @@ class Config(dict):
|
||||
|
||||
|
||||
def process_file(filename, log):
|
||||
with open(filename, 'rb') as f:
|
||||
config_text = f.read()
|
||||
config_text = filename.read_bytes()
|
||||
node = NodeCollector()
|
||||
mods = Collector(Mod)
|
||||
ns = {'Node': node.add, 'Mod': mods.add, 'Param': Param, 'Command': Param, 'Group': Group}
|
||||
@ -149,22 +149,21 @@ def process_file(filename, log):
|
||||
def to_config_path(cfgfile, log):
|
||||
candidates = [cfgfile + e for e in ['_cfg.py', '.py', '']]
|
||||
if os.sep in cfgfile: # specified as full path
|
||||
filename = cfgfile if os.path.exists(cfgfile) else None
|
||||
file = Path(cfgfile) if Path(cfgfile).exists() else None
|
||||
else:
|
||||
for filename in [os.path.join(d, candidate)
|
||||
for d in generalConfig.confdir.split(os.pathsep)
|
||||
for candidate in candidates]:
|
||||
if os.path.exists(filename):
|
||||
for file in [Path(d) / candidate
|
||||
for d in generalConfig.confdir
|
||||
for candidate in candidates]:
|
||||
if file.exists():
|
||||
break
|
||||
else:
|
||||
filename = None
|
||||
|
||||
if filename is None:
|
||||
file = None
|
||||
if file is None:
|
||||
raise ConfigError(f"Couldn't find cfg file {cfgfile!r} in {generalConfig.confdir}")
|
||||
if not filename.endswith('_cfg.py'):
|
||||
log.warning("Config files should end in '_cfg.py': %s", os.path.basename(filename))
|
||||
log.debug('Using config file %s for %s', filename, cfgfile)
|
||||
return filename
|
||||
if not file.name.endswith('_cfg.py'):
|
||||
log.warning("Config files should end in '_cfg.py': %s", file.name)
|
||||
log.debug('Using config file %s for %s', file, cfgfile)
|
||||
return file
|
||||
|
||||
|
||||
def load_config(cfgfiles, log):
|
||||
|
Reference in New Issue
Block a user