From 3a54f3ca03943d5287e7b52ed967c7b2a7bbf6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Thu, 10 Jul 2014 10:11:48 +0200 Subject: [PATCH] config: using new ini system --- MANIFEST.in | 1 + mandaye_vincennes/config.py | 183 ++++++++++++++++----------- mandaye_vincennes/default-config.ini | 49 +++++++ 3 files changed, 158 insertions(+), 75 deletions(-) create mode 100644 mandaye_vincennes/default-config.ini diff --git a/MANIFEST.in b/MANIFEST.in index 6d06f76..28e794b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ include COPYING MANIFEST.in VERSION +include mandaye_vincennes/default-config.ini recursive-include mandaye_vincennes/templates *.html recursive-include mandaye_vincennes/static * diff --git a/mandaye_vincennes/config.py b/mandaye_vincennes/config.py index 660305a..a40f7d7 100644 --- a/mandaye_vincennes/config.py +++ b/mandaye_vincennes/config.py @@ -1,18 +1,38 @@ import logging import os -_PROJECT_PATH = os.path.join(os.path.dirname(__file__), '..') +from ConfigParser import SafeConfigParser +from mandaye.exceptions import ImproperlyConfigured + +# get configuration files from : +# 1. default-settings.ini from source code +# 2. os.environ.get('SETTINGS_INI') if it exists +# else /etc/mandaye-cam/config.ini +# and then /etc/mandaye-cam/local-config.ini +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +SETTINGS_INI = (os.path.join(BASE_DIR, 'default-config.ini'),) +if os.environ.get('SETTINGS_INI'): + SETTINGS_INI += (os.environ.get('SETTINGS_INI'),) +else: + ETC_DIR = os.path.join('/', 'etc', 'mandaye-vincennes') + SETTINGS_INI += ( + os.path.join(ETC_DIR, 'config.ini'), + os.path.join(ETC_DIR, 'local-config.ini') + ) + +config = SafeConfigParser() +config.read(SETTINGS_INI) ## SQL Backend config # Database configuration # http://docs.sqlalchemy.org/en/rel_0_7/core/engines.html # rfc 1738 https://tools.ietf.org/html/rfc1738 # dialect+driver://username:password@host:port/database -db_url = 'sqlite:///' + os.path.join(_PROJECT_PATH, 'test.db') +db_url = config.get('database', 'url') -debug = False +debug = config.getboolean('debug', 'debug') -## Log configuration +# Log configuration LOGGING = { 'version': 1, 'disable_existing_loggers': True, @@ -22,8 +42,9 @@ LOGGING = { 'format': '%(asctime)s %(levelname)s %(message)s', 'datefmt': '%H:%M:%S', }, - 'syslog': { - 'format': '%(name)s %(levelname)s %(uuid)s %(message)s', + 'file': { + 'format': '%(asctime)s %(levelname)s %(uuid)s %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S' } }, 'handlers': { @@ -33,100 +54,112 @@ LOGGING = { 'formatter': 'console' }, 'syslog': { - 'level': 'INFO', + 'level': 'DEBUG', 'class': 'entrouvert.logging.handlers.SysLogHandler', - 'formatter': 'syslog', + 'formatter': 'file', 'address': '/dev/log' }, }, - 'loggers': { - '': { - 'handlers': ['console'], - 'level': 'DEBUG', - 'propagate': False, - }, - 'mandaye': { - 'handlers': ['console', 'syslog'], - 'level': 'DEBUG', - 'propagate': False, - }, - 'mandaye_vincennes': { - 'handlers': ['console', 'syslog'], - 'level': 'DEBUG', - 'propagate': False, - }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'INFO', + 'propagate': False, }, - } + 'mandaye': { + 'handlers': ['console', 'syslog'], + 'level': 'INFO', + 'propagate': False, + }, + 'cam': { + 'handlers': ['console', 'syslog'], + 'level': 'INFO', + 'propagate': False, + }, + }, + } + +if config.getboolean('debug', 'log_debug'): + LOGGING['loggers']['']['level'] = 'DEBUG' + LOGGING['loggers']['mandaye']['level'] = 'DEBUG' + LOGGING['loggers']['cam']['level'] = 'DEBUG' ## PATH -# Template directory -template_directory = os.path.join(_PROJECT_PATH, 'mandaye_vincennes/templates') # Configuration directory -config_root = os.path.join(_PROJECT_PATH, 'conf.d') +config_root = config.get('dirs', 'config_root') +# Template directory +template_directory = os.path.join(BASE_DIR, 'templates') # Static url -static_url = '/mandaye/static' +static_url = config.get('dirs', 'static_url') # Static folder -static_root = os.path.join(_PROJECT_PATH, 'mandaye_vincennes/static') +static_root = config.get('dirs', 'static_root') # Data dir -data_dir = os.path.join(_PROJECT_PATH, 'data') - -# Raven Sentry configuration -raven_dsn = None - -# Email notification configuration -email_notification = False -email_prefix = '[Mandaye CAM]' -smtp_host = 'localhost' -smtp_port = 25 -email_from = 'traceback@entrouvert.com' -email_to = ['admin@localhost'] - -# Use long traceback with xtraceback -use_long_trace = True - -# Ask Mandaye to auto decompress a response message -# Decompress response only if you load a filter -auto_decompress = True -# Ask mandaye to add a toolbar -mandaye_toolbar = True -# Authentic 2 auto connection -a2_auto_connection = True - -# Encrypt service provider passwords with a secret -# You should install pycypto to use this feature -encrypt_sp_password = False -# Must be a 16, 24, or 32 bytes long -encrypt_secret = '' - -# Beaker session configuration -session_opts = { - 'session.type': 'file', - 'session.cookie_expires': True, - 'session.timeout': 3600, - 'session.data_dir': '/var/tmp/beaker' -} +data_dir = config.get('dirs', 'data_dir') # Supported authentification authentifications = { - 'saml2': 'mandaye.auth.saml2.SAML2Auth', - 'saml2_espace_famille': 'mandaye_vincennes.auth.espacefamille.EspaceFamilleAuth' + 'saml2': 'mandaye.auth.saml2.SAML2Auth' } # sp mappers mappers = { - 'biblio': 'mandaye_vincennes.mappers.biblio_vincennes', - 'duonet': 'mandaye_vincennes.mappers.duonet_vincennes', - 'espace_famille': 'mandaye_vincennes.mappers.famille_vincennes', -} + 'biblio': 'mandaye_vincennes.mappers.biblio_vincennes', + 'duonet': 'mandaye_vincennes.mappers.duonet_vincennes', + 'espace_famille': 'mandaye_vincennes.mappers.famille_vincennes', + } + +# Raven Sentry configuration +raven_dsn = config.get('debug', 'sentry_dsn') + +# Email notification configuration +email_notification = config.getboolean('email', 'notification') +email_prefix = config.get('email', 'prefix') +smtp_host = config.get('email', 'smtp_host') +smtp_port = config.getint('email', 'smtp_port') +email_from = config.get('email', 'from') +email_to = config.get('email', 'to').split() + +# Use long traceback with xtraceback +use_long_trace = config.getboolean('debug', 'use_long_trace') +# Ask Mandaye to auto decompress a response message +# Decompress response only if you load a filter +auto_decompress = config.getboolean('mandaye', 'auto_decompress') +# Ask mandaye to add a toolbar with Mandaye's links +mandaye_toolbar = config.getboolean('mandaye', 'toolbar') +# Authentic 2 auto connection +a2_auto_connection = config.getboolean('mandaye', 'a2_auto_connection') # Choose storage # Only mandaye.backends.sql at the moment -storage_backend = "mandaye.backends.sql" +if config.get('mandaye', 'storage_backend') == 'sql': + storage_backend = "mandaye.backends.sql" +else: + ImproperlyConfigured('Storage backend must be sql') +# Encrypt service provider passwords with a secret +# You should install pycypto to use this feature +encrypt_sp_password = config.getboolean('mandaye', 'encrypt_sp_password') +# Must be a 15, 24, or 32 bytes long +encrypt_secret = config.get('mandaye', 'encrypt_secret') + +session_type = config.get('session', 'type') +if session_type not in ('file', 'dbm', 'memory', 'memcached'): + raise ImproperlyConfigured('Sesssion type %r not supported' % session_type) +if session_type == 'memcached': + session_type = 'ext:memcached' + +# Beaker session configuration +session_opts = { + 'session.type': session_type, + 'session.url': config.get('session', 'url'), + 'session.cookie_expires': config.getboolean('session', 'cookie_expires'), + 'session.timeout': config.getint('session', 'timeout'), + 'session.data_dir': config.get('session', 'data_dir') +} # Import local config try: - from mandaye_vincennes.local_config import * + from cam.local_config import * except ImportError, e: if not 'local_config' in e.args[0]: raise ImproperlyConfigured('Error while importing "local_config.py"') diff --git a/mandaye_vincennes/default-config.ini b/mandaye_vincennes/default-config.ini new file mode 100644 index 0000000..9c0b0a2 --- /dev/null +++ b/mandaye_vincennes/default-config.ini @@ -0,0 +1,49 @@ +[DEFAULT] +base_dir: . + +[database] +; http://docs.sqlalchemy.org/en/rel_0_8/core/engines.html +url: sqlite:///%(base_dir)s/{project_name}.db + +[dirs] +config_root: %(base_dir)s/conf.d +data_dir: %(base_dir)s/data +static_root: %(base_dir)s/{project_name}/static +static_url: /mandaye/static + +[debug] +debug: false +use_long_trace: true +log_debug: false +; you need to install python-raven for this feature +sentry_dsn: + +[mandaye] +toolbar: true +a2_auto_connection: true +; only sql at the moment +storage_backend: sql +auto_decompress: true +; if you want to encypt password set to true +; you need to install pycrypto for this feature +encrypt_sp_password: false +; if encrypt_sp_password then you need to choose a secret +; must be a 16, 24, or 32 bytes long +encrypt_secret: + +[session] +; file, dbm, memory or memcached +; if memcached you need to install python-memcached and memcached +type: memcached +url: 127.0.0.1:11211 +cookie_expires: true +timeout: 3600 +data_dir: %(base_dir)s/data + +[email] +notification: false +prefix: [Mandaye CAM] +smtp_host: localhost +smtp_port: 25 +from: traceback@entrouvert.com +to: admin+mandaye-{project_name}@entrouvert.com