From 03ff0f697a54de73f80341f92ebcb2111db2ce7e Mon Sep 17 00:00:00 2001 From: Tristan Cacqueray Date: Fri, 16 Dec 2016 01:21:19 +0000 Subject: [PATCH] Add config module This change is a follow-up to the previous refactor to address the nits. Change-Id: I2a648c581352b9bcaa69f82f08cd06ed8cbfaa85 --- openstack_election/cmds/create_directories.py | 2 - openstack_election/config.py | 38 +++++++++++++++++++ openstack_election/utils.py | 18 ++------- 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 openstack_election/config.py diff --git a/openstack_election/cmds/create_directories.py b/openstack_election/cmds/create_directories.py index c12c32eb..ed6654de 100755 --- a/openstack_election/cmds/create_directories.py +++ b/openstack_election/cmds/create_directories.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python -# # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at diff --git a/openstack_election/config.py b/openstack_election/config.py new file mode 100644 index 00000000..2116cc89 --- /dev/null +++ b/openstack_election/config.py @@ -0,0 +1,38 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import datetime +import pytz +import yaml + + +TIME_FMT = "%b %d, %Y %H:%M %Z" + + +# Load configuration.yaml and create datetime objects +def parse_datetime(iso_format): + date = datetime.datetime.strptime(iso_format, "%Y-%m-%dT%H:%M") + return date.replace(tzinfo=pytz.utc) + + +def load_conf(): + conf = yaml.safe_load(open('configuration.yaml')) + for key in ('start', 'end', 'email_deadline'): + date = parse_datetime(conf['timeframe'][key]) + conf['timeframe'][key] = date + conf['timeframe'][key+'_str'] = date.strftime(TIME_FMT) + for event in conf['timeline']: + for key in ('start', 'end'): + date = parse_datetime(event[key]) + event[key] = date + event[key+'_str'] = date.strftime(TIME_FMT) + return conf diff --git a/openstack_election/utils.py b/openstack_election/utils.py index ab959fcf..0e2c78d4 100644 --- a/openstack_election/utils.py +++ b/openstack_election/utils.py @@ -26,6 +26,8 @@ import time import urllib import yaml +from openstack_election import config + # Library constants CANDIDATE_PATH = 'candidates' @@ -34,22 +36,8 @@ ELECTION_REPO = 'openstack/election' CGIT_URL = 'https://git.openstack.org/cgit' PROJECTS_URL = ('%s/openstack/governance/plain/reference/projects.yaml' % (CGIT_URL)) -TIME_FMT = "%b %d, %Y %H:%M %Z" - -# Election configuration -conf = yaml.load(open('configuration.yaml')) - -# Convert time to datetime object -strptime = lambda x: datetime.datetime.strptime( - x, "%Y-%m-%dT%H:%M").replace(tzinfo=pytz.utc) -for key in ('start', 'end', 'email_deadline'): - conf['timeframe'][key] = strptime(conf['timeframe'][key]) - conf['timeframe'][key+'_str'] = conf['timeframe'][key].strftime(TIME_FMT) -for event in conf['timeline']: - for key in ('start', 'end'): - event[key] = strptime(event[key]) - event[key+'_str'] = event[key].strftime(TIME_FMT) +conf = config.load_conf() exceptions = None