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:
parent
dd748cf296
commit
03ff0f697a
@ -1,5 +1,3 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# 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
|
# not use this file except in compliance with the License. You may obtain
|
||||||
# a copy of the License at
|
# a copy of the License at
|
||||||
|
38
openstack_election/config.py
Normal file
38
openstack_election/config.py
Normal 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
|
@ -26,6 +26,8 @@ import time
|
|||||||
import urllib
|
import urllib
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from openstack_election import config
|
||||||
|
|
||||||
|
|
||||||
# Library constants
|
# Library constants
|
||||||
CANDIDATE_PATH = 'candidates'
|
CANDIDATE_PATH = 'candidates'
|
||||||
@ -34,22 +36,8 @@ ELECTION_REPO = 'openstack/election'
|
|||||||
CGIT_URL = 'https://git.openstack.org/cgit'
|
CGIT_URL = 'https://git.openstack.org/cgit'
|
||||||
PROJECTS_URL = ('%s/openstack/governance/plain/reference/projects.yaml' %
|
PROJECTS_URL = ('%s/openstack/governance/plain/reference/projects.yaml' %
|
||||||
(CGIT_URL))
|
(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
|
exceptions = None
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user