ported to over-2.0

This commit is contained in:
Martinez 2016-05-18 12:59:13 +02:00
parent de2178c4f9
commit 963a6799da
3 changed files with 97 additions and 141 deletions

74
aux.py
View file

@ -4,7 +4,6 @@
import os
import pathlib
import over
prefix = over.core.text.prefix
# --------------------------------------------------
@ -32,15 +31,11 @@ def to_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)
context_file_header = """# Context file for %s-%s
# This file stores configuration valid for this directory.
# It is updated automatically with options you use.
"""
def update_cfg_context(main, ignore=[]):
"""
@ -51,53 +46,16 @@ def update_cfg_context(main, ignore=[]):
name=value
"""
options = {o.name: o for o in main.options}
overrides = {}
options_to_consider = [option for option in main.options.values() if option.name not in ignore and option.in_cfg_file]
options_to_write = {option.name: option for option in options_to_consider if option.source == over.app.Option_sources.command_line}
options_to_read = {option.name: option for option in options_to_consider if option.source != over.app.Option_sources.command_line}
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("=", 1)
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
context_file_read = over.app.ConfigFile(options_to_read, ".over-video")
restored = context_file_read.read_config()
if restored:
main.print("restored from <m>.over-video<.>: %s" %(", ".join("<g>%s<.>" %(o.name) for o in restored)), main.print.tl.note)
main.print("syncing context (from .over-video)", prefix.start)
for option in options.values():
if option.name not in ignore:
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)
context_file_write = over.app.ConfigFile(options_to_write, ".over-video")
added = context_file_write.update_config(context_file_header, (main.name, main.version))
if added:
main.print("added to <m>.over-video<.>: %s" %(", ".join("<g>%s<.>" %(o.name) for o in added)))