Repository : http://git.fedorahosted.org/cgit/fedocal.git
On branch : master
commit ceead1674c08583cf8a92e743ac56088529824ab Author: Pierre-Yves Chibon pingou@pingoured.fr Date: Sat Nov 17 19:46:26 2012 +0100
Move the configuration to the Flask configuration model
Flask recommands to use an environment variable to point to the actual configuration file which overloads the defaults present in the default_config.py within the application.
This commits does this change.
The run_tests.sh shell script points to its own (new) configuration file.
Fixes #8
fedocal.cfg.sample | 12 ++++++++++++ fedocal/__init__.py | 15 ++++----------- fedocal/default_config.py | 31 +++++++++++++++++++++++++++++++ fedocal/fedocal.cfg.sample | 9 --------- fedocal/tests/fedocal_test.cfg | 8 ++++++++ fedocal/tests/test_flask.py | 6 ++---- run_tests.sh | 2 +- 7 files changed, 58 insertions(+), 25 deletions(-)
diff --git a/fedocal.cfg.sample b/fedocal.cfg.sample new file mode 100644 index 0000000..d68897d --- /dev/null +++ b/fedocal.cfg.sample @@ -0,0 +1,12 @@ +# Beware that the quotes around the values are mandatory + +### Secret key for the Flask application +SECRET_KEY='<The web application secret key>' + +### url to the database server: +#DB_URL=mysql://user:pass@host/db_name +#DB_URL=postgres://user:pass@host/db_name +DB_URL='sqlite:////tmp/fedocal_dev.sqlite' + +### The FAS group in which the admin of fedocal are +ADMIN_GROUP='fedocal_admin' diff --git a/fedocal/__init__.py b/fedocal/__init__.py index a24fc10..ac0b947 100644 --- a/fedocal/__init__.py +++ b/fedocal/__init__.py @@ -45,20 +45,13 @@ import fedocallib.dbaction from fedocallib.exceptions import FedocalException from fedocallib.model import (Calendar, Meeting, Reminder)
-CONFIG = ConfigParser.ConfigParser() -if os.path.exists('/etc/fedocal.cfg'): # pragma: no cover - CONFIG.readfp(open('/etc/fedocal.cfg')) -else: - CONFIG.readfp(open(os.path.join(os.path.dirname( - os.path.abspath(__file__)), - 'fedocal.cfg'))) - # Create the application. APP = flask.Flask(__name__) # set up FAS FAS = FAS(APP) -APP.secret_key = CONFIG.get('fedocal', 'secret_key') -SESSION = fedocallib.create_session(CONFIG.get('fedocal', 'db_url')) +APP.config.from_object('fedocal.default_config') +APP.config.from_envvar('FEDOCAL_CONFIG') +SESSION = fedocallib.create_session(APP.config['DB_URL'])
@APP.context_processor @@ -94,7 +87,7 @@ def is_admin(): if not flask.g.fas_user: return False else: - if CONFIG.get('fedocal', 'admin_group') in flask.g.fas_user.groups: + if APP.config['ADMIN_GROUP'] in flask.g.fas_user.groups: return True
diff --git a/fedocal/default_config.py b/fedocal/default_config.py new file mode 100644 index 0000000..7648448 --- /dev/null +++ b/fedocal/default_config.py @@ -0,0 +1,31 @@ +#-*- coding: UTF-8 -*- + +""" + (c) 2012 - Copyright Pierre-Yves Chibon pingou@pingoured.fr + + Distributed under License GPLv3 or later + You can find a copy of this license on the website + http://www.gnu.org/licenses/gpl.html + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. +""" + + +# url to the database server: +DB_URL='sqlite:////tmp/fedocal_dev.sqlite' + +# The FAS group in which the admin of fedocal are +ADMIN_GROUP='fedocal_admin' diff --git a/fedocal/fedocal.cfg.sample b/fedocal/fedocal.cfg.sample deleted file mode 100644 index 8893e9e..0000000 --- a/fedocal/fedocal.cfg.sample +++ /dev/null @@ -1,9 +0,0 @@ -[fedocal] -# Secret key for the Flask application -secret_key=<The web application secret key> -# url to the database server: -;db_url=mysql://user:pass@host/db_name -;db_url=postgres://user:pass@host/db_name -db_url=sqlite:////tmp/fedocal_dev.sqlite -# The FAS group in which the admin of fedocal are -admin_group=fedocal_admin diff --git a/fedocal/tests/fedocal_test.cfg b/fedocal/tests/fedocal_test.cfg new file mode 100644 index 0000000..7a8406d --- /dev/null +++ b/fedocal/tests/fedocal_test.cfg @@ -0,0 +1,8 @@ +### Secret key for the Flask application +SECRET_KEY='<The web application secret key>' + +### url to the database server: +DB_URL='sqlite:////tmp/fedocal_dev.sqlite' + +### The FAS group in which the admin of fedocal are +ADMIN_GROUP='packager' diff --git a/fedocal/tests/test_flask.py b/fedocal/tests/test_flask.py index 677db15..4f446dd 100644 --- a/fedocal/tests/test_flask.py +++ b/fedocal/tests/test_flask.py @@ -193,8 +193,7 @@ class Flasktests(Modeltests): with app.test_request_context(): flask.g.fas_user = None self.assertFalse(fedocal.is_admin()) - flask.g.fas_user = FakeUser(fedocal.CONFIG.get('fedocal', - 'admin_group')) + flask.g.fas_user = FakeUser(fedocal.APP.config['ADMIN_GROUP']) self.assertTrue(fedocal.is_admin())
def test_get_timezone(self): @@ -202,8 +201,7 @@ class Flasktests(Modeltests): app = flask.Flask('fedocal')
with app.test_request_context(): - flask.g.fas_user = FakeUser(fedocal.CONFIG.get('fedocal', - 'admin_group')) + flask.g.fas_user = FakeUser(fedocal.APP.config['ADMIN_GROUP']) self.assertEqual(fedocal.get_timezone(), 'Europe/Paris')
diff --git a/run_tests.sh b/run_tests.sh index adf2b09..101b71c 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,2 +1,2 @@ #!/bin/bash -PYTHONPATH=fedocal nosetests --with-coverage --cover-package=fedocal +FEDOCAL_CONFIG=tests/fedocal_test.cfg PYTHONPATH=fedocal nosetests --with-coverage --cover-package=fedocal
fedocal-devel@lists.fedorahosted.org