Skip to content

Commit

Permalink
Replace local_files with subdir of work directory
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 23, 2018
1 parent 32c86b8 commit d2d51a1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 66 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 = '1584'
_commit = '5c989ad'
_release = '1585'
_commit = '32c86b8'
51 changes: 11 additions & 40 deletions src/pywws/regulartasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,20 @@ def __init__(self, context):
self.monthly_data = context.monthly_data
self.flush = eval(self.params.get('config', 'frequent writes', 'False'))
# get directories
self.work_dir = self.params.get('paths', 'work', '/tmp/pywws')
if not os.path.isdir(self.work_dir):
os.makedirs(self.work_dir)
self.template_dir = self.params.get(
'paths', 'templates', os.path.expanduser('~/weather/templates/'))
self.graph_template_dir = self.params.get(
'paths', 'graph_templates', os.path.expanduser('~/weather/graph_templates/'))
self.local_dir = self.params.get(
'paths', 'local_files', os.path.expanduser('~/weather/results/'))
self.module_dir = self.params.get(
'paths', 'modules', os.path.expanduser('~/weather/modules/'))
# create calibration object
self.calibrator = Calib(self.params, self.raw_data)
# create templater object
self.templater = pywws.template.Template(context)
# create plotter objects
self.plotter = pywws.plot.GraphPlotter(context, self.work_dir)
self.roseplotter = pywws.windrose.RosePlotter(context, self.work_dir)
# create FTP uploads directory
self.uploads_directory = os.path.join(self.work_dir, 'uploads')
if not os.path.isdir(self.uploads_directory):
os.makedirs(self.uploads_directory)
self.plotter = pywws.plot.GraphPlotter(context, self.context.work_dir)
self.roseplotter = pywws.windrose.RosePlotter(
context, self.context.work_dir)
# get daytime end hour
self.day_end_hour, self.use_dst = pywws.process.get_day_end_hour(
self.params)
Expand Down Expand Up @@ -122,7 +114,7 @@ def __init__(self, context):
task = None
elif 'T' in flags:
task = ('twitter', result)
elif any([x[1] == template for x in services]):
elif any([x[1] == result for x in services]):
task = None
else:
task = (mod, result)
Expand Down Expand Up @@ -154,21 +146,6 @@ def __init__(self, context):
if self.params.get(section, 'yowindow'):
logger.error(
'Obsolete yowindow entry in weather.ini [%s]', section)
# check for 'local' template results
if os.path.isdir(self.local_dir):
return
has_local = False
for section in list(self.cron.keys()) + [
'live', 'logged', 'hourly', '12 hourly', 'daily']:
for template, flags in self._parse_templates(section, 'text'):
if 'L' in flags:
has_local = True
for template, flags in self._parse_templates(section, 'plot'):
if 'L' in flags:
has_local = True
if has_local:
os.makedirs(self.local_dir)
break

def has_live_tasks(self):
if self.cron:
Expand Down Expand Up @@ -206,10 +183,10 @@ def _do_common(self, now, sections, live_data=None):
plot_tasks.append(task)
# do plot templates
for template, flags in plot_tasks:
self.do_plot(template, local='L' in flags)
self.do_plot(template)
# do text templates
for template, flags in text_tasks:
self.do_template(template, data=live_data, local='L' in flags)
self.do_template(template, data=live_data)
# do service tasks
for name in self.services:
self.services[name].do_catchup()
Expand Down Expand Up @@ -285,14 +262,11 @@ def do_tasks(self):
for name in self.services:
self.services[name].stop()

def do_plot(self, template, local=False):
def do_plot(self, template):
logger.info("Graphing %s", template)
input_file = os.path.join(self.graph_template_dir, template)
output_file = os.path.splitext(template)[0]
if local:
output_file = os.path.join(self.local_dir, output_file)
else:
output_file = os.path.join(self.uploads_directory, output_file)
output_file = os.path.join(
self.context.output_dir, os.path.splitext(template)[0])
input_xml = pywws.plot.GraphFileReader(input_file)
if (input_xml.get_children(self.plotter.plot_name) and
self.plotter.do_plot(input_xml, output_file) == 0):
Expand All @@ -303,12 +277,9 @@ def do_plot(self, template, local=False):
logger.warning('nothing to graph in %s', input_file)
return None

def do_template(self, template, data=None, local=False):
def do_template(self, template, data=None):
logger.info("Templating %s", template)
input_file = os.path.join(self.template_dir, template)
if local:
output_file = os.path.join(self.local_dir, template)
else:
output_file = os.path.join(self.uploads_directory, template)
output_file = os.path.join(self.context.output_dir, template)
self.templater.make_file(input_file, output_file, live_data=data)
return output_file
7 changes: 1 addition & 6 deletions src/pywws/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,6 @@ 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 Down Expand Up @@ -248,7 +243,7 @@ def upload_batch(self):
files.append(path)
else:
if not os.path.isabs(upload):
upload = os.path.join(self.local_dir, upload)
upload = os.path.join(self.context.output_dir, upload)
if upload not in files:
files.append(upload)
if upload not in pending:
Expand Down
15 changes: 7 additions & 8 deletions src/pywws/service/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@
Edit the ``[hourly]`` section in ``weather.ini``. If your toots include
one or more graphs you need to add the graph templates to the ``plot``
list, with an ``L`` flag to store the result in your ``local_files``
directory. Note that if you reuse your Twitter graph you only need to
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
list. Note that if you reuse your Twitter graph you only need to
generate it once. Add your toot template to the ``text`` list. Finally,
add ``mastodon`` to the ``services`` list, with an option specifying the
template processing result. For example::
[hourly]
text = [('toot.txt', 'L')]
text = ['toot.txt']
plot = [('tweet.png.xml', 'L')]
services = [('mastodon', 'toot.txt')]
Expand All @@ -143,7 +141,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. You need to give the
full path of any files that are not in your ``local_files`` directory.
full path of any files that are not in your "output" directory (a
subdirectory of your work directory called ``output``).
.. _botsin.space: https://botsin.space/
.. _Mastodon: https://joinmastodon.org/
Expand Down Expand Up @@ -199,7 +198,7 @@ def upload_file(self, session, filename):
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_item = os.path.join(self.context.output_dir, media_item)
media.append(media_item)
media_ids = []
try:
Expand Down
19 changes: 9 additions & 10 deletions src/pywws/service/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,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``
template to the ``text`` list. Then add ``twitter`` to the ``services``
list, with an option specifying the template processing result. For
example::
[hourly]
text = [('tweet.txt', 'L')]
text = ['tweet.txt']
services = [('twitter', 'tweet.txt')]
You could use the ``[logged]``, ``[12 hourly]`` or ``[daily]`` sections
Expand All @@ -155,21 +154,21 @@
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 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.
that are not in your "output" directory (a subdirectory of your work
directory called ``output``). 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
make sure the graph is drawn before the tweet is sent. The
:py:mod:`pywws.regulartasks` module processes graph and text templates
before doing service uploads, so you can include the graph drawing in
the same section. The ``L`` flag ensures the plot is stored in your
local files directory. For example::
the same section. (The ``L`` flag stops auto-editing of weather.ini
adding the file as an ftp task). For example::
[hourly]
plot = [('tweet.png.xml', 'L')]
text = [('tweet_media.txt', 'L')]
text = ['tweet_media.txt']
services = [('twitter', 'tweet_media.txt')]
.. _Twitter: https://twitter.com/
Expand Down Expand Up @@ -284,7 +283,7 @@ def upload_file(self, session, filename):
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_item = os.path.join(self.context.output_dir, media_item)
media.append(media_item)
try:
session.post(tweet, media)
Expand Down
5 changes: 5 additions & 0 deletions src/pywws/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,11 @@ def __init__(self, data_dir, live_logging):
# open params and status files
self.params = ParamStore(data_dir, 'weather.ini')
self.status = ParamStore(data_dir, 'status.ini')
# create working directories
self.work_dir = self.params.get('paths', 'work', '/tmp/pywws')
self.output_dir = os.path.join(self.work_dir, 'output')
if not os.path.isdir(self.output_dir):
os.makedirs(self.output_dir)
# open data file stores
self.raw_data = RawStore(data_dir)
self.calib_data = CalibStore(data_dir)
Expand Down

0 comments on commit d2d51a1

Please sign in to comment.