easyconf-lemonbar/easyconf_lemonbar/parse_config_file.py

62 lines
3 KiB
Python

from yaml import safe_load
from schema import And, Optional, Regex, Schema, SchemaError, Or
from signal import SIGRTMIN, SIGRTMAX
def get_bar_config_and_monitor_list(filename):
config_schema = Schema({
"bar": {
Optional("config"): {
Optional("geometry"): Regex(r'^[0-9]*x[0-9]*\+[0-9]*\+[0-9]*$'),
Optional("bottom"): bool,
Optional("force"): bool,
Optional("font"): str,
Optional("name"): str,
Optional("line_thickness"): And(int, lambda n: n > 0),
Optional("bg_color", default="#000"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$'),
Optional("fg_color", default="#FFF"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$'),
Optional("offset"): int,
Optional("line_color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$'),
Optional("padding", default=" "): str,
Optional("seperator", default="|"): str,
Optional("margin", default=""): str
},
"monitor": {
And(int, lambda n: 0 <= n <= 9): {
"modules": {
str: {
"command": str,
"refresh": And(int, lambda n : n >= 0),
Optional("prefix"): str,
Optional("signal"): And(int, lambda n : SIGRTMIN <= SIGRTMIN+n <= SIGRTMAX),
"format": {
"align": Or("left", "center", "right"),
Optional("bg_color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$'),
Optional("fg_color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$'),
Optional("font"): And(int, lambda n: 1 <= n <= 5),
Optional("offset"): int,
Optional("line"): {
"type": Or("underline", "overline"),
Optional("color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$')
}
# Not yet implemented.
#Optional("button"): {
# "activator": Or("left", "center", "right", "scrup", "scrdown"),
# "command": str
#}
}
}
}
}
}
}
})
with open(filename, "r", encoding="utf8") as file:
config_file = safe_load(file)
try:
validated_config = config_schema.validate(config_file)
return validated_config["bar"]["config"], validated_config["bar"]["monitor"]
except SchemaError as se:
raise se