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
import sys
config_path = Path.cwd()
for child in sorted(config_path.iterdir()):
if child.is_file() and child.name.endswith('.py'):
mod_name = child.name.rsplit('.', 1)[0]
if mod_name == 'tzk_config':
sys.path.insert(0, str(config_path))
importlib.import_module(mod_name)
del sys.path[0]
break
else:
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)
class ConfigurationManager:
def __init__(self):
config_path = Path.cwd()
for child in sorted(config_path.iterdir()):
if child.is_file() and child.name.endswith('.py'):
mod_name = child.name.rsplit('.', 1)[0]
if mod_name == 'tzk_config':
sys.path.insert(0, str(config_path))
self.conf_mod = importlib.import_module(mod_name)
del sys.path[0]
break
else:
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 os
import config
import git
import tw
class CliCommand(ABC):
@ -40,7 +42,43 @@ class CommitCommand(CliCommand):
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")
# TODO: confirm we're in the right directory