diff --git a/alembic/env.py b/alembic/env.py index 1db1ebb..4c1996f 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -17,8 +17,8 @@ from mandaye.models import Base target_metadata = Base.metadata # Import db url form mandaye -from mandaye.config import db_url -config.set_main_option("sqlalchemy.url", db_url) +from mandaye import config as mandaye_config +config.set_main_option("sqlalchemy.url", mandaye_config.db_url) # other values from the config, defined by the needs of env.py, # can be acquired: diff --git a/mandaye/__init__.py b/mandaye/__init__.py index 61acab0..f21c666 100644 --- a/mandaye/__init__.py +++ b/mandaye/__init__.py @@ -1 +1,19 @@ -VERSION=0.3 +VERSION=0.4 + +import os + +from mandaye import global_config +from mandaye.exceptions import ImproperlyConfigured + +ENVIRONMENT_VARIABLE = "MANDAYE_CONFIG_MODULE" +config = None + +if os.environ.has_key(ENVIRONMENT_VARIABLE): + try: + mod = import_module(path) + except ImportError, e: + raise ImproperlyConfigured('Error importing config %s: "%s"' % (path, e)) + config = mod +else: + config = global_config + diff --git a/mandaye/auth/saml2.py b/mandaye/auth/saml2.py index 23c3161..7c2eb39 100644 --- a/mandaye/auth/saml2.py +++ b/mandaye/auth/saml2.py @@ -94,7 +94,7 @@ class SAML2Auth(AuthForm): return _500('sso', 'Unable to find metadata.') metadata_file = open(metadata_file_path, 'a+') metadata_file.write(metadata) - metadata.close() + metadata_file.close() return metadata_file_path def _get_metadata(self, env): diff --git a/mandaye/backends/default.py b/mandaye/backends/default.py index 0d0d779..e8036f2 100644 --- a/mandaye/backends/default.py +++ b/mandaye/backends/default.py @@ -1,7 +1,7 @@ from importlib import import_module -from mandaye.config import storage_backend +from mandaye import config from mandaye.exceptions import ImproperlyConfigured class DefaultManagerIDPUser: @@ -85,7 +85,7 @@ def import_backend(path): raise ImproperlyConfigured('Error importing backend %s: "%s"' % (path, e)) return mod -backend = import_backend(storage_backend) +backend = import_backend(config.storage_backend) ManagerServiceProvider = backend.ManagerServiceProvider ManagerIDPUser = backend.ManagerIDPUser ManagerSPUser = backend.ManagerSPUser diff --git a/mandaye/configs/linuxfr_saml_example.py b/mandaye/configs/linuxfr_saml_example.py index 56e3960..3917eda 100644 --- a/mandaye/configs/linuxfr_saml_example.py +++ b/mandaye/configs/linuxfr_saml_example.py @@ -12,10 +12,8 @@ form_values = { } auth = SAML2Auth(form_values, 'linuxfr', saml2_config) -saml_mapping = auth.get_default_mapper() -linuxfr_mapping = saml_mapping.extend( - [ +linuxfr_mapping = [ { 'path': r'/mandaye/associate$', 'method': 'GET', @@ -45,6 +43,5 @@ linuxfr_mapping = saml_mapping.extend( ] }, ] - ) - +linuxfr_mapping.extend(auth.get_default_mapping()) diff --git a/mandaye/db.py b/mandaye/db.py index 3c94729..646b761 100644 --- a/mandaye/db.py +++ b/mandaye/db.py @@ -1,9 +1,18 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session -from mandaye.config import db_url -if not "sqlite" in db_url: - sql_session = scoped_session(sessionmaker(bind=create_engine(db_url, pool_size=16, - pool_recycle=1800))) +from mandaye import config + +if not "sqlite" in config.db_url: + sql_session = scoped_session( + sessionmaker( + bind=create_engine(config.db_url, pool_size=16, + pool_recycle=1800) + ) + ) else: - sql_session = scoped_session(sessionmaker(bind=create_engine(db_url))) + sql_session = scoped_session( + sessionmaker( + bind=create_engine(config.db_url) + ) + ) diff --git a/mandaye/filters/vincennes.py b/mandaye/filters/vincennes.py index dc065c7..55afdaf 100644 --- a/mandaye/filters/vincennes.py +++ b/mandaye/filters/vincennes.py @@ -11,7 +11,7 @@ from mandaye.models import Site, ExtUser, LocalUser from mandaye.response import _302, _401 from mandaye.template import serve_template -from mandaye.config.backend import ManagerSPUser +from mandaye.backend.default import ManagerSPUser def get_associate_form(env, values): """ Return association template content diff --git a/mandaye/config.py b/mandaye/global_config.py similarity index 53% rename from mandaye/config.py rename to mandaye/global_config.py index e975c82..b347f3f 100644 --- a/mandaye/config.py +++ b/mandaye/global_config.py @@ -1,6 +1,7 @@ -import logging -from mandaye.exceptions import ImproperlyConfigured +import os + +_PROJECT_PATH = os.path.join(os.path.dirname(__file__), '..') # Choose storage # Only mandaye.backends.sql at the moment @@ -17,9 +18,10 @@ keyfile = '' certfile = '' # Log configuration -debug = True +import logging +debug = False syslog = False -log_file = '/var/log/mandaye/mandaye.log' +log_file = os.path.join(_PROJECT_PATH, 'mandaye/mandaye.log') log_level = logging.INFO # Log rotation @@ -31,12 +33,11 @@ log_interval = 1 log_backup = 52 # Template directory -template_directory = 'mandaye/templates' +template_directory = os.path.join(_PROJECT_PATH, 'mandaye/templates') # Static folder -static_root = 'mandaye/static' - +static_root = os.path.join(_PROJECT_PATH, 'mandaye/static') # Data dir -data_dir = '/var/tmp' +data_dir = os.path.join(_PROJECT_PATH, 'data') # Email notification configuration email_notification = False @@ -59,37 +60,18 @@ encrypt_sp_password = False encrypt_secret = '' hosts = { - 'proxy.roleid.lan:8088': [ + 'linuxfr.local:8000': [ {'path': r'/', - 'target': 'http://perdu.com', - 'mapping': None, - 'local': 'test' - }, + 'target': 'http://linuxfr.org', + 'mapping': 'mandaye.configs.linuxfr_saml_example.linuxfr_mapping' + }, + { 'path': r'/static', + 'static': static_root + } ], } -#example_mapping = [ -# { -# 'path': r'/' -# 'on_response': [{ -# 'filter': 'module.path.to.my_filter', -# 'values': {} -# }] -# }, -# { -# 'path': r'/test$' -# 'method': 'GET', -# 'content-types': ['text/html', 'text/plain'], -# 'response': [{ -# 'filter': 'my_test', -# 'values': { -# 'toto': 'titi', -# } -# }] -# }, -#] - -# beaker session +# beaker session configuration session_opts = { 'session.type': 'file', 'session.cookie_expires': True, @@ -97,14 +79,3 @@ session_opts = { 'session.data_dir': '/var/tmp/beaker' } -# token timeout -# timeout in seconds -token_timeout = 60 - -# Import local settings -try: - from mandaye.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/log.py b/mandaye/log.py index d18d382..8131835 100644 --- a/mandaye/log.py +++ b/mandaye/log.py @@ -1,23 +1,25 @@ import logging + from logging.handlers import SysLogHandler, TimedRotatingFileHandler -from mandaye.config import log_level, log_file, syslog -from mandaye.config import log_when, log_interval, log_backup + +from mandaye import config logger = logging.getLogger('mandaye') -logger.setLevel(log_level) +logger.setLevel(config.log_level) # Syslog logging -if syslog: +if config.syslog: syslog_handler = SysLogHandler(address='/dev/log') logger.addHandler(syslog_handler) else: syslog_handler = None # File logging -if log_file: - filehandler = TimedRotatingFileHandler(log_file, when=log_when, - interval=log_interval, backupCount=log_backup) +if config.log_file: + filehandler = TimedRotatingFileHandler(config.log_file, + when=config.log_when, + interval=config.log_interval, backupCount=config.log_backup) filehandler.setFormatter( logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')) diff --git a/mandaye/response.py b/mandaye/response.py index 7215ae7..e45ab2f 100644 --- a/mandaye/response.py +++ b/mandaye/response.py @@ -6,15 +6,15 @@ import xtraceback from cgi import escape +from mandaye import config from mandaye.log import logger from mandaye.http import HTTPResponse, HTTPHeader -from mandaye.config import debug, email_notification, use_long_trace from mandaye.emails import Email from mandaye.template import serve_template email = None -if email_notification: +if config.email_notification: email = Email() def _get_html_error(tb_str, path, env=None): @@ -40,7 +40,7 @@ def _get_text_error(tbr_str, path, env=None): def _get_traceback(): """ Return a string wih the traceback """ - if use_long_trace: + if config.use_long_trace: tb_str = str(xtraceback.XTraceback(*sys.exc_info(), color=False)) else: tb_str = traceback.format_exc() @@ -85,7 +85,7 @@ def _500(path, msg, exception=None, env=None): logger.error("500 %s: %s, env: %r" % (path, msg, env)) title = 'Internal Server Error' headers = HTTPHeader({'Content-Type': ['text/html']}) - if debug: + if config.debug: if exception: msg += _get_html_error(tb_str, path, env=env) else: @@ -108,7 +108,7 @@ def _502(path, target_hostname, exception=None): msg = "The Mandaye server can't join the target server or misconfiguration \ and was unable to complete your request. Please contact the server \ administrator and inform them of the time the error occurred." - if debug and exception: + if config.debug and exception: msg += "

Target hostname: %s

" % target_hostname msg += _get_html_error(_get_traceback(), path) return HTTPResponse(502, 'Bad Gateway', headers, diff --git a/mandaye/server.py b/mandaye/server.py index 31289af..d2d8973 100644 --- a/mandaye/server.py +++ b/mandaye/server.py @@ -1,6 +1,5 @@ import Cookie -import config import urllib import urllib2 import poster.streaminghttp @@ -18,7 +17,7 @@ from static import Cling import mandaye -from mandaye.config import debug +from mandaye import config from mandaye.dispatcher import Dispatcher from mandaye.log import logger, format_logging_handlers from mandaye.handlers.default import MandayeRedirectHandler, MandayeErrorHandler @@ -45,7 +44,7 @@ def get_response(env, request, url, cookiejar=None): if request.cookies: req.add_header('cookie', request.cookies.output(attrs=[], sep=';', header='')) try: - if debug: + if config.debug: opener = urllib2.build_opener(MandayeErrorHandler, MandayeRedirectHandler, urllib2.HTTPSHandler(debuglevel=1), urllib2.HTTPHandler(debuglevel=1)) else: diff --git a/mandaye/template.py b/mandaye/template.py index 2de273f..1dae65f 100644 --- a/mandaye/template.py +++ b/mandaye/template.py @@ -2,9 +2,9 @@ from mako.lookup import TemplateLookup from mako.template import Template -from mandaye.config import template_directory +from mandaye import config -mylookup = TemplateLookup(directories=[template_directory], +mylookup = TemplateLookup(directories=[config.template_directory], input_encoding='utf-8') def serve_template(templatename, **kwargs): diff --git a/mandaye/wsgi.py b/mandaye/wsgi.py index 0168977..76390c8 100644 --- a/mandaye/wsgi.py +++ b/mandaye/wsgi.py @@ -1,7 +1,8 @@ + +from mandaye import config from mandaye.server import MandayeApp -from mandaye.config import session_opts from beaker.middleware import SessionMiddleware -application = SessionMiddleware(MandayeApp(), session_opts) +application = SessionMiddleware(MandayeApp(), config.session_opts) diff --git a/scripts/mandaye b/scripts/mandaye new file mode 100755 index 0000000..08bd871 --- /dev/null +++ b/scripts/mandaye @@ -0,0 +1,5 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" Script to manage mandaye project +""" diff --git a/setup.py b/setup.py index 5c25a28..4051e77 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ setup(name="mandaye", author_email="info@entrouvert.org", maintainer="Jerome Schneider", maintainer_email="jschneider@entrouvert.com", - scripts=['mandaye_admin.py', 'mandaye_migrate.py'], + scripts=['scripts/mandaye'], packages=find_packages(), package_data={}, install_requires=install_requires