Skip to content

Commit

Permalink
Move weather.ini auto updates to pywws.storage
Browse files Browse the repository at this point in the history
Also added a version number to the weather.ini file so the updates are
only tried once.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 29, 2018
1 parent 90907f6 commit 9668be3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 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 = '1605'
_commit = '71125a9'
_release = '1606'
_commit = '90907f6'
7 changes: 2 additions & 5 deletions src/pywws/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,8 @@ def generate_monthly(rain_day_threshold, day_end_hour, use_dst,

def get_day_end_hour(params):
# get daytime end hour (in local time)
day_end_str = params.get('config', 'day end hour', '9, False')
if not ',' in day_end_str:
day_end_str += ', False'
params.set('config', 'day end hour', day_end_str)
day_end_hour, use_dst = eval(day_end_str)
day_end_hour, use_dst = eval(
params.get('config', 'day end hour', '9, False'))
day_end_hour = day_end_hour % 24
return day_end_hour, use_dst

Expand Down
47 changes: 0 additions & 47 deletions src/pywws/regulartasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,53 +79,6 @@ def __init__(self, context):
last_update += timezone.utcoffset(last_update)
while self.cron[section].get_current(datetime) <= last_update:
self.cron[section].get_next()
# convert to use pywws.service.{copy,ftp,sftp,twitter}
if self.params.get('ftp', 'local site') == 'True':
self.params.set(
'copy', 'directory', self.params.get('ftp', 'directory', ''))
mod = 'copy'
elif self.params.get('ftp', 'secure') == 'True':
for key in ('site', 'user', 'directory', 'port',
'password', 'privkey'):
self.params.set('sftp', key, self.params.get('ftp', key, ''))
mod = 'sftp'
else:
mod = 'ftp'
for key in ('local site', 'secure', 'privkey'):
self.params.unset('ftp', key)
for section in list(self.cron.keys()) + [
'live', 'logged', 'hourly', '12 hourly', 'daily']:
for t_p in ('text', 'plot'):
templates = eval(self.params.get(section, t_p, '[]'))
services = eval(self.params.get(section, 'services', '[]'))
changed = False
for n, template in enumerate(templates):
if isinstance(template, (list, tuple)):
template, flags = template
else:
flags = ''
if 'T' in flags:
templates[n] = template
changed = True
if t_p == 'plot':
result = os.path.splitext(template)[0]
else:
result = template
if 'L' in flags:
task = None
elif 'T' in flags:
task = ('twitter', result)
elif any([x[1] == result for x in services]):
task = None
else:
task = (mod, result)
if task and task not in services:
services.append(task)
changed = True
if changed:
logger.error('updating %s in [%s]', t_p, section)
self.params.set(section, t_p, repr(templates))
self.params.set(section, 'services', repr(services))
# create service uploader objects
self.services = {}
for section in list(self.cron.keys()) + [
Expand Down
63 changes: 63 additions & 0 deletions src/pywws/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
else:
from ConfigParser import RawConfigParser

from pywws import _release
from pywws.constants import DAY
from pywws.weatherstation import WSDateTime, WSFloat, WSInt, WSStatus

Expand Down Expand Up @@ -773,6 +774,8 @@ 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')
# update weather.ini
self.update_params()
# create working directories
self.work_dir = self.params.get('paths', 'work', '/tmp/pywws')
self.output_dir = os.path.join(self.work_dir, 'output')
Expand All @@ -787,6 +790,66 @@ def __init__(self, data_dir, live_logging):
# create an event to shutdown threads
self.shutdown = threading.Event()

def update_params(self):
current_version = int(_release)
file_version = int(self.params.get('config', 'config version', '0'))
if file_version < 1488:
# convert day end hour
day_end_str = self.params.get('config', 'day end hour')
if day_end_str and not ',' in day_end_str:
logger.error('updating "day end hour" in weather.ini')
day_end_str += ', False'
self.params.set('config', 'day end hour', day_end_str)
if file_version < 1582:
# convert uploads to use pywws.service.{copy,ftp,sftp,twitter}
if self.params.get('ftp', 'local site') == 'True':
self.params.set(
'copy', 'directory', self.params.get('ftp', 'directory', ''))
mod = 'copy'
elif self.params.get('ftp', 'secure') == 'True':
for key in ('site', 'user', 'directory', 'port',
'password', 'privkey'):
self.params.set('sftp', key, self.params.get('ftp', key, ''))
mod = 'sftp'
else:
mod = 'ftp'
for key in ('local site', 'secure', 'privkey'):
self.params.unset('ftp', key)
for section in self.params._config.sections():
if section.split()[0] != 'cron' and section not in [
'live', 'logged', 'hourly', '12 hourly', 'daily']:
continue
for t_p in ('text', 'plot'):
templates = eval(self.params.get(section, t_p, '[]'))
services = eval(self.params.get(section, 'services', '[]'))
changed = False
for n, template in enumerate(templates):
if isinstance(template, (list, tuple)):
template, flags = template
templates[n] = template
changed = True
else:
flags = ''
if t_p == 'plot':
result = os.path.splitext(template)[0]
else:
result = template
if 'L' in flags:
task = None
elif 'T' in flags:
task = ('twitter', result)
else:
task = (mod, result)
if task and task not in services:
services.append(task)
changed = True
if changed:
logger.error('updating %s in [%s]', t_p, section)
self.params.set(section, t_p, repr(templates))
self.params.set(section, 'services', repr(services))
if file_version < current_version:
self.params.set('config', 'config version', str(current_version))

def terminate(self):
if self.live_logging:
# signal threads to terminate
Expand Down

4 comments on commit 9668be3

@ashenshugarRET
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear Jim,

Would it be better to put the software version flag in the status.ini file rather than the weather.ini file? My rationale is that the weather.ini file is user editable and flag might be accidentally deleted / corrupted, whereas the status.ini file is rarely opened / edited by the user.

Kind Regards

Richard

@jim-easterbrook
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is that the version is the weather.ini file version, so if an old one was recovered from backup the auto edits would be done again. I'm also likely to tell people to delete their status.ini as a cure for some problems.

@ashenshugarRET
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that sounds very reasonable.

@jim-easterbrook
Copy link
Owner Author

@jim-easterbrook jim-easterbrook commented on 9668be3 Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm now veering towards removing the use of a version number, and back to inferring the need for conversion from what's in the file. The version number stuff is just too flaky.

Please sign in to comment.