add dynamic config commands
This commit is contained in:
51
src/main.py
51
src/main.py
@@ -3,6 +3,7 @@
|
||||
# Akbar Rahman <hi@alv.cx>
|
||||
#
|
||||
|
||||
import json
|
||||
import sys
|
||||
import subprocess
|
||||
import time
|
||||
@@ -22,31 +23,45 @@ def get_args():
|
||||
|
||||
|
||||
def generate(config):
|
||||
if config.get('template'):
|
||||
img = Image.open(config['template'])
|
||||
else:
|
||||
img = Image.new(mode="RGBA", size = config['size'])
|
||||
|
||||
draw = ImageDraw.Draw(img)
|
||||
with open(config['font'], "rb") as fp:
|
||||
font = BdfFontFile.BdfFontFile(fp).to_imagefont()
|
||||
|
||||
process = subprocess.run(config['command'], stdout=subprocess.PIPE, shell=False)
|
||||
if process.returncode != config.get('return_code', 0):
|
||||
# process did not run succesfully.
|
||||
# do not risk displaying that to user
|
||||
return
|
||||
cmd_output = process.stdout.decode('utf-8').split('\n')
|
||||
print(f"{cmd_output=}")
|
||||
dynamic_config = {}
|
||||
if len(cmd_output) > 2:
|
||||
dynamic_config = tomllib.loads('\n'.join(cmd_output[:-2]))
|
||||
text = cmd_output[-2]
|
||||
else:
|
||||
text = cmd_output[0]
|
||||
|
||||
text = process.stdout.decode('utf-8').split('\n')[0]
|
||||
for filter in config.get('text_filters', []):
|
||||
if filter == "lowercase":
|
||||
text = text.lower()
|
||||
elif filter == "uppercase":
|
||||
text = text.upper()
|
||||
print(f"{dynamic_config=}")
|
||||
for key, val in dynamic_config.items():
|
||||
config[key] = val
|
||||
|
||||
fill = config.get('text_fill')
|
||||
fill = tuple(fill) if fill else None
|
||||
draw.text(config.get('text_offset', [0, 0]), text, font=font, fill=fill)
|
||||
if config.get('template'):
|
||||
img = Image.open(config['template'])
|
||||
else:
|
||||
img = Image.new(mode="RGBA", size = config['size'])
|
||||
|
||||
print(f"{text=}")
|
||||
|
||||
if text:
|
||||
draw = ImageDraw.Draw(img)
|
||||
with open(config['font'], "rb") as fp:
|
||||
font = BdfFontFile.BdfFontFile(fp).to_imagefont()
|
||||
|
||||
for filter in config.get('text_filters', []):
|
||||
if filter == "lowercase":
|
||||
text = text.lower()
|
||||
elif filter == "uppercase":
|
||||
text = text.upper()
|
||||
|
||||
fill = config.get('text_fill')
|
||||
fill = tuple(fill) if fill else None
|
||||
draw.text(config.get('text_offset', [0, 0]), text, font=font, fill=fill)
|
||||
|
||||
img.save(config['output'], save_all=True, loop=0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user