tolkien: add CLI scripts

This commit is contained in:
pompeo
2026-04-08 18:03:40 +02:00
parent 15e1c90d6c
commit cbfb4cc621

94
scripts/tolkien_cli.py Executable file
View File

@@ -0,0 +1,94 @@
#!/usr/bin/env python3
import sys, os, argparse, datetime, subprocess
ROOT=os.path.dirname(os.path.dirname(__file__))
CAMPAIGNS_DIR=os.path.join(ROOT,'campaings')
def git_commit_and_push(msg):
subprocess.run(['git','add','-A'],cwd=ROOT,check=True)
subprocess.run(['git','commit','-m',msg],cwd=ROOT,check=True)
subprocess.run(['git','push','origin','main'],cwd=ROOT,check=True)
def ensure_dirs(pg_name):
base=os.path.join(CAMPAIGNS_DIR,pg_name)
os.makedirs(os.path.join(base,'npc'),exist_ok=True)
os.makedirs(os.path.join(base,'facts'),exist_ok=True)
os.makedirs(os.path.join(base,'seasons'),exist_ok=True)
os.makedirs(os.path.join(base,'sessions'),exist_ok=True)
return base
def create_new(args):
pg=args.name
base=ensure_dirs(pg)
readme=os.path.join(base,'README.md')
setting=os.path.join(base,'SETTING.md')
pgfile=os.path.join(base,'PG.md')
ts=datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
session=os.path.join(base,'sessions',f'session-{ts}.md')
if not os.path.exists(readme):
with open(readme,'w') as f:
f.write(f"# {pg}\n\nactive: true\n\nsummary: |\n TODO: short description\n")
if not os.path.exists(setting):
with open(setting,'w') as f:
f.write("# SETTING\n\nDescribe the world here.\n")
if not os.path.exists(pgfile):
with open(pgfile,'w') as f:
f.write("# PG\n\nPlayer character sheet.\n")
with open(session,'w') as f:
f.write(f"# Session {ts}\n\nPlayer: \n\n")
print('created',base,'session',session)
git_commit_and_push(f'tolkien: new campaign {pg}')
def list_active(args):
res=[]
if not os.path.isdir(CAMPAIGNS_DIR):
print('no campaigns dir')
return
for name in os.listdir(CAMPAIGNS_DIR):
p=os.path.join(CAMPAIGNS_DIR,name,'README.md')
if os.path.exists(p):
with open(p) as f:
content=f.read()
active='active: true' in content
summary_lines=[l for l in content.splitlines() if l.strip().startswith('summary:')]
res.append((name,active))
for r in res:
print(r[0],'ACTIVE' if r[1] else 'inactive')
def close_session(args):
pg=args.name
base=os.path.join(CAMPAIGNS_DIR,pg)
sessions=os.path.join(base,'sessions')
if not os.path.isdir(sessions):
print('no sessions for',pg); return
files=sorted(os.listdir(sessions))
if not files:
print('no session files'); return
last=files[-1]
path=os.path.join(sessions,last)
# simple recap: list new files under npc/facts/seasons (untracked detection omitted)
# Append a closing note
with open(path,'a') as f:
f.write('\n\n---\nSession closed by tolkien.\n')
git_commit_and_push(f'tolkien: close session {pg} {last}')
print('closed',path)
parser=argparse.ArgumentParser()
subparsers=parser.add_subparsers()
p_new=subparsers.add_parser('new')
p_new.add_argument('name')
p_new.set_defaults(func=create_new)
p_list=subparsers.add_parser('restore')
p_list.set_defaults(func=list_active)
p_close=subparsers.add_parser('close')
p_close.add_argument('name')
p_close.set_defaults(func=close_session)
if __name__=='__main__':
args=parser.parse_args()
if not hasattr(args,'func'):
parser.print_help(); sys.exit(1)
args.func(args)