clean up a bunch of stuff.

This commit is contained in:
GitMatt 2025-04-26 07:43:49 -07:00
parent c8cb76761a
commit f2e8c63459
6 changed files with 159 additions and 145 deletions

View File

@ -1,61 +1,76 @@
import os import os
import subprocess
import sys
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
from .templates import default from jinja2 import Environment, PackageLoader, select_autoescape
def today() -> Path: def get_template(template_name: str = "plan.html", plan_path: Path | None = None):
""" """
templates = Environment(
loader=PackageLoader("plan"), autoescape=select_autoescape()
)
template = templates.get_template(template_name)
today = datetime.today().strftime("%Y-%m-%d") today = datetime.today().strftime("%Y-%m-%d")
today_path = Path().home() / ".plan" / f"{today}.md" return template.render(today=today, plan_path=plan_path)
return today_path
def print_plan(plan: Path): def plan_path() -> Path:
import subprocess """
returns: Path to current date's .plan.md
"""
today = datetime.today().strftime("%Y-%m-%d")
path = Path().home() / ".plan" / f"{today}.md"
return path
cat_process = subprocess.Popen(["cat", str(plan)], stdout=subprocess.PIPE)
def print_plan(plan_path: Path):
"""
send .plan.md to printer.
uses pandoc and lp shell tools.
"""
# preamble ="""
# ---
# documentclass: extarticle
# fontsize: 20pt
# ---
# """
cat_process = subprocess.Popen(["cat", str(plan_path)], stdout=subprocess.PIPE)
pandoc_process = subprocess.Popen( pandoc_process = subprocess.Popen(
["pandoc", "-t", "pdf", "-V", "geometry:margin=.5in", "-"], ["pandoc", "-t", "pdf", "-V", "geometry:margin=.5in", "-"],
stdin=cat_process.stdout, stdin=cat_process.stdout,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
) )
subprocess.run( subprocess.run(
["lp", "-o", "sides=two-sided-long-edge"], ["lp", "-o", "sides=two-sided-long-edge", "-o", "number-up=2"],
# , "-o", "number-up=4"],
stdin=pandoc_process.stdout, stdin=pandoc_process.stdout,
) )
def template_or_open( def create_and_open_template(plan: Path, name: str = "plan.html") -> int:
plan: Path, name: str = "plan.html", should_open: bool = True plan.parent.mkdir(parents=True, exist_ok=True)
) -> str | None: plan.touch()
template = default(name)
if should_open: template = get_template(name, plan_path=plan)
import subprocess with plan.open("w") as f:
f.write(template)
plan.parent.mkdir(parents=True, exist_ok=True) editor = os.getenv("EDITOR", "nvim")
if not plan.exists(): os.chdir(plan.parent)
plan.touch() p = subprocess.Popen([editor, str(plan)])
with plan.open("w") as f: sys.exit(p.wait())
f.write(template)
editor = os.getenv("EDITOR", "nvim")
os.chdir(plan.parent)
subprocess.run([editor, str(plan)])
else:
return template
def open_in_editor(plan: Path): def open_in_editor(plan: Path):
import subprocess
plan.parent.mkdir(parents=True, exist_ok=True) plan.parent.mkdir(parents=True, exist_ok=True)
if not plan.exists(): if not plan.exists():
plan.touch() plan.touch()
with plan.open("w") as f: with plan.open("w") as f:
f.write(default()) f.write(default_template(plan_path=plan))
editor = os.getenv("EDITOR", "nvim") editor = os.getenv("EDITOR", "nvim")
os.chdir(Path().home() / ".plan") os.chdir(Path().home() / ".plan")
subprocess.run([editor, str(plan)]) subprocess.run([editor, str(plan)])

View File

View File

@ -1,94 +1,41 @@
import sys
from pathlib import Path from pathlib import Path
from pydantic import BaseModel, ConfigDict
from typing import Iterable, Optional
import click import click
from . import open_in_editor, template_or_open, today, print_plan
import markdown
from io import StringIO
from click_default_group import DefaultGroup from click_default_group import DefaultGroup
from .main import (
create_plan,
edit_plan,
plan_path,
print_plan,
)
@click.group(cls=DefaultGroup, default="today", default_if_no_args=True)
@click.group(cls=DefaultGroup, default="main", default_if_no_args=True)
def cli(): def cli():
pass pass
@cli.command("print") @cli.command("print")
def print_today(): @click.option(
path = today() "--plan_path",
print_plan(path) "-p",
"plan_path",
type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path),
default=plan_path(),
)
def send_to_printer(plan_path: Path):
print_plan(plan_path)
@cli.command("todo") @cli.command("main")
def todo():
import sys
from lxml import etree
class Result(BaseModel):
path: Path
todos: Iterable[etree.Element]
model_config = ConfigDict(arbitrary_types_allowed=True)
def parse_todo() -> Iterable[Result]:
for m in recent():
with open(m) as f:
text = f.read()
content = markdown.markdown(text)
tree = etree.parse(
StringIO(f"<root>{content}</root>"), etree.HTMLParser()
)
for node in tree.xpath("//h2/following-sibling::ul/li"):
yield Result(path=m, todos=node)
todos = parse_todo()
def print_tree(node: Optional[etree.Element], tabs=0):
if node is None:
return
print(f"{' ' * tabs}- {node.text}")
for child in node.iterchildren():
return print_tree(child, tabs + 1)
for day in todos:
for root in day.todos:
print(f"{day.path.name}")
print_tree(root)
else:
print("no todos")
sys.exit(0)
"""
uv run plan todo
interact
import wat
print(node)
r = next(root)
r
wat(node)
dir(node)
wat(node)
[s for s in node.itersiblings()]
[s for s in node.iterchildren()]
print(node.tag)
parent = node.xpath("//following-sibling::ul")[0]
[s.text for s in parent.iterchildren()]
sys.exit(0)
"""
@cli.command("today")
@click.option( @click.option(
"-t", "-t",
"--template", "--template",
"template", "template",
required=True, required=True,
type=click.Choice(["plan.html", "work.html", "blank.html", "print.html"]), type=click.Choice(["plan.html", "work.html"]),
prompt=True, prompt=True,
default="plan.html", default="plan.html",
show_default=True, show_default=True,
@ -103,17 +50,9 @@ def todo():
) )
def open_today(template, should_print): def open_today(template, should_print):
"""open today""" """open today"""
path = today() path = plan_path()
content = create_plan(plan_path=path, name=template)
if should_print: if should_print:
print(template_or_open(path, should_open=False, name=template)) print(content)
else: else:
open_in_editor(path) sys.exit(edit_plan(path))
def recent() -> Iterable[Path]:
parent = Path().home() / ".plan"
for m in parent.iterdir():
if m.suffix == ".md":
yield m
else:
yield m

84
src/plan/main.py Normal file
View File

@ -0,0 +1,84 @@
import os
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from jinja2 import Environment, PackageLoader, select_autoescape
def get_template(template_name: str = "plan.html", plan_path: Path | None = None):
""" """
templates = Environment(
loader=PackageLoader("plan"), autoescape=select_autoescape()
)
breakpoint()
template = templates.get_template(template_name)
today = datetime.today().strftime("%Y-%m-%d")
return template.render(today=today, plan_path=plan_path)
def plan_path(date: datetime | None = None) -> Path:
"""
returns: Path to current date's .plan.md
"""
if date is None:
date = datetime.today()
formatted = date.strftime("%Y-%m-%d")
path = Path().home() / ".plan" / f"{formatted}.md"
return path
def print_plan(plan_path: Path):
"""
send .plan.md to printer.
uses pandoc and lp shell tools.
"""
preamble = """
---
documentclass: extarticle
fontsize: 20pt
---
"""
preamble = subprocess.Popen(["echo", "-n", preamble], stdout=subprocess.PIPE)
cat = subprocess.Popen(
["cat", "-", str(plan_path)],
stdin=preamble.stdout,
stdout=subprocess.PIPE,
)
pandoc = subprocess.Popen(
["pandoc", "-t", "pdf", "-V", "geometry:margin=.5in", "-"],
stdin=cat.stdout,
stdout=subprocess.PIPE,
)
send = subprocess.Popen(
["lp", "-o", "sides=two-sided-long-edge", "-o", "number-up=2"],
stdin=pandoc.stdout,
)
sys.exit(send.wait())
def create_plan(plan_path: Path, name: str = "plan.html", force=False) -> str:
plan_path.parent.mkdir(parents=True, exist_ok=True)
if not plan_path.exists() or force:
content = get_template(name, plan_path=plan_path)
with plan_path.open("w") as f:
f.write(content)
else:
content = get_plan(plan_path)
return content
def get_plan(plan_path: Path) -> str:
with plan_path.open("r") as f:
content = f.read()
return content
def edit_plan(plan_path: Path) -> int:
editor = os.getenv("EDITOR", "nvim")
os.chdir(plan_path.parent)
p = subprocess.Popen([editor, str(plan_path)])
return p.wait()

View File

@ -2,38 +2,18 @@
## todo ## todo
- chore: - appreciate life.
- workout:
- job:
- comm:
- buy:
## meals ## meals
- breakfast: oatmeal - breakfast: coffee, oatmeal
- lunch: leftovers - lunch: lx, leftovers, carrots, cheese, crackers, apple
- dinner: curry - dinner: lx, curry
## schedule ## schedule
- 5:00: sleep. - 6:30: wakeup.
- 6:00: breakfast. - 9:30: sleep
- 7:00: walk.
- 8:00: meeting.
- 9:00: code.
- 10:00: code.
- 11:00: walk.
- 12:00: lunch.
- 1:00: code.
- 2:00: code.
- 3:00: walk.
- 4:00: code.
- 5:30: trailhead.
- 6:00: cook.
- 7:00: dinner.
- 8:00: wind down.
- 9:00: read.
- 10:00: sleep.
## groceries ## groceries
@ -48,4 +28,3 @@
- cheese. - cheese.
- apples. - apples.
- bread. - bread.

View File

@ -1,3 +0,0 @@
```bash
cat ~/.plan/{{ today }}.md | pandoc -t pdf -V geometry:margin=.5in - | lp -o sides=two-sided-long-edge.plan -o number-up=4
```