Skip to content

Commit

Permalink
Allow relative paths in FileService based uploaders
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 21, 2018
1 parent f6fa877 commit 1a5bac2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1567'
_commit = '4eef439'
_release = '1568'
_commit = 'f6fa877'
2 changes: 1 addition & 1 deletion src/pywws/examples/templates/toot.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
media /tmp/pywws/results/tweet.png
media tweet.png
#live#
#timezone local#
#roundtime True#
Expand Down
2 changes: 1 addition & 1 deletion src/pywws/examples/templates/tweet_media.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
media /home/jim/weather/results/tweet.png
media tweet.png
#hourly#
#timezone local#
#roundtime True#
Expand Down
10 changes: 9 additions & 1 deletion src/pywws/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from collections import deque
from datetime import datetime, timedelta
import os
import sys
import threading

Expand Down Expand Up @@ -277,6 +278,11 @@ def upload_batch(self):


class FileService(ServiceBase):
def __init__(self, context):
super(FileService, self).__init__(context)
self.local_dir = context.params.get(
'paths', 'local_files', os.path.expanduser('~/weather/results/'))

def do_catchup(self):
pending = eval(self.context.status.get(
'pending', self.service_name, '[]'))
Expand All @@ -303,6 +309,8 @@ def upload_batch(self):
if path not in files:
files.append(path)
else:
if not os.path.isabs(upload):
upload = os.path.join(self.local_dir, upload)
if upload not in files:
files.append(upload)
if upload not in pending:
Expand Down Expand Up @@ -354,7 +362,7 @@ def main(class_, argv=None):
return 0
if issubclass(class_, FileService):
for file in args.file:
uploader.upload(option=file)
uploader.upload(option=os.path.abspath(file))
else:
uploader.upload(catchup=args.catchup, test_mode=not args.catchup)
uploader.stop()
Expand Down
16 changes: 9 additions & 7 deletions src/pywws/service/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
[hourly]
text = [('toot.txt', 'L')]
plot = [('tweet.png.xml', 'L')]
services = [('mastodon', '/tmp/pywws/results/toot.txt')]
services = [('mastodon', 'toot.txt')]
Create an account
-----------------
Expand Down Expand Up @@ -99,8 +99,7 @@
other templates. The example template includes a ``media`` line to send
a graph. Either remove this or copy the example graph template
``tweet.png.xml`` to your graph templates directory, if you don't
already have one there, and edit the ``media`` line to use your
``local_files`` directory.
already have one there.
Now generate a toot file from your template, for example::
Expand All @@ -127,12 +126,12 @@
generate it once. Add your toot template to the ``text`` list, again
with an ``L`` flag to store the result locally. Finally, add
``mastodon`` to the ``services`` list, with an option specifying the
full path of the template processing result. For example::
template processing result. For example::
[hourly]
text = [('toot.txt', 'L')]
plot = [('tweet.png.xml', 'L')]
services = [('mastodon', '/tmp/pywws/results/toot.txt')]
services = [('mastodon', 'toot.txt')]
You could use the ``[logged]``, ``[12 hourly]`` or ``[daily]`` sections
instead, but I think ``[hourly]`` is most appropriate for Mastodon
Expand All @@ -143,8 +142,8 @@
Mastodon allows up to four images per "toot", so you could add more
graphs, or a webcam image, or a weather forecast icon. Use one ``media``
line per image at the start of your toot template and specify the full
path for each one.
line per image at the start of your toot template. You need to give the
full path of any files that are not in your ``local_files`` directory.
.. _botsin.space: https://botsin.space/
.. _Mastodon: https://joinmastodon.org/
Expand All @@ -156,6 +155,7 @@
import codecs
from contextlib import contextmanager
import logging
import os
import sys

from mastodon import Mastodon
Expand Down Expand Up @@ -197,6 +197,8 @@ def upload_file(self, session, filename):
while toot.startswith('media'):
media_item, toot = toot.split('\n', 1)
media_item = media_item.split()[1]
if not os.path.isabs(media_item):
media_item = os.path.join(self.local_dir, media_item)
media.append(media_item)
media_ids = []
try:
Expand Down
22 changes: 11 additions & 11 deletions src/pywws/service/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@
Edit the ``[hourly]`` section in ``weather.ini``. Add your tweet
template to the ``text`` list, with an ``L`` flag to store the result in
your ``local_files`` directory. Then add ``twitter`` to the ``services``
list, with an option specifying the full path of the template processing
result. For example::
list, with an option specifying the template processing result. For
example::
[hourly]
text = [('tweet.txt', 'L')]
services = [('twitter', '/tmp/pywws/results/tweet.txt')]
services = [('twitter', 'tweet.txt')]
You could use the ``[logged]``, ``[12 hourly]`` or ``[daily]`` sections
instead, but I think ``[hourly]`` is most appropriate for Twitter
Expand All @@ -154,9 +154,10 @@
You can add up to four images to your tweets by specifying the image
file locations in the tweet template. Make the first line of the tweet
``media path`` where ``path`` is the absolute location of the file.
Repeat for any additional image files. The "tweet_media.txt" example
template shows how to do this.
``media path`` where ``path`` is the file name, or full path for files
that are not in your ``local_files`` directory. Repeat for any
additional image files. The "tweet_media.txt" example template shows how
to do this.
The image could be from a web cam, or for a weather forecast it could be
an icon representing the forecast. To add a weather graph you need to
Expand All @@ -169,7 +170,7 @@
[hourly]
plot = [('tweet.png.xml', 'L')]
text = [('tweet_media.txt', 'L')]
services = [('twitter', '/tmp/pywws/results/tweet_media.txt')]
services = [('twitter', 'tweet_media.txt')]
.. _Twitter: https://twitter.com/
Expand All @@ -179,8 +180,8 @@

import codecs
from contextlib import contextmanager
from datetime import timedelta
import logging
import os
import sys

twitter = None
Expand All @@ -195,10 +196,7 @@
raise ex

from pywws.constants import Twitter as pct
import pywws.localisation
import pywws.logger
import pywws.service
import pywws.storage

__docformat__ = "restructuredtext en"
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -284,6 +282,8 @@ def upload_file(self, session, filename):
while tweet.startswith('media'):
media_item, tweet = tweet.split('\n', 1)
media_item = media_item.split()[1]
if not os.path.isabs(media_item):
media_item = os.path.join(self.local_dir, media_item)
media.append(media_item)
try:
session.post(tweet, media)
Expand Down

0 comments on commit 1a5bac2

Please sign in to comment.