Exemple #1
0
    def _loadini(self, baseini, defaultsett):
        """Parse master ini configuration file ``baseini`` and ini files
        refered by `baseini`. Construct a dictionary of settings for special
        sections and plugin sections."""
        from pluggdapps.plugin import pluginnames, plugin_info

        if not baseini or (not isfile(baseini)):
            return deepcopy(defaultsett)

        # Initialize return dictionary.
        settings = {}

        # context for parsing ini files.
        _vars = {"here": abspath(dirname(baseini))}

        # Read master ini file.
        cp = SafeConfigParser()
        cp.read(baseini)

        # [DEFAULT] overriding global def.
        s = deepcopy(defaultsett["DEFAULT"])
        s.update(dict(cp.defaults()))
        settings["DEFAULT"] = normalize_defaults(s)

        # [pluggdapps]
        s = deepcopy(defaultsett["pluggdapps"])
        if cp.has_section("pluggdapps"):
            s.update(dict(cp.items("pluggdapps", vars=_vars)))
            s.pop("here", None)  # TODO : how `here` gets populated ??
            settings["pluggdapps"] = normalize_pluggdapps(s)

        # Override plugin's package default settings with [DEFAULT] settings.
        for pluginsec, sett in defaultsett.items():
            if not pluginsec.startswith("plugin:"):
                continue
            sett = h.mergedict(sett, settings["DEFAULT"])
            if cp.has_section(pluginsec):
                sett.update(dict(cp.items(pluginsec, vars=_vars)))
                sett.pop("here", None)  # TODO : how `here` ??
            cls = plugin_info(h.sec2plugin(pluginsec))["cls"]
            for b in reversed(cls.mro()):
                if hasattr(b, "normalize_settings"):
                    sett = b.normalize_settings(sett)
            settings[pluginsec] = sett
        return settings
    def _loadini(self, baseini, defaultsett):
        """Parse master ini configuration file ``baseini`` and ini files
        refered by `baseini`. Construct a dictionary of settings for special
        sections and plugin sections."""
        from pluggdapps.plugin import pluginnames, plugin_info

        if not baseini or (not isfile(baseini)):
            return deepcopy(defaultsett)

        # Initialize return dictionary.
        settings = {}

        # context for parsing ini files.
        _vars = {'here': abspath(dirname(baseini))}

        # Read master ini file.
        cp = SafeConfigParser()
        cp.read(baseini)

        # [DEFAULT] overriding global def.
        s = deepcopy(defaultsett['DEFAULT'])
        s.update(dict(cp.defaults()))
        settings['DEFAULT'] = normalize_defaults(s)

        # [pluggdapps]
        s = deepcopy(defaultsett['pluggdapps'])
        if cp.has_section('pluggdapps'):
            s.update(dict(cp.items('pluggdapps', vars=_vars)))
            s.pop('here', None)  # TODO : how `here` gets populated ??
            settings['pluggdapps'] = normalize_pluggdapps(s)

        # Override plugin's package default settings with [DEFAULT] settings.
        for pluginsec, sett in defaultsett.items():
            if not pluginsec.startswith('plugin:'): continue
            sett = h.mergedict(sett, settings['DEFAULT'])
            if cp.has_section(pluginsec):
                sett.update(dict(cp.items(pluginsec, vars=_vars)))
                sett.pop('here', None)  # TODO : how `here` ??
            cls = plugin_info(h.sec2plugin(pluginsec))['cls']
            for b in reversed(cls.mro()):
                if hasattr(b, 'normalize_settings'):
                    sett = b.normalize_settings(sett)
            settings[pluginsec] = sett
        return settings
Exemple #3
0
def with_inisettings( baseini, appdefaults, plugindefaults ):
    """Parse master ini configuration file `baseini` and ini files refered by
    `baseini`. Construct a dictionary of settings for all application
    instances."""
    from pluggdapps.plugin import pluginnames, applications

    # context for parsing ini files.
    _vars = { 'here' : dirname(baseini) }

    # Parse master ini file.
    cp = configparser.SafeConfigParser()
    cp.read( baseini )

    # Fetch special section [webmounts]
    if cp.has_section('webmounts') :
        webmounts = cp.items( 'webmounts', vars=_vars )
    else :
        webmounts = []

    # Make local copy of appdefaults, the local copy will be used to detect
    # applications that are left un-configured in master ini file.
    appdefaults_ = deepcopy( appdefaults )

    # Compute default settings for each application instance.
    instdef = {}
    appsecs = appdefaults.keys()

    for (name, val) in webmounts :
        appsec = app2sec( name )
        if appsec not in appsecs : continue

        # Parse mount values and validate them.
        y = [ x.strip() for x in val.split(',') ]
        if len(y) == 3 :
            (t,mountname,instconfig) = y
        elif len(y) == 2 :
            (t,mountname,instconfig) = y + [None]
        else :
            raise Exception("Invalid mount configuration %r" % val)

        if t not in MOUNT_TYPES :
            raise Exception("%r for %r is not a valid mount type" % (t, name))
        
        # Settings for application instance is instantiated here for the first
        # time.
        instkey = (appsec,t,mountname,instconfig)
        if instkey in instdef :
            raise Exception( "App instance %r already defined" % instkey )
        instdef[ instkey ] = { appsec : deepcopy( appdefaults.get( appsec )) }
        appdefaults_.pop( appsec, None )

    # Compute default settings for unconfigured application instance.
    for appsec, sett in list( appdefaults_.items() ) :
        instkey = ( appsec, MOUNT_SCRIPT, "/"+sec2app(appsec), None )
        if instkey in instdef :
            raise Exception("Application instance %r already defined"%instkey)
        instdef[ instkey ] = { appsec : deepcopy(sett) }

    # Override package [DEFAULT] settings with master ini file's [DEFAULT]
    # settings.
    defaultsett1 = dict( DEFAULT().items() ) # Global defaults
    defaultsett2 = cp.defaults() )           # [DEFAULT] overriding global def.

    # Package defaults for `pluggdapps` special section. And override them
    # with [DEFAULT] settings and then with settings from master ini file's
    # [pluggdapps] section.
    settings = { 'pluggdapps' : h.mergedict(
                                    defaultsett1, 
                                    dict( pluggdapps_defaultsett().items() ),
                                    defaultsett2
                                )}
    if cp.has_section('pluggdapps') :
        settings['pluggdapps'].update(
                    dict( cp.items( 'pluggdapps', vars=_vars )))
Exemple #4
0
    # Package defaults for `pluggdapps` special section. And override them
    # with [DEFAULT] settings and then with settings from master ini file's
    # [pluggdapps] section.
    settings = { 'pluggdapps' : h.mergedict(
                                    defaultsett1, 
                                    dict( pluggdapps_defaultsett().items() ),
                                    defaultsett2
                                )}
    if cp.has_section('pluggdapps') :
        settings['pluggdapps'].update(
                    dict( cp.items( 'pluggdapps', vars=_vars )))

    # Package defaults for `webmounts` special section. And override them
    # with [DEFAULT] settings.
    settings['webmounts'] = h.mergedict( defaultsett1,
                                         dict(webmounts),
                                         defaultsett2 )

    # Override plugin's package default settings with [DEFAULT] settings.
    plugindefaults = { key : h.mergedict(defaultsett1, sett, defaultsett2)
                       for key, sett plugindefaults.items() }
    settings.update( deepcopy( plugindefaults ))

    # Create instance-wise full settings dictionary.
    for instkey, values in instdef.items() :
        appsec, t, mountname, config = instkey
        settings[instkey] = { appsec : h.mergedict( defaultsett1,
                                                    values[appsec], 
                                                    defaultsett2 ) }
        settings[instkey].update( deepcopy( plugindefaults ))