add listen command

This commit is contained in:
Soren I. Bjornstad 2021-08-25 15:25:50 -05:00
parent 7da128ea80
commit b61ff148ed
3 changed files with 72 additions and 16 deletions

View File

@ -3,19 +3,25 @@ import os
from pathlib import Path from pathlib import Path
import sys import sys
config_path = Path.cwd()
for child in sorted(config_path.iterdir()): class ConfigurationManager:
if child.is_file() and child.name.endswith('.py'): def __init__(self):
mod_name = child.name.rsplit('.', 1)[0] config_path = Path.cwd()
if mod_name == 'tzk_config':
sys.path.insert(0, str(config_path)) for child in sorted(config_path.iterdir()):
importlib.import_module(mod_name) if child.is_file() and child.name.endswith('.py'):
del sys.path[0] mod_name = child.name.rsplit('.', 1)[0]
break if mod_name == 'tzk_config':
else: sys.path.insert(0, str(config_path))
print( self.conf_mod = importlib.import_module(mod_name)
f"Your TZK config file could not be found. " del sys.path[0]
f"Please ensure there is a file called tzk_config.py " break
f"in the current directory.", file=sys.stderr) else:
sys.exit(1) print(
f"Your TZK config file could not be found. "
f"Please ensure there is a file called tzk_config.py "
f"in the current directory.", file=sys.stderr)
sys.exit(1)
def __getattr__(self, attr):
return getattr(self.conf_mod, attr, None)

12
tw.py Normal file
View File

@ -0,0 +1,12 @@
import subprocess
from typing import Sequence
def exec(args: Sequence[Sequence[str]]) -> None:
bin_dir = subprocess.check_output(("npm", "bin"), text=True).strip()
call_args = [bin_dir + "/tiddlywiki"]
for tw_arg in args:
call_args.append(f"--{tw_arg[0]}")
for inner_arg in tw_arg[1:]:
call_args.append(inner_arg)
return subprocess.call(call_args)

40
tzk.py
View File

@ -2,7 +2,9 @@ from abc import ABC, abstractmethod, abstractclassmethod
import argparse import argparse
import os import os
import config
import git import git
import tw
class CliCommand(ABC): class CliCommand(ABC):
@ -40,7 +42,43 @@ class CommitCommand(CliCommand):
git.exec("push", "backup") git.exec("push", "backup")
import config class ListenCommand(CliCommand):
cmd = "listen"
help = "Start a TiddlyWiki server in the current directory."
@classmethod
def setup_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"-p", "--port",
metavar="PORT",
help="Port to listen on.",
default=str(cm.listen_port or "8080"),
)
parser.add_argument(
"--username",
metavar="USERNAME",
default=cm.listen_username or "",
help="Username to use for basic authentication, if any.",
)
parser.add_argument(
"--password",
metavar="PASSWORD",
default=cm.listen_password or "",
help="Password to use for basic authentication, if any.",
)
def execute(self, args: argparse.Namespace) -> None:
tw.exec(
[
("listen",
f"port={args.port}",
f"username={args.username}",
f"password={args.password}")
]
)
cm = config.ConfigurationManager()
os.chdir("zk-wiki") os.chdir("zk-wiki")
# TODO: confirm we're in the right directory # TODO: confirm we're in the right directory