1. Derek Litz
  2. CherryPy


CherryPy / cherrypy / _cpconfig.py

import _cputil, cperror
import ConfigParser
from lib import autoreload

cpg = None # delayed import

defaultGlobal = {
    'server.socketPort': 8080,
    'server.socketHost': '',
    'server.socketFile': '',
    'server.socketQueueSize': 5,

    'server.environment': 'development',
    'server.protocolVersion': 'HTTP/1.0',
    'server.logToScreen': True,
    'server.logFile': '',
    'server.reverseDNS': False,
    'server.threadPool': 0,

    'session.storageType': 'ram',
    'session.timeout': 60,
    'session.cleanUpDelay': 60,
    'session.cookieName': 'CherryPySession',
    'session.storageFileDir': '',
    'sessionFilter.on': False,
    'sessionFilter.timeout': 60,
    'sessionFilter.cleanUpDelay': 60,
    'sessionFilter.storageType' : 'ram',
    'sessionFilter.cookieName': 'CherryPySession',
    'sessionFilter.storageFileDir': '.sessionFiles',
    'sessionFilter.new': 'sessionMap', # legacy setting
configMap = {"global": defaultGlobal.copy()}

def update(updateMap=None, file=None):
    if updateMap:
        for section, valueMap in updateMap.items():
            if not isinstance(valueMap, dict):
                # Shortcut syntax
                #   ex: update({'server.socketPort': 80})
                valueMap = {section: valueMap}
                section = 'global'
            s = configMap.get(section)
            if not s:
                configMap[section] = valueMap
    if file:
        if file not in autoreload.reloadFiles:

def get(key, defaultValue=None, returnSection=False, startPath = None):
    # Look, ma, no Python function calls! Uber-fast.
    # start path lest you overload the starting search path (needed by getAll)
    global cpg
    if not cpg:
        import cpg
    if startPath:
        path = startPath
            path = cpg.request.path
        except AttributeError:
            path = "/"
    while True:
        if path == "":
            path = "/"
            result = configMap[path][key]
        except KeyError:
            if path not in ("/", "global"):
                i = path.rfind("/")
                if i < 0:
                    result = defaultValue
                    path = path[:i]
            elif path != "global":
                path = "global"
                result = defaultValue
    if returnSection:
        if path == 'global':
            return '/'
        return path
        return result
import os.path

def getAll(key):
    getAll will lookup the key in the current node and all of its parent nodes,
    it will return a dictionary paths of each node containing the key and its value

    This function is required by the session filter
    path = get(key, None, returnSection = True)
    value = get(key)
    result = {}
    while value != None and path != '/':
        result[path]= value
        path = os.path.split(path)[0]
        value = get(key, None, returnSection = False, startPath = path)
        path  = get(key, None, returnSection = True, startPath = path)
    if path == '/' and value != None:
        result[path] = value
    return result

class CaseSensitiveConfigParser(ConfigParser.ConfigParser):
    """ Sub-class of ConfigParser that keeps the case of options and
        that raises an exception if the file cannot be read
    def optionxform(self, optionstr):
        return optionstr
    def read(self, filenames):
        if isinstance(filenames, basestring):
            filenames = [filenames]
        for filename in filenames:
            # try:
            #     fp = open(filename)
            # except IOError:
            #     continue
            fp = open(filename)
            self._read(fp, filename)

def _load(configFile = None):
    """ Convert an INI file to a dictionary """
    _cpLogMessage = _cputil.getSpecialAttribute('_cpLogMessage')

    # Parse config file
    configParser = CaseSensitiveConfigParser()
    if hasattr(configFile, 'read'):
        _cpLogMessage("Reading infos from configFile stream", 'CONFIG')
        _cpLogMessage("Reading infos from configFile: %s" % configFile, 'CONFIG')

    # Load INI file into cpg.configMap
    for section in configParser.sections():
        if section not in configMap:
            configMap[section] = {}
        for option in configParser.options(section):
            value = configParser.get(section, option)
                value = _cputil.unrepr(value)
            except cperror.WrongUnreprValue, s:
                msg = ("section: %s, option: %s, value: %s" %
                       (repr(section), repr(option), repr(value)))
                raise cperror.WrongConfigValue, msg
            configMap[section][option] = value

def outputConfigMap():
    _cpLogMessage = _cputil.getSpecialAttribute('_cpLogMessage')
    _cpLogMessage("Server parameters:", 'CONFIG')
    _cpLogMessage("  server.environment: %s" % get('server.environment'), 'CONFIG')
    _cpLogMessage("  server.logToScreen: %s" % get('server.logToScreen'), 'CONFIG')
    _cpLogMessage("  server.logFile: %s" % get('server.logFile'), 'CONFIG')
    _cpLogMessage("  server.protocolVersion: %s" % get('server.protocolVersion'), 'CONFIG')
    _cpLogMessage("  server.socketHost: %s" % get('server.socketHost'), 'CONFIG')
    _cpLogMessage("  server.socketPort: %s" % get('server.socketPort'), 'CONFIG')
    _cpLogMessage("  server.socketFile: %s" % get('server.socketFile'), 'CONFIG')
    _cpLogMessage("  server.reverseDNS: %s" % get('server.reverseDNS'), 'CONFIG')
    _cpLogMessage("  server.socketQueueSize: %s" % get('server.socketQueueSize'), 'CONFIG')
    _cpLogMessage("  server.threadPool: %s" % get('server.threadPool'), 'CONFIG')
    _cpLogMessage("  session.storageType: %s" % get('session.storageType'), 'CONFIG')
    if get('session.storageType'):
        _cpLogMessage("  session.timeout: %s min" % get('session.timeout'), 'CONFIG')
        _cpLogMessage("  session.cleanUpDelay: %s min" % get('session.cleanUpDelay'), 'CONFIG')
        _cpLogMessage("  session.cookieName: %s" % get('session.cookieName'), 'CONFIG')
        _cpLogMessage("  session.storageFileDir: %s" % get('session.storageFileDir'), 'CONFIG')
    _cpLogMessage("  staticContent: %s" % get('staticContent'), 'CONFIG')