From 193020543bc9ca6ec57a08636f5b3f55c4dd78d7 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Sun, 7 Feb 2016 14:05:18 -0800 Subject: [PATCH] support for a pre_tweet_hook configuration parameter --- README.rst | 5 ++++- config.example | 1 + twtxt/cli.py | 13 ++++++++++--- twtxt/config.py | 5 +++++ twtxt/helper.py | 8 ++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 45f6cd8..15ab547 100644 --- a/README.rst +++ b/README.rst @@ -125,6 +125,7 @@ Here’s an example ``conf`` file, showing every currently supported option: limit_timeline = 20 timeout = 5.0 sorting = descending + pre_tweet_hook = "scp buckket@example.org:~/public_html/twtxt.txt {twtfile}" post_tweet_hook = "scp {twtfile} buckket@example.org:~/public_html/twtxt.txt" [following] @@ -151,10 +152,12 @@ Here’s an example ``conf`` file, showing every currently supported option: +-------------------+-------+------------+---------------------------------------------------+ | sorting | TEXT | descending | sort timeline either descending or ascending | +-------------------+-------+------------+---------------------------------------------------+ +| pre_tweet_hook | TEXT | | command to be executed before tweeting | ++-------------------+-------+------------+---------------------------------------------------+ | post_tweet_hook | TEXT | | command to be executed after tweeting | +-------------------+-------+------------+---------------------------------------------------+ -``post_tweet_hook`` is very useful if you want to push your twtxt file to a remote (web) server. Check the example above tho see how it’s used with ``scp``. +``pre_tweet_hook`` and ``post_tweet_hook`` are very useful if you want to push your twtxt file to a remote (web) server. Check the example above tho see how it’s used with ``scp``. [followings] section: ===================== diff --git a/config.example b/config.example index 9fd7852..dff0c39 100644 --- a/config.example +++ b/config.example @@ -6,6 +6,7 @@ use_pager = False limit_timeline = 20 timeout = 5.0 sorting = descending +pre_tweet_hook = "scp buckket@example.org:~/public_html/twtxt.txt {twtfile}" post_tweet_hook = "scp {twtfile} buckket@example.org:~/public_html/twtxt.txt" [following] diff --git a/twtxt/cli.py b/twtxt/cli.py index 64134c6..22e4da3 100644 --- a/twtxt/cli.py +++ b/twtxt/cli.py @@ -18,6 +18,7 @@ from twtxt.config import Config from twtxt.file import get_local_tweets, add_local_tweet from twtxt.helper import run_post_tweet_hook +from twtxt.helper import run_pre_tweet_hook from twtxt.helper import style_tweet, style_source, style_source_with_status from twtxt.helper import validate_created_at, validate_text from twtxt.helper import sort_and_truncate_tweets @@ -69,12 +70,18 @@ def cli(ctx, config, verbose): def tweet(ctx, created_at, twtfile, text): """Append a new tweet to your twtxt file.""" tweet = Tweet(text, created_at) if created_at else Tweet(text) + + pre_tweet_hook = ctx.obj["conf"].pre_tweet_hook + if pre_tweet_hook: + if not run_pre_tweet_hook(pre_tweet_hook, ctx.obj["conf"].options): + raise Exception("✗ pre_tweet_hook returned non-zero") + if not add_local_tweet(tweet, twtfile): click.echo("✗ Couldn’t write to file.") else: - hook = ctx.obj["conf"].post_tweet_hook - if hook: - run_post_tweet_hook(hook, ctx.obj["conf"].options) + post_tweet_hook = ctx.obj["conf"].post_tweet_hook + if post_tweet_hook: + run_post_tweet_hook(post_tweet_hook, ctx.obj["conf"].options) @cli.command() diff --git a/twtxt/config.py b/twtxt/config.py index 9a72ddd..796ec04 100644 --- a/twtxt/config.py +++ b/twtxt/config.py @@ -101,6 +101,11 @@ def nick(self): cfg = self.open_config() return cfg.get("twtxt", "nick", fallback=os.environ.get("USER", "")) + @property + def pre_tweet_hook(self): + cfg = self.open_config() + return cfg.get("twtxt", "pre_tweet_hook", fallback=None) + @property def post_tweet_hook(self): cfg = self.open_config() diff --git a/twtxt/helper.py b/twtxt/helper.py index f0884b6..206706b 100644 --- a/twtxt/helper.py +++ b/twtxt/helper.py @@ -66,6 +66,14 @@ def validate_text(ctx, param, value): raise click.BadArgumentUsage("Text can’t be empty.") +def run_pre_tweet_hook(hook, options): + try: + command = shlex.split(hook.format(**options)) + except KeyError: + click.echo("✗ Invalid variables in pre_tweet_hook.") + return False + return not subprocess.call(command, shell=True, stdout=subprocess.PIPE) + def run_post_tweet_hook(hook, options): try: command = shlex.split(hook.format(**options))