예제 #1
0
파일: config.py 프로젝트: mikejmets/iw.fss
def loadConfig():
    """Loads configuration from a ZConfig file"""
    from customconfig import ZOPETESTCASE

    global ZCONFIG, dummy_handler, CONFIG_FILE

    import os
    from Globals import INSTANCE_HOME
    from ZConfig.loader import ConfigLoader
    from iw.fss.configuration.schema import fssSchema

    # Configuration directories
    INSTANCE_ETC = os.path.join(INSTANCE_HOME, 'etc')
    _this_directory = os.path.abspath(os.path.dirname(__file__))
    FSS_ETC = os.path.join(_this_directory, 'etc')

    def filePathOrNone(file_path):
        return os.path.isfile(file_path) and file_path or None

    # (Potential) configuration files
    CONFIG_FILENAME = 'plone-filesystemstorage.conf'
    INSTANCE_CONFIG = filePathOrNone(os.path.join(INSTANCE_ETC, CONFIG_FILENAME))
    FSS_CONFIG = filePathOrNone(os.path.join(FSS_ETC, CONFIG_FILENAME))
    FSS_CONFIG_IN = filePathOrNone(os.path.join(FSS_ETC, CONFIG_FILENAME + '.in'))

    # We configure on the first available config file
    CONFIG_FILE = [fp for fp in (INSTANCE_CONFIG, FSS_CONFIG, FSS_CONFIG_IN)
                   if fp is not None][0]

    # We ignore personal configuration on unit tests
    if ZOPETESTCASE:
        ZCONFIG, dummy_handler = ConfigLoader(fssSchema).loadURL(FSS_CONFIG_IN)
    else:
        ZCONFIG, dummy_handler = ConfigLoader(fssSchema).loadURL(CONFIG_FILE)
예제 #2
0
def get_config_loader() -> ConfigLoader:
    """
    Get the config loader with all extensions

    :return: The fully extended config loader
    """

    # Patch the parser because otherwise it will reject the example section in the schema

    # Construct the paths to all necessary files
    schema_filename = os.path.abspath(os.path.join(os.path.dirname(__file__), "config_schema.xml"))

    # Patch the parser if we have an old version of ZConfig
    # noinspection PyProtectedMember
    if 'schema' not in BaseParser._allowed_parents['example']:
        # noinspection PyProtectedMember
        BaseParser._allowed_parents['example'] += ['schema', 'sectiontype']

        # Add some properties
        ZConfig.info.SchemaType.example = None
        ZConfig.info.SectionType.example = None

        # Patch a method by saving the old method and replacing the original with a patched version
        ZConfig.info.oldCreateDerivedSchema = ZConfig.info.createDerivedSchema

        # noinspection PyUnresolvedReferences,PyPep8Naming
        def patchedCreateDerivedSchema(base: ZConfig.info.SchemaType) -> ZConfig.info.SchemaType:
            """
            Also copy the example section.

            :param base: The original
            :return: The copy
            """
            new = ZConfig.info.oldCreateDerivedSchema(base)
            new.example = base.example
            return new

        ZConfig.info.createDerivedSchema = patchedCreateDerivedSchema

    # Load the schema
    schema_loader = SchemaLoader()
    schema = schema_loader.loadURL(url=schema_filename)

    # Build the config loader based on the schema, extended with the schemas of option handlers
    config_loader = ConfigLoader(schema=schema)

    # Iterate over all server extensions
    for extension_name, extension in server_extension_registry.items():
        # Option handlers that refer to packages contain components
        if inspect.ismodule(extension) and hasattr(extension, '__path__'):
            # It's a package! Try to import
            try:
                config_loader.importSchemaComponent(extension.__name__)
                logger.debug("Configuration extension {} loaded".format(extension_name))
            except SchemaResourceError:
                # Component missing, assume it's a package without a config component
                pass

    return config_loader
예제 #3
0
def loadConfig(files, schema=coreblog2Schema, overwrite=False):
    """Config loader
    
    The config loader tries to load the first existing file
    """
    global zconf, handler, conf_file
    if not isinstance(files, (tuple, list)):
        files = (files, )
    if zconf is not None and not overwrite:
        raise RuntimeError, 'Configuration is already loaded'
    for file in files:
        if file is not None:
            if not os.path.exists(file):
                raise RuntimeError, '%s does not exist' % file
            conf_file = file
            zconf, handler = ConfigLoader(schema).loadURL(conf_file)
            break
예제 #4
0
 def load_configfile(self):
     loader = ConfigLoader(self.schema)
     self.configroot, self.confighandlers = loader.loadURL(self.configfile)
예제 #5
0
 def create_config_loader(self, schema):
     return ConfigLoader(schema)
예제 #6
0
 def load_configfile(self):
     loader = ConfigLoader(self.schema)
     self.configroot, self.confighandlers = loader.loadURL(
         self.configfile)