over-video/aux.py

102 lines
2.7 KiB
Python

#! /usr/bin/env python3
# encoding: utf-8
import os
import pathlib
import over
prefix = over.core.text.prefix
# --------------------------------------------------
def parse_fps(raw):
if '/' in raw:
num, den = (int(x) for x in raw.split('/'))
return float(num) / float(den)
return float(raw)
# --------------------------------------------------
def to_Path(raw_path):
'''
Returns pathlib.Path pointing to raw_path, handling '~/' correctly.
To be removed after python:3.5 move.
'''
if raw_path.startswith('~'):
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)