Skip to content

Commit

Permalink
support for a pre_tweet_hook configuration parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kitchen committed Feb 7, 2016
1 parent b5517f6 commit 1930205
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]:~/public_html/twtxt.txt {twtfile}"
post_tweet_hook = "scp {twtfile} [email protected]:~/public_html/twtxt.txt"
[following]
Expand All @@ -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:
=====================
Expand Down
1 change: 1 addition & 0 deletions config.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use_pager = False
limit_timeline = 20
timeout = 5.0
sorting = descending
pre_tweet_hook = "scp [email protected]:~/public_html/twtxt.txt {twtfile}"
post_tweet_hook = "scp {twtfile} [email protected]:~/public_html/twtxt.txt"

[following]
Expand Down
13 changes: 10 additions & 3 deletions twtxt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions twtxt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions twtxt/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 1930205

Please sign in to comment.