feat: add reusable config object

This commit is contained in:
Daylin Morgan 2022-09-24 16:20:23 -05:00
parent cc81782069
commit bc4c95aab9
8 changed files with 75 additions and 23 deletions

View file

@ -4,7 +4,14 @@ import sys
from pathlib import Path
import jinja2
py_script_names = ["help", "ansi", "info", "print-ansi", "vars", "confirm"]
py_script_names = [
"help",
"info",
"print-ansi",
"vars",
"confirm",
"utils",
]
def get_jinja_env():

View file

@ -4,10 +4,10 @@ ACCENT_STYLE ?= b_yellow
PARAMS_STYLE ?= $(ACCENT_STYLE)
GOAL_STYLE ?= $(ACCENT_STYLE)
MSG_STYLE ?= faint
DIVIDER_STYLE ?= default
DIVIDER ?=
DIVIDER_STYLE ?= default
HELP_SEP ?=
# python f-string literals
EPILOG ?=
USAGE ?={ansi.$(HEADER_STYLE)}usage{ansi.end}:\n make <recipe>\n
USAGE ?={ansi.header}usage{ansi.end}:\n make <recipe>\n
INHERIT_SHELL ?=

View file

@ -4,7 +4,7 @@
import sys
##- '$(ansi_py)' -##
##- '$(utils_py)' -##
def confirm():

View file

@ -9,7 +9,13 @@ import subprocess
import sys
from textwrap import wrap
##- '$(ansi_py)' -##
##- '$(utils_py)' -##
###-
# this is just to trick the LSP during development
from utils import ansi, cfg
# -###
MaxLens = namedtuple("MaxLens", "goal msg")
@ -32,12 +38,12 @@ def parseargs(argstring):
def divider(len):
return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_STYLE)")
return ansi.style(f" {cfg.div*len}", "div_style")
def gen_makefile():
makefile = ""
for file in os.getenv("MAKEFILE_LIST").split():
for file in os.getenv("MAKEFILE_LIST", "").split():
with open(file, "r") as f:
makefile += f.read() + "\n\n"
return makefile
@ -71,7 +77,7 @@ def recipe_help_header(goal):
item[0].get("msgargs", ""),
)
else:
return f" {ansi.style(goal,'$(GOAL_STYLE)')}"
return f" {ansi.style(goal,'goal')}"
def get_goal_deps(goal="task.mk"):
@ -82,7 +88,7 @@ def get_goal_deps(goal="task.mk"):
match = dep_pattern.search(line)
if match and match.groups()[0]:
return wrap(
f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'$(MSG_STYLE)')}",
f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'msg')}",
initial_indent=" ",
subsequent_indent=" ",
)
@ -116,8 +122,8 @@ def parse_goal(file, goal):
def fmt_goal(goal, msg, max_goal_len, argstr):
args = parseargs(argstr)
goal_style = args.goal_style.strip() if args.goal_style else "$(GOAL_STYLE)"
msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)"
goal_style = args.goal_style.strip() if args.goal_style else "goal"
msg_style = args.msg_style.strip() if args.msg_style else "msg"
return (
ansi.style(f" {goal:>{max_goal_len}}", goal_style)
+ f" $(HELP_SEP) "
@ -128,20 +134,20 @@ def fmt_goal(goal, msg, max_goal_len, argstr):
def fmt_rawmsg(msg, argstr, maxlens):
args = parseargs(argstr)
lines = []
msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)"
msg_style = args.msg_style.strip() if args.msg_style else "msg"
if not os.getenv("SHOW_HIDDEN") and args.hidden:
return []
if msg:
if args.align == "sep":
lines.append(
f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,msg_style)}"
f"{' '*(maxlens.goal+len(cfg.sep)+4)}{ansi.style(msg,msg_style)}"
)
elif args.align == "center":
lines.append(f" {ansi.style(msg.center(sum(maxlens)),msg_style)}")
else:
lines.append(f" {ansi.style(msg,msg_style)}")
if args.divider:
lines.append(divider(len("$(HELP_SEP)") + sum(maxlens) + 2))
lines.append(divider(len(cfg.sep) + sum(maxlens) + 2))
if args.whitespace:
lines.append("\n")
@ -149,7 +155,7 @@ def fmt_rawmsg(msg, argstr, maxlens):
def print_help():
lines = [f"""$(USAGE)"""]
lines = [cfg.usage]
items = list(parse_help(gen_makefile()))
maxlens = MaxLens(
@ -164,12 +170,12 @@ def print_help():
)
if "rawmsg" in item:
lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens))
lines.append(f"""$(EPILOG)""")
lines.append(cfg.epilog)
print("\n".join(lines))
def print_arg_help(help_args):
print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n")
print(f"{ansi.style('task.mk recipe help','header')}\n")
for arg in help_args.split():
print("\n".join(parse_goal(gen_makefile(), arg)))

View file

@ -1,7 +1,7 @@
#% extends "py-script.mk" %#
#% block name %#info#% endblock %#
#% block script %#
##- '$(ansi_py)' -##
##- '$(utils_py)' -##
print(f"""$(2)""")
#% endblock %#

View file

@ -1,7 +1,7 @@
#% extends "py-script.mk" %#
#% block name %#print_ansi#% endblock %#
#% block script %#
##- '$(ansi_py)' -##
##- '$(utils_py)' -##
sep = f"$(HELP_SEP)"
codes_names = {getattr(ansi, attr): attr for attr in ansi.__dict__}
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):

View file

@ -1,8 +1,24 @@
#% extends "py-script.mk" %#
#% block name %#ansi#% endblock %#
#% block name %#utils#% endblock %#
#% block script %#
import os
import sys
from dataclasses import dataclass
@dataclass
class Config:
header: str
accent: str
params: str
goal: str
msg: str
div: str
div_style: str
sep: str
epilog: str
usage: str
color2byte = dict(
black=0,
@ -77,6 +93,15 @@ class Ansi:
return code + end
def add_cfg(self, cfg):
cfg_attrs = {
attr: getattr(cfg, attr)
for attr in cfg.__dict__
if attr not in ["div", "sep", "epilog", "usage"]
}
for name, cfg_attr in cfg_attrs.items():
self.setcode(name, getattr(ansi, cfg_attr))
def style(self, text, style):
if style not in self.__dict__:
print(f"unknown style: {style}")
@ -86,4 +111,18 @@ class Ansi:
a = ansi = Ansi()
cfg = Config(
"$(HEADER_STYLE)",
"$(ACCENT_STYLE)",
"$(PARAMS_STYLE)",
"$(GOAL_STYLE)",
"$(MSG_STYLE)",
"$(DIVIDER)",
"$(DIVIDER_STYLE)",
"$(HELP_SEP)",
f"""$(EPILOG)""",
f"""$(USAGE)""",
)
ansi.add_cfg(cfg)
#% endblock %#

View file

@ -3,15 +3,15 @@
#% block script %#
import os
##- '$(ansi_py)' -##
##- '$(utils_py)' -##
vars = "$2".split()
length = max((len(v) for v in vars))
print(f"{ansi.$(HEADER_STYLE)}vars:{ansi.end}\n")
print(f"{ansi.header}vars{ansi.end}:\n")
for v in vars:
print(f" {ansi.b_magenta}{v:<{length}}{ansi.end} = {os.getenv(v)}")
print(f" {ansi.params}{v:<{length}}{ansi.end} = {os.getenv(v)}")
print()
#% endblock %#