Add config module

This change is a follow-up to the previous refactor to address the nits.

Change-Id: I2a648c581352b9bcaa69f82f08cd06ed8cbfaa85
This commit is contained in:
Tristan Cacqueray 2016-12-16 01:21:19 +00:00
parent dd748cf296
commit 03ff0f697a
3 changed files with 41 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -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