PageRenderTime 20ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/webapps/demo_sequencer/config.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 121 lines | 113 code | 4 blank | 4 comment | 1 complexity | c1f1da8433c8aa22c936b6131c9c0b42 MD5 | raw file
  1"""
  2Universe configuration builder.
  3"""
  4
  5import sys, os
  6import logging, logging.config
  7from optparse import OptionParser
  8import ConfigParser
  9from galaxy.util import string_as_bool
 10
 11from galaxy import eggs
 12import pkg_resources
 13
 14log = logging.getLogger( __name__ )
 15
 16def resolve_path( path, root ):
 17    """If 'path' is relative make absolute by prepending 'root'"""
 18    if not( os.path.isabs( path ) ):
 19        path = os.path.join( root, path )
 20    return path
 21
 22class ConfigurationError( Exception ):
 23    pass
 24
 25class Configuration( object ):
 26    def __init__( self, **kwargs ):
 27        self.config_dict = kwargs
 28        self.root = kwargs.get( 'root_dir', '.' )
 29        # Collect the umask and primary gid from the environment
 30        self.umask = os.umask( 077 ) # get the current umask
 31        os.umask( self.umask ) # can't get w/o set, so set it back
 32        # Where dataset files are stored
 33        self.file_path = resolve_path( kwargs.get( "file_path", "database/files" ), self.root )
 34        self.new_file_path = resolve_path( kwargs.get( "new_file_path", "database/tmp" ), self.root )
 35        self.cookie_path = kwargs.get( "cookie_path", "/" )
 36        self.test_conf = resolve_path( kwargs.get( "test_conf", "" ), self.root )
 37        self.id_secret = kwargs.get( "id_secret", "USING THE DEFAULT IS NOT SECURE!" )
 38        self.use_remote_user = string_as_bool( kwargs.get( "use_remote_user", "False" ) )
 39        self.remote_user_maildomain = kwargs.get( "remote_user_maildomain", None )
 40        self.remote_user_logout_href = kwargs.get( "remote_user_logout_href", None )
 41        self.require_login = string_as_bool( kwargs.get( "require_login", "False" ) )
 42        self.allow_user_creation = string_as_bool( kwargs.get( "allow_user_creation", "True" ) )
 43        self.allow_user_deletion = string_as_bool( kwargs.get( "allow_user_deletion", "False" ) )
 44        self.template_path = resolve_path( kwargs.get( "template_path", "templates" ), self.root )
 45        self.template_cache = resolve_path( kwargs.get( "template_cache_path", "database/compiled_templates/demo_sequencer" ), self.root )
 46        self.admin_users = kwargs.get( "admin_users", "" )
 47        self.sendmail_path = kwargs.get('sendmail_path',"/usr/sbin/sendmail")
 48        self.mailing_join_addr = kwargs.get('mailing_join_addr',"galaxy-announce-join@bx.psu.edu")
 49        self.error_email_to = kwargs.get( 'error_email_to', None )
 50        self.smtp_server = kwargs.get( 'smtp_server', None )
 51        self.log_actions = string_as_bool( kwargs.get( 'log_actions', 'False' ) )
 52        self.brand = kwargs.get( 'brand', None )
 53        self.wiki_url = kwargs.get( 'wiki_url', 'http://wiki.g2.bx.psu.edu/FrontPage' )
 54        self.blog_url = kwargs.get( 'blog_url', None )
 55        self.screencasts_url = kwargs.get( 'screencasts_url', None )
 56        self.log_events = False
 57        self.cloud_controller_instance = False
 58        # Proxy features
 59        self.apache_xsendfile = kwargs.get( 'apache_xsendfile', False )
 60        self.nginx_x_accel_redirect_base = kwargs.get( 'nginx_x_accel_redirect_base', False )
 61        self.sequencer_actions_config = kwargs.get( 'sequencer_actions_config_file', 'galaxy/webapps/demo_sequencer/sequencer_actions.xml' )
 62        # Parse global_conf and save the parser
 63        global_conf = kwargs.get( 'global_conf', None )
 64        global_conf_parser = ConfigParser.ConfigParser()
 65        self.global_conf_parser = global_conf_parser
 66        if global_conf and "__file__" in global_conf:
 67            global_conf_parser.read(global_conf['__file__'])
 68    def get( self, key, default ):
 69        return self.config_dict.get( key, default )
 70    def get_bool( self, key, default ):
 71        if key in self.config_dict:
 72            return string_as_bool( self.config_dict[key] )
 73        else:
 74            return default
 75    def check( self ):
 76        # Check that required directories exist
 77        for path in self.root, self.file_path, self.template_path:
 78            if not os.path.isdir( path ):
 79                raise ConfigurationError("Directory does not exist: %s" % path )
 80    def is_admin_user( self, user ):
 81        """
 82        Determine if the provided user is listed in `admin_users`.
 83        """
 84        admin_users = self.get( "admin_users", "" ).split( "," )
 85        return user is not None and user.email in admin_users
 86
 87def configure_logging( config ):
 88    """
 89    Allow some basic logging configuration to be read from the cherrpy
 90    config.
 91    """
 92    # PasteScript will have already configured the logger if the appropriate
 93    # sections were found in the config file, so we do nothing if the
 94    # config has a loggers section, otherwise we do some simple setup
 95    # using the 'log_*' values from the config.
 96    if config.global_conf_parser.has_section( "loggers" ):
 97        return
 98    format = config.get( "log_format", "%(name)s %(levelname)s %(asctime)s %(message)s" )
 99    level = logging._levelNames[ config.get( "log_level", "DEBUG" ) ]
100    destination = config.get( "log_destination", "stdout" )
101    log.info( "Logging at '%s' level to '%s'" % ( level, destination ) )
102    # Get root logger
103    root = logging.getLogger()
104    # Set level
105    root.setLevel( level )
106    # Turn down paste httpserver logging
107    if level <= logging.DEBUG:
108        logging.getLogger( "paste.httpserver.ThreadPool" ).setLevel( logging.WARN )
109    # Remove old handlers
110    for h in root.handlers[:]:
111        root.removeHandler(h)
112    # Create handler
113    if destination == "stdout":
114        handler = logging.StreamHandler( sys.stdout )
115    else:
116        handler = logging.FileHandler( destination )
117    # Create formatter
118    formatter = logging.Formatter( format )
119    # Hook everything up
120    handler.setFormatter( formatter )
121    root.addHandler( handler )