From 0b6084d4ee35e130dce82ddc13aa214e6e864880 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Mon, 7 Jan 2019 22:53:08 +0000 Subject: [PATCH] Nicer server setup --- README.md | 6 ++++-- server/config.py | 3 --- server/run | 8 ------- server/server.py | 43 +++++++++++++++++++++++++++++++++----- server/setup | 54 +++++++++++++++++++++++++++++++++--------------- 5 files changed, 79 insertions(+), 35 deletions(-) delete mode 100644 server/config.py delete mode 100755 server/run mode change 100644 => 100755 server/server.py diff --git a/README.md b/README.md index 3304974..37701c9 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,12 @@ possibly selected text, additional comments or tags and send it into your [Org M * `pip3 install --user hug` for [Hug](http://www.hug.rest/) HTTP server. # Running -1. Install server counterpart as systemd service: `server/setup`. +1. Install server counterpart as systemd service (to autostart it): `server/setup --path /path/to/your/capture.org [--port ]`. + + Or alternatively, just run it directly if you don't want to autostart it: `server/grasp_server.py --path /path/to/your/capture.org [--port ]`. 2. Install chrome extension and configure hotkeys -That's it! Currently port `12212` is hardcoded, but it will be configurable. +That's it! **Currently port `12212` is hardcoded** in the extension, but it will be configurable later. # Motivation Why use org-capture? Well, it's hard to explain, maybe some other time... However if you do know you want to use it instead/alongside your browser bookmarks, by default diff --git a/server/config.py b/server/config.py deleted file mode 100644 index c21ace7..0000000 --- a/server/config.py +++ /dev/null @@ -1,3 +0,0 @@ -from pathlib import Path - -CAPTURE_PATH = Path('/L/Dropbox/mobile/notes/links.org') diff --git a/server/run b/server/run deleted file mode 100755 index e8a71cb..0000000 --- a/server/run +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -eux - -cd "$(dirname "$0")" - -PORT=12212 - -hug -p "$PORT" -f server.py diff --git a/server/server.py b/server/server.py old mode 100644 new mode 100755 index 94dbf57..00b4601 --- a/server/server.py +++ b/server/server.py @@ -1,13 +1,17 @@ -"""First API, local access only""" +#!/usr/bin/python3 +import argparse import json import re +import os +from pathlib import Path import hug # type: ignore import hug.types as T # type: ignore -from config import CAPTURE_PATH # TODO mm, maybe configure it during server setup? from org_tools import append_org_entry +CAPTURE_PATH_VAR = 'GRASP_CAPTURE_PATH' + def empty(s) -> bool: return s is None or len(s.strip()) == 0 @@ -27,6 +31,8 @@ def capture( ): log("capturing", url, title, selection, comment) + capture_path = Path(os.environ[CAPTURE_PATH_VAR]).expanduser() + heading = url parts = [] tags = [] @@ -51,13 +57,13 @@ def capture( ]) body = None if len(parts) == 0 else '\n'.join(parts) - + # TODO format response on extension site response = { - 'file': str(CAPTURE_PATH), + 'file': str(capture_path), } try: append_org_entry( - CAPTURE_PATH, + capture_path, heading=heading, body=body, tags=tags, @@ -74,3 +80,30 @@ def capture( }) return json.dumps(response).encode('utf8') + +def run(port: str, capture_path: str): + env = os.environ.copy() + # not sure if there is a simpler way to communicate with the server... + env[CAPTURE_PATH_VAR] = capture_path + os.execvpe( + 'hug', + [ + 'grasp-server', + '-p', port, + '-f', __file__, + ], + env, + ) + +def setup_parser(p): + p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension') + p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into') + +def main(): + p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter) + setup_parser(p) + args = p.parse_args() + run(args.port, args.path) + +if __name__ == '__main__': + main() diff --git a/server/setup b/server/setup index 45b2d9b..91cbbe8 100755 --- a/server/setup +++ b/server/setup @@ -1,10 +1,13 @@ #!/usr/bin/env python3 +import argparse + from pathlib import Path from subprocess import check_call -run_script = Path(__file__).parent.joinpath('run').absolute() +# TODO rename to grasp_server.py +from server import setup_parser -CONFIG = f""" +SYSTEMD_CONFIG = """ [Unit] Description=Grasp extension server conterpart @@ -12,29 +15,46 @@ Description=Grasp extension server conterpart WantedBy=default.target [Service] -ExecStart={run_script} +ExecStart={server} {extra_args} Type=simple Restart=always """ -SNAME = 'grasp.service' - -out = Path(f'~/.config/systemd/user/{SNAME}').expanduser() - -print(f"Writing systemd config to {out}:") -with out.open('w') as fo: - fo.write(CONFIG) - - def systemd(*args): check_call([ 'systemctl', '--user', *args, ]) -systemd('daemon-reload') -systemd('enable', SNAME) -systemd('start' , SNAME) -systemd('status', SNAME) +def setup(args): + unit_name = args.unit_name + out = Path(f'~/.config/systemd/user/{unit_name}').expanduser() + print(f"Writing systemd config to {out}:") -# TODO journalctl --user -u grasp.service to debug + server_bin = Path(__file__).parent.joinpath('server.py').absolute() + extra_args = f'--port {args.port} --path {args.path}' + + with out.open('w') as fo: + fo.write(SYSTEMD_CONFIG.format( + server=server_bin, + extra_args=extra_args, + )) + + try: + systemd('daemon-reload') + systemd('enable', unit_name) + systemd('start' , unit_name) + systemd('status', unit_name) + except Exception as e: + print(f"Something has gone wrong... you might want to use 'journalctl --user -u {unit_name}' to debug") + raise e + +def main(): + p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter) + p.add_argument('--unit-name', type=str, default='grasp.service', help='Systemd unit name') + setup_parser(p) + args = p.parse_args() + setup(args) + +if __name__ == '__main__': + main()