Skip to content

Commit

Permalink
Allow service modules to prepare data
Browse files Browse the repository at this point in the history
This is useful for any service where simple templating isn't good
enough.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed May 16, 2018
1 parent e5e311b commit f3856a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 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.5.0'
_release = '1528'
_commit = '10bccbf'
_release = '1529'
_commit = 'e5e311b'
15 changes: 9 additions & 6 deletions src/pywws/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def __init__(self, context):
'obsolete item "template" found in weather.ini '
'section [{}]'.format(self.service_name))
# create templater
self.templater = pywws.template.Template(context, use_locale=False)
self.template_file = StringIO(self.template)
if self.template:
self.templater = pywws.template.Template(context, use_locale=False)
self.template_file = StringIO(self.template)
# set timestamp of first data to upload
earliest = self.context.calib_data.before(datetime.max) - max(
timedelta(days=self.catchup), self.interval)
Expand All @@ -161,10 +162,7 @@ def upload(self, catchup=True, live_data=None, test_mode=False):
timestamp = data['idx']
if test_mode:
timestamp = None
# convert data
data_str = self.templater.make_text(self.template_file, data)
self.template_file.seek(0)
prepared_data = eval('{' + data_str + '}')
prepared_data = self.prepare_data(data)
prepared_data.update(self.fixed_data)
self.upload_thread.queue.append(
(timestamp, {'prepared_data': prepared_data, 'live': live}))
Expand All @@ -173,6 +171,11 @@ def upload(self, catchup=True, live_data=None, test_mode=False):
if self.upload_thread.queue and not self.upload_thread.is_alive():
self.upload_thread.start()

def prepare_data(self, data):
data_str = self.templater.make_text(self.template_file, data)
self.template_file.seek(0)
return eval('{' + data_str + '}')

def next_data(self, catchup, live_data):
if catchup:
start = self.next_update
Expand Down

0 comments on commit f3856a1

Please sign in to comment.