add listen command
This commit is contained in:
parent
7da128ea80
commit
b61ff148ed
14
config.py
14
config.py
@ -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:
|
||||||
|
def __init__(self):
|
||||||
|
config_path = Path.cwd()
|
||||||
|
|
||||||
|
for child in sorted(config_path.iterdir()):
|
||||||
if child.is_file() and child.name.endswith('.py'):
|
if child.is_file() and child.name.endswith('.py'):
|
||||||
mod_name = child.name.rsplit('.', 1)[0]
|
mod_name = child.name.rsplit('.', 1)[0]
|
||||||
if mod_name == 'tzk_config':
|
if mod_name == 'tzk_config':
|
||||||
sys.path.insert(0, str(config_path))
|
sys.path.insert(0, str(config_path))
|
||||||
importlib.import_module(mod_name)
|
self.conf_mod = importlib.import_module(mod_name)
|
||||||
del sys.path[0]
|
del sys.path[0]
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
f"Your TZK config file could not be found. "
|
f"Your TZK config file could not be found. "
|
||||||
f"Please ensure there is a file called tzk_config.py "
|
f"Please ensure there is a file called tzk_config.py "
|
||||||
f"in the current directory.", file=sys.stderr)
|
f"in the current directory.", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
return getattr(self.conf_mod, attr, None)
|
||||||
|
12
tw.py
Normal file
12
tw.py
Normal 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
40
tzk.py
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user