finished new parsing

This commit is contained in:
Noah Swerhun 2023-02-12 14:09:01 -06:00
parent 8bb3bdbebf
commit 8aee497225
6 changed files with 189 additions and 200 deletions

Binary file not shown.

View file

@ -1,6 +1,5 @@
from yaml import dump, safe_load from yaml import safe_load
from schema import And, Optional, Regex, Schema, SchemaError, Or from schema import And, Optional, Regex, Schema, SchemaError, Or
from json import dumps
def get_validated_config(filename): def get_validated_config(filename):
config_schema = Schema({ config_schema = Schema({
@ -21,7 +20,7 @@ def get_validated_config(filename):
}, },
"modules": [ "modules": [
{ {
str: { "name": str,
"command": str, "command": str,
"refresh": And(int, lambda n : n > 0), "refresh": And(int, lambda n : n > 0),
Optional("prefix"): str, Optional("prefix"): str,
@ -41,7 +40,6 @@ def get_validated_config(filename):
} }
} }
} }
}
] ]
} }
@ -84,7 +82,71 @@ def get_lemonbar_flags(config):
flags += (" -U '" + configuration_options[option] + "'") flags += (" -U '" + configuration_options[option] + "'")
return flags.strip() return flags.strip()
def parse_module(module):
alignment = ""
pre = ""
name = ""
prefix = ""
command = ""
refresh = 0
post =""
print(get_lemonbar_flags(get_validated_config("./testing_config.yml"))) for option in module:
match option:
case "name":
name = module[option]
case "command":
command = module[option]
case "refresh":
refresh = module[option]
case "prefix":
prefix = module[option]
format_options = module["format"]
for option in format_options:
match option:
case "align":
alignment = format_options[option]
case "offset":
pre = ("%{O" + str(format_options[option]) + "}") + pre
case "bg_color":
pre = ("%{B" + format_options[option] + "}") + pre
post += "%{B-}"
case "fg_color":
pre = ("%{F" + format_options[option] + "}") + pre
post += "%{F-}"
case "font":
pre = ("%{T" + format_options[option] + "}") + pre
post += "%{T-}"
case "line":
line_options = format_options[option]
for line_option in line_options:
match line_option:
case "type":
if line_options[line_option] == "underline":
pre = ("%{+u}") + pre
post += ("%{-u}")
elif line_options[line_option] == "overline":
pre = ("%{+o}") + pre
post += ("%{-o}")
case "color":
pre = ("%{U" + line_options[line_option] + "}") + pre
post += "%{U-}"
case "button":
button_options = format_options[option]
button = 1
match button_options["activator"]:
case "left":
button = 1
case "middle":
button = 2
case "right":
button = 3
case "scrup":
button = 4
case "scrdown":
button = 5
pre = ("%{A" + str(button)+ ":" + button_options["command"] + ":}") + pre
post += "%{A}"
return [alignment, pre, name, prefix, command, post, refresh]

View file

@ -47,7 +47,8 @@ bar:
modules: modules:
# This is an example module. You can name it whatever you want, it doesn't # This is an example module. You can name it whatever you want, it doesn't
# matter. Notice the `-` before the name. Each module must be a list item! # matter. Notice the `-` before the name. Each module must be a list item!
- example_module: # THIS IS A REQUIRED FIELD.
- name: "time"
# This command is run by `sh -c`, and whatever is sent to stdout is the # This command is run by `sh -c`, and whatever is sent to stdout is the
# text that will be displayed for this module. `date` here is used as an # text that will be displayed for this module. `date` here is used as an
# example. THIS IS A REQUIRED FIELD. # example. THIS IS A REQUIRED FIELD.
@ -90,7 +91,7 @@ bar:
command: "echo 'hello' > ~/file.txt" command: "echo 'hello' > ~/file.txt"
# Here is a simpler example module showing only the required fields. # Here is a simpler example module showing only the required fields.
- bare_minimum: - name: "bare_minimum"
command: "whoami" command: "whoami"
refresh: 1000 refresh: 1000
format: format:

View file

@ -1,33 +1,4 @@
import yaml from config_parsing import get_lemonbar_flags, get_validated_config
import io
with io.open("./testing_config.yml", 'r', encoding='utf8') as file:
config_file = yaml.safe_load(file)
lemonbar_command = "lemonbar" print(get_lemonbar_flags(get_validated_config("./testing_config.yml")))
lemonbar_cmd_options = config_file['bar']['config']
for item in lemonbar_cmd_options:
match item:
case 'geometry':
lemonbar_command += (" -g '" + lemonbar_cmd_options['geometry'] + "'")
case 'bottom':
lemonbar_command += (" -b")
case 'force':
lemonbar_command += (" -d")
case 'font':
lemonbar_command += (" -f '" + lemonbar_cmd_options['font'] + "'")
case 'name':
lemonbar_command += (" -n '" + lemonbar_cmd_options['name'] + "'")
case 'line_thickness':
lemonbar_command += (" -u " + str(lemonbar_cmd_options['line_thickness']))
case 'bg_color':
lemonbar_command += (" -B '" + lemonbar_cmd_options['bg_color'] + "'")
case 'fg_color':
lemonbar_command += (" -F '" + lemonbar_cmd_options['fg_color'] + "'")
case 'offset':
lemonbar_command += (" -o " + str(lemonbar_cmd_options['offset']))
case 'line_color':
lemonbar_command += (" -U '" + lemonbar_cmd_options['line_color'] + "'")
print(lemonbar_command)

56
main.py Normal file
View file

@ -0,0 +1,56 @@
from threading import Thread
from subprocess import run
from time import sleep
from config_parsing import get_validated_config, parse_module
running_modules_dict = {"left": {}, "center": {}, "right": {}}
def print_bar():
for alignment in running_modules_dict:
match alignment:
case "left":
print("%{l}", end="")
case "center":
print("%{c}", end="")
case "right":
print("%{r}", end="")
group = running_modules_dict[alignment]
for index, module in enumerate(group):
print(group[module], end="")
if index != (len(group) - 1):
print(seperator, end="")
print(flush=True)
def create_module_string(pre, prefix, command, post):
cmd_output = run(command, shell=True, capture_output=True, text=True).stdout.strip()
return (pre + padding + prefix + cmd_output + padding + post)
def new_module_thread(alignment, pre, name, prefix, command, post, refresh):
while True:
module_string = create_module_string(pre, prefix, command, post)
running_modules_dict[alignment][name] = module_string
print_bar()
sleep(refresh/1000)
def main():
global padding
global seperator
config = get_validated_config("./testing_config.yml")
padding = config["bar"]["config"]["padding"]
seperator = config["bar"]["config"]["seperator"]
modules = config["bar"]["modules"]
for module in modules:
parameters = parse_module(module)
# print(parameters)
x = Thread(target=new_module_thread, args=parameters)
x.start()
if __name__ == "__main__":
main()

View file

@ -1,101 +0,0 @@
from yaml import safe_load
from io import open
from threading import Thread
from subprocess import run
from time import sleep
running_modules_dict = {"left": {}, "center": {}, "right": {}}
def print_modules():
for alignment in running_modules_dict:
match alignment:
case "left":
print("%{l}", end="")
case "center":
print("%{c}", end="")
case "right":
print("%{r}", end="")
for module in running_modules_dict[alignment]:
print(running_modules_dict[alignment][module], end="")
print(flush=True)
def new_module_thread(alignment, name, cmd, refresh, pre, post):
while True:
result = pre
result += run(cmd, shell=True, capture_output=True, text=True).stdout.strip()
result += post
running_modules_dict[alignment][name] = result
print_modules()
sleep(refresh/1000)
def parse_module(module):
cmd = module["command"]
refresh = module["refresh"]
pre = ""
post = ""
format_options = module["format"]
for option in format_options:
match option:
# Align is handled differently. See below.
# case "align":
case "offset":
pre += ("%{O" + str(format_options[option]) + "}")
case "bg_color":
pre += ("%{B" + format_options[option] + "}")
post += "%{B-}"
case "fg_color":
pre += ("%{F" + format_options[option] + "}")
post += "%{F-}"
case "font":
pre += ("%{T" + format_options[option] + "}")
post += "%{T-}"
case "line":
line_options = format_options[option]
for line_option in line_options:
match line_option:
case "type":
if line_options[line_option] == "underline":
pre += ("%{+u}")
post += ("%{-u}")
elif line_options[line_option] == "overline":
pre += ("%{+o}")
post += ("%{-o}")
case "color":
pre += ("%{U" + line_options[line_option] + "}")
post += "%{U-}"
case "button":
button_options = format_options[option]
button = 1
match button_options["activator"]:
case "left":
button = 1
case "middle":
button = 2
case "right":
button = 3
case "scrup":
button = 4
case "scrdown":
button = 5
pre += ("%{A" + str(button)+ ":" + button_options["command"] + ":}")
post += "%{A}"
return [cmd, refresh, pre, post]
with open("./testing_config.yml", "r", encoding="utf8") as file:
config_file = safe_load(file)
modules = config_file["bar"]["modules"]
for module in modules:
parameters = parse_module(modules[module])
parameters.insert(0, module)
alignment = modules[module]["format"]["align"]
parameters.insert(0, alignment)
running_modules_dict[alignment][module] = ''
print(parameters)
x = Thread(target=new_module_thread, args=parameters)
x.start()