add --context (per-directory encoding config)
This commit is contained in:
parent
d2bc68885e
commit
590c8ad280
2 changed files with 78 additions and 1 deletions
73
aux.py
73
aux.py
|
@ -3,6 +3,8 @@
|
|||
|
||||
import os
|
||||
import pathlib
|
||||
import over
|
||||
prefix = over.core.text.prefix
|
||||
|
||||
# --------------------------------------------------
|
||||
|
||||
|
@ -27,3 +29,74 @@ def to_Path(raw_path):
|
|||
raw_path = os.path.expanduser(raw_path)
|
||||
|
||||
return pathlib.Path(raw_path)
|
||||
|
||||
# --------------------------------------------------
|
||||
|
||||
def _serialize(d):
|
||||
"""
|
||||
Transforms d into a string compatible with over config files.
|
||||
"""
|
||||
|
||||
if type(d) is str:
|
||||
return '"' + d.replace('"', '\\"') + '"'
|
||||
else:
|
||||
return str(d)
|
||||
|
||||
def update_cfg_context(main):
|
||||
"""
|
||||
All main.options that are sourced from either the cfg file or defaults get overridden by those in .over-video.
|
||||
Those that are from command line get saved into .over-video.
|
||||
|
||||
File format:
|
||||
name=value
|
||||
"""
|
||||
|
||||
options = {o.name: o for o in main.options}
|
||||
overrides = {}
|
||||
|
||||
try:
|
||||
with open(".over-video") as f:
|
||||
for i, line in enumerate(f):
|
||||
line = line.strip()
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
||||
try:
|
||||
name, value = line.split("=")
|
||||
except ValueError:
|
||||
main.print(".over-video §rsyntax error§/ on line §B{:d}§/: §y{:s}§/".format(i, line), prefix.fail)
|
||||
main.print("fix the error or set §B--no-§gcontext§/")
|
||||
main.exit(1)
|
||||
|
||||
try:
|
||||
option = options[name]
|
||||
|
||||
try:
|
||||
overrides[name] = over.core.app._parse(value, option.dtype)
|
||||
except:
|
||||
main.print(".over-video §rdata syntax error§/ on line §B{:d}§/: §y{:s}§/".format(i, line), prefix.fail)
|
||||
main.print("fix the error or set §B--no-§gcontext§/")
|
||||
main.exit(1)
|
||||
except KeyError:
|
||||
main.print(".over-video option §y{:s}§/ §rdoes not exist§/".format(name), prefix.warn)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
main.print("syncing context (from .over-video)", prefix.start)
|
||||
|
||||
for option in options.values():
|
||||
if option.name in overrides and option.source is not "cmdline":
|
||||
override = overrides[option.name]
|
||||
main.print("using §B--§g{:s}§/ = §m{:s}§/".format(option.name, str(override)))
|
||||
option.value = override
|
||||
elif option.source is "cmdline":
|
||||
overrides[option.name] = option.value
|
||||
main.print("storing §B--§g{:s}§/ = §m{:s}§/".format(option.name, str(option.value)))
|
||||
|
||||
with open(".over-video", "w") as f:
|
||||
for name, value in overrides.items():
|
||||
line = "{:s}={:s}\n".format(name, _serialize(value))
|
||||
f.write(line)
|
||||
|
||||
main.print("syncing context", prefix.done)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue