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,24 +20,23 @@ 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,
"format": { "format": {
"align": Or("left", "center", "right"), "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("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("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("font"): And(int, lambda n: 1 <= n <= 5),
Optional("offset"): int, Optional("offset"): int,
Optional("line"): { Optional("line"): {
"type": Or("underline", "overline"), "type": Or("underline", "overline"),
Optional("color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$') Optional("color"): Regex(r'^#[0-9a-fA-F]{3}$|^#[0-9a-fA-F]{6}$|^#[0-9a-fA-F]{8}$')
}, },
Optional("button"): { Optional("button"): {
"activator": Or("left", "center", "right", "scrup", "scrdown"), "activator": Or("left", "center", "right", "scrup", "scrdown"),
"command": str "command": str
}
} }
} }
} }
@ -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):
print(get_lemonbar_flags(get_validated_config("./testing_config.yml"))) alignment = ""
pre = ""
name = ""
prefix = ""
command = ""
refresh = 0
post =""
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,51 +47,52 @@ 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.
# This command is run by `sh -c`, and whatever is sent to stdout is the - name: "time"
# text that will be displayed for this module. `date` here is used as an # This command is run by `sh -c`, and whatever is sent to stdout is the
# example. THIS IS A REQUIRED FIELD. # text that will be displayed for this module. `date` here is used as an
command: "date" # example. THIS IS A REQUIRED FIELD.
# How often to re-run the command and display the updated text. THIS IS A command: "date"
# REQUIRED FIELD. # How often to re-run the command and display the updated text. THIS IS A
refresh: 1000 # REQUIRED FIELD.
# Prefix to be printed before the text of the command. refresh: 1000
prefix: "DATE: " # Prefix to be printed before the text of the command.
# Formatting options. These will only affect the text and padding of this prefix: "DATE: "
# module. # Formatting options. These will only affect the text and padding of this
format: # module.
# Where the text should be placed on the bar. `left`, `right`, or format:
# `center`. THIS IS A REQUIRED FIELD. # Where the text should be placed on the bar. `left`, `right`, or
align: left # `center`. THIS IS A REQUIRED FIELD.
# Background color of the bar. align: left
bg_color: "#000" # Background color of the bar.
# Text color bg_color: "#000"
fg_color: "#FFF" # Text color
# INDEX of the font for this module to be displayed in (see above). fg_color: "#FFF"
font: 1 # INDEX of the font for this module to be displayed in (see above).
# Horizontal offset of the module text in pixels (can be negative). font: 1
offset: 0 # Horizontal offset of the module text in pixels (can be negative).
# Set an underline or overline offset: 0
line: # Set an underline or overline
# Self-explanatory. `underline` or `overline`. IF you set a line, then line:
# this is a required field. # Self-explanatory. `underline` or `overline`. IF you set a line, then
type: underline # this is a required field.
# Set a custom color for the line. type: underline
color: "#000" # Set a custom color for the line.
# Make this module act like a button. Both of these fields are required color: "#000"
# if you are making a button. You may also define multiple buttons with # Make this module act like a button. Both of these fields are required
# different activators for a single module. # if you are making a button. You may also define multiple buttons with
button: # different activators for a single module.
# What mouse click you have to make to activate the button. Can be button:
# `left`, `center`, or `right`. You can also set it to activate when # What mouse click you have to make to activate the button. Can be
# you scroll up or down with `scrup` and `scrdown.` # `left`, `center`, or `right`. You can also set it to activate when
activator: left # you scroll up or down with `scrup` and `scrdown.`
# Command to be executed upon activation. Will be run with `sh -c`. activator: left
command: "echo 'hello' > ~/file.txt" # Command to be executed upon activation. Will be run with `sh -c`.
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:
align: right align: right

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()