PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/webapps/reports/config.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 118 lines | 115 code | 2 blank | 1 comment | 0 complexity | 2d69c5b6305f82b3957080e6d5edc762 MD5 | raw file
  1"""Universe configuration builder."""
  2import sys, os, logging, logging.config, ConfigParser
  3from optparse import OptionParser
  4from galaxy.util import string_as_bool
  5
  6log = logging.getLogger( __name__ )
  7
  8def resolve_path( path, root ):
  9    """If 'path' is relative make absolute by prepending 'root'"""
 10    if not( os.path.isabs( path ) ):
 11        path = os.path.join( root, path )
 12    return path
 13
 14class ConfigurationError( Exception ):
 15    pass
 16
 17class Configuration( object ):
 18    def __init__( self, **kwargs ):
 19        self.config_dict = kwargs
 20        self.root = kwargs.get( 'root_dir', '.' )
 21        # Database related configuration
 22        self.database = resolve_path( kwargs.get( "database_file", "database/universe.d" ), self.root )
 23        self.database_connection =  kwargs.get( "database_connection", False )
 24        self.database_engine_options = get_database_engine_options( kwargs )
 25        # Where dataset files are stored
 26        self.file_path = resolve_path( kwargs.get( "file_path", "database/files" ), self.root )
 27        self.new_file_path = resolve_path( kwargs.get( "new_file_path", "database/tmp" ), self.root )
 28        self.id_secret = kwargs.get( "id_secret", "USING THE DEFAULT IS NOT SECURE!" )
 29        self.use_remote_user = string_as_bool( kwargs.get( "use_remote_user", "False" ) )
 30        self.require_login = string_as_bool( kwargs.get( "require_login", "False" ) )
 31        self.template_path = resolve_path( kwargs.get( "template_path", "templates" ), self.root )
 32        self.template_cache = resolve_path( kwargs.get( "template_cache_path", "database/compiled_templates/reports" ), self.root )
 33        self.sendmail_path = kwargs.get('sendmail_path',"/usr/sbin/sendmail")
 34        self.allow_user_creation = string_as_bool( kwargs.get( "allow_user_creation", "True" ) )
 35        self.allow_user_deletion = string_as_bool( kwargs.get( "allow_user_deletion", "False" ) )
 36        self.log_actions = string_as_bool( kwargs.get( 'log_actions', 'False' ) )
 37        self.brand = kwargs.get( 'brand', None )
 38        # Configuration for the message box directly below the masthead.
 39        self.message_box_visible = kwargs.get( 'message_box_visible', False )
 40        self.message_box_content = kwargs.get( 'message_box_content', None )
 41        self.message_box_class = kwargs.get( 'message_box_class', 'info' )
 42        self.wiki_url = kwargs.get( 'wiki_url', 'http://wiki.g2.bx.psu.edu/FrontPage' )
 43        self.blog_url = kwargs.get( 'blog_url', None )
 44        self.screencasts_url = kwargs.get( 'screencasts_url', None )
 45        self.log_events = False
 46        self.cookie_path = kwargs.get( "cookie_path", "/" )
 47        # Error logging with sentry
 48        self.sentry_dsn = kwargs.get( 'sentry_dsn', None )
 49        #Parse global_conf
 50        global_conf = kwargs.get( 'global_conf', None )
 51        global_conf_parser = ConfigParser.ConfigParser()
 52        if global_conf and "__file__" in global_conf:
 53            global_conf_parser.read(global_conf['__file__'])
 54    def get( self, key, default ):
 55        return self.config_dict.get( key, default )
 56    def check( self ):
 57        # Check that required directories exist
 58        for path in self.root, self.file_path, self.template_path:
 59            if not os.path.isdir( path ):
 60                raise ConfigurationError("Directory does not exist: %s" % path )
 61
 62def get_database_engine_options( kwargs ):
 63    """
 64    Allow options for the SQLAlchemy database engine to be passed by using
 65    the prefix "database_engine_option".
 66    """
 67    conversions =  {
 68        'convert_unicode': string_as_bool,
 69        'pool_timeout': int,
 70        'echo': string_as_bool,
 71        'echo_pool': string_as_bool,
 72        'pool_recycle': int,
 73        'pool_size': int,
 74        'max_overflow': int,
 75        'pool_threadlocal': string_as_bool
 76    }
 77    prefix = "database_engine_option_"
 78    prefix_len = len( prefix )
 79    rval = {}
 80    for key, value in kwargs.iteritems():
 81        if key.startswith( prefix ):
 82            key = key[prefix_len:]
 83            if key in conversions:
 84                value = conversions[key](value)
 85            rval[ key  ] = value
 86    return rval
 87
 88def configure_logging( config ):
 89    """
 90    Allow some basic logging configuration to be read from the cherrpy
 91    config.
 92    """
 93    format = config.get( "log_format", "%(name)s %(levelname)s %(asctime)s %(message)s" )
 94    level = logging._levelNames[ config.get( "log_level", "DEBUG" ) ]
 95    destination = config.get( "log_destination", "stdout" )
 96    log.info( "Logging at '%s' level to '%s'" % ( level, destination ) )
 97    # Get root logger
 98    root = logging.getLogger()
 99    # Set level
100    root.setLevel( level )
101    # Turn down paste httpserver logging
102    if level <= logging.DEBUG:
103        logging.getLogger( "paste.httpserver.ThreadPool" ).setLevel( logging.WARN )
104    # Remove old handlers
105    for h in root.handlers[:]:
106        root.removeHandler(h)
107    # Create handler
108    if destination == "stdout":
109        handler = logging.StreamHandler( sys.stdout )
110    else:
111        handler = logging.FileHandler( destination )
112    # Create formatter
113    formatter = logging.Formatter( format )
114    # Hook everything up
115    handler.setFormatter( formatter )
116    root.addHandler( handler )
117
118