Skip to content

Commit

Permalink
Merge branch 'kitchen-pre_tweet_hook'
Browse files Browse the repository at this point in the history
  • Loading branch information
buckket committed Feb 12, 2016
2 parents de12adb + 964102f commit 5f0d000
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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"
# post_tweet_hook = "aws s3 {twtfile} s3://mybucket.org/twtxt.txt --acl public-read --storage-class REDUCED_REDUNDANCY --cache-control 'max-age=60,public'"
Expand Down Expand Up @@ -192,10 +193,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 @@ -9,6 +9,7 @@ porcelain = 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
3 changes: 3 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def config_dir(tmpdir_factory):
cfg.set("twtxt", "timeout", "1.0")
cfg.set("twtxt", "sorting", "ascending")
cfg.set("twtxt", "post_tweet_hook", "echo {twtfile")
cfg.set("twtxt", "pre_tweet_hook", "echo {twtfile")

cfg.add_section("following")
cfg.set("following", "foo", "https://example.org/foo.twtxt")
Expand Down Expand Up @@ -50,6 +51,7 @@ def test_defaults():
assert empty_conf.timeout == 5.0
assert empty_conf.sorting == "descending"
assert empty_conf.post_tweet_hook is None
assert empty_conf.pre_tweet_hook is None


def check_cfg(cfg):
Expand All @@ -64,6 +66,7 @@ def check_cfg(cfg):
assert cfg.timeout == 1.0
assert cfg.sorting == "ascending"
assert cfg.post_tweet_hook == "echo {twtfile"
assert cfg.pre_tweet_hook == "echo {twtfile"


def test_from_file(config_dir):
Expand Down
15 changes: 11 additions & 4 deletions twtxt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from twtxt.cache import Cache
from twtxt.config import Config
from twtxt.helper import run_post_tweet_hook
from twtxt.helper import run_pre_tweet_hook, run_post_tweet_hook
from twtxt.helper import sort_and_truncate_tweets
from twtxt.helper import style_timeline, style_source, style_source_with_status
from twtxt.helper import validate_created_at, validate_text
Expand Down Expand Up @@ -72,12 +72,19 @@ def tweet(ctx, created_at, twtfile, text):
"""Append a new tweet to your twtxt file."""
text = expand_mentions(text)
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):
click.echo("✗ pre_tweet_hook returned non-zero")
raise click.Abort

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
4 changes: 4 additions & 0 deletions twtxt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def sorting(self):
def source(self):
return Source(self.nick, self.twturl)

@property
def pre_tweet_hook(self):
return self.cfg.get("twtxt", "pre_tweet_hook", fallback=None)

@property
def post_tweet_hook(self):
return self.cfg.get("twtxt", "post_tweet_hook", fallback=None)
Expand Down
11 changes: 10 additions & 1 deletion twtxt/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ 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))
except KeyError:
click.echo("✗ Invalid variables in post_tweet_hook.")
return False
subprocess.call(command, shell=True, stdout=subprocess.PIPE)
return not subprocess.call(command, shell=True, stdout=subprocess.PIPE)


def sort_and_truncate_tweets(tweets, direction, limit):
Expand Down

0 comments on commit 5f0d000

Please sign in to comment.