示例#1
0
def drop_tables_from_standard_configuration(configuration_yaml_path, section='pyramid_oereb'):
    """
    Drops all schemas which are defined in the passed yaml file: <section>.<plrs>.[<plr>.<code>]. The code
    must be camel case. It will be transformed to snake case and used as schema name.
    Drops all tables inside the created schemas.

    Args:
        configuration_yaml_path (str): The absolute path to the yaml file which contains the plr
            definitions.
        section (str): The section in yaml file where the plrs are configured in. Default is 'pyramid_oereb'.
    """
    config = parse(configuration_yaml_path, section)
    main_schema_engine = create_engine(config.get('app_schema').get('db_connection'), echo=True)
    main_schema_connection = main_schema_engine.connect()
    main_schema_connection.execute('DROP SCHEMA IF EXISTS {name} CASCADE;'.format(
        name=config.get('app_schema').get('name'))
    )
    main_schema_connection.close()
    for schema in config.get('plrs'):
        if schema.get('standard'):
            plr_schema_engine = create_engine(schema.get('source').get('params').get('db_connection'),
                                              echo=True)
            plr_schema_connection = plr_schema_engine.connect()
            plr_schema_connection.execute('DROP SCHEMA IF EXISTS {name} CASCADE;'.format(
                name=convert_camel_case_to_snake_case(schema.get('code')))
            )
            plr_schema_connection.close()
    def __init__(self, configuration, section='pyramid_oereb', directory='sample_data', sql_file=None):
        """

        Args:
            configuration (str): Path to the configuration yaml file.
            section (str): The used section within the yaml file. Default is `pyramid_oereb`.
            directory (str): Location of the sample data. Default is `sample_data`.
            sql_file (file): The SQL file to be created. Default is None.
        """
        self._configuration = configuration
        self._section = section
        self._directory = directory
        self._sql_file = sql_file

        config = parse(self._configuration, self._section)
        self._engine = create_engine(config.get('app_schema').get('db_connection'), echo=True)
        self._connection = None
示例#3
0
def _create_theme_tables(configuration_yaml_path,
                         theme,
                         section='pyramid_oereb',
                         tables_only=False):
    """
    Create all tables defined in the specified module.

    Args:
        configuration_yaml_path (str): Path to the configuration file.
        theme (str): Code of the theme to create the tables for.
        section (str): Section within the specified configuration file used for pyramid_oereb. Default is
            'pyramid_oereb'.
        tables_only (bool): True to skip creation of schema. Default is False.
    """

    # Parse themes from configuration
    config = parse(configuration_yaml_path, section)
    themes = config.get('plrs')
    if not isinstance(themes, list):
        raise ConfigurationError('No list of themes found.')

    # Find the specified theme
    found = False
    for t in themes:
        if t.get('code') == theme:

            # Check required configuration parameters
            params = t.get('source').get('params')
            if not isinstance(params, dict):
                raise ConfigurationError(
                    'Missing params property in source definition.')
            if not ('db_connection' in params and 'models' in params):
                raise ConfigurationError(
                    'Params has to contain "db_connection" and "models" properties.'
                )

            # Create sqlalchemy engine for configured connection and load module containing models
            engine = create_engine(params.get('db_connection'), echo=True)
            models = DottedNameResolver().resolve(params.get('models'))

            if not tables_only:
                # Iterate over contained classes to collect needed schemas
                classes = inspect.getmembers(models, inspect.isclass)
                schemas = []
                create_schema_sql = 'CREATE SCHEMA IF NOT EXISTS {schema};'
                for c in classes:
                    class_ = c[1]
                    if hasattr(class_, '__table__'
                               ) and class_.__table__.schema not in schemas:
                        schemas.append(class_.__table__.schema)

                # Try to create missing schemas
                connection = engine.connect()
                try:
                    for schema in schemas:
                        connection.execute(
                            create_schema_sql.format(schema=schema))
                finally:
                    connection.close()

            # Create tables
            models.Base.metadata.create_all(engine)
            found = True
            break

    if not found:
        raise ValueError(
            'Specified theme "{theme}" not found in configuration.'.format(
                theme=theme))
示例#4
0
def test_missing_configuration_file():
    with pytest.raises(ConfigurationError):
        parse(None, None)
示例#5
0
def test_parse_configuration():
    cfg = parse('./tests/resources/test_config.yml', 'section2')
    assert cfg.get('param1') == 1
    assert len(cfg.get('param2')) == 2
    assert cfg.get('param2')[0] == 'first'
    assert cfg.get('param2')[1] == 'second'
示例#6
0
def test_configuration_file_not_found():
    with pytest.raises(IOError) as excinfo:
        parse('not_existing_config.yml', 'invalidsection')
    assert ', Current working directory is ' in str(excinfo.value)
示例#7
0
def test_wrong_configuration_section():
    with pytest.raises(ConfigurationError):
        parse('./tests/resources/test_config.yml', 'invalidsection')
示例#8
0
def test_missing_configuration_section():
    with pytest.raises(ConfigurationError):
        parse('myconfig.yml', None)
示例#9
0
def includeme(config):
    """
    By including this in your pyramid web app you can easily provide a running OEREB Server

    Args:
        config (Configurator): The pyramid apps config object
    """

    global route_prefix, app_schema_name, srid

    # Set route prefix
    route_prefix = config.route_prefix

    # Get settings
    settings = config.get_settings()

    # Load configuration file
    cfg_file = settings.get('pyramid_oereb.cfg.file', None)
    cfg_c2ctemplate_file = settings.get('pyramid_oereb.cfg.c2ctemplate.file',
                                        None)
    cfg_section = settings.get('pyramid_oereb.cfg.section', None)
    Config.init(cfg_file or cfg_c2ctemplate_file, cfg_section,
                cfg_file is None)
    Config.update_settings(settings)

    real_estate_config = Config.get_real_estate_config()
    municipality_config = Config.get_municipality_config()
    exclusion_of_liability_config = Config.get_exclusion_of_liability_config()
    glossary_config = Config.get_glossary_config()
    logos = Config.get_logo_config()
    app_schema_name = Config.get('app_schema').get('name')
    srid = Config.get('srid')

    plr_cadastre_authority = Config.get_plr_cadastre_authority()

    real_estate_reader = RealEstateReader(
        real_estate_config.get('source').get('class'),
        **real_estate_config.get('source').get('params'))

    municipality_reader = MunicipalityReader(
        municipality_config.get('source').get('class'),
        **municipality_config.get('source').get('params'))

    exclusion_of_liability_reader = ExclusionOfLiabilityReader(
        exclusion_of_liability_config.get('source').get('class'),
        **exclusion_of_liability_config.get('source').get('params'))

    glossary_reader = GlossaryReader(
        glossary_config.get('source').get('class'),
        **glossary_config.get('source').get('params'))

    plr_sources = []
    for plr in Config.get('plrs'):
        plr_source_class = DottedNameResolver().maybe_resolve(
            plr.get('source').get('class'))
        plr_sources.append(plr_source_class(**plr))

    extract_reader = ExtractReader(plr_sources, plr_cadastre_authority, logos)

    settings.update({
        'pyramid_oereb':
        parse(cfg_file or cfg_c2ctemplate_file, cfg_section, cfg_file is None)
    })
    processor = Processor(
        real_estate_reader=real_estate_reader,
        municipality_reader=municipality_reader,
        exclusion_of_liability_reader=exclusion_of_liability_reader,
        glossary_reader=glossary_reader,
        plr_sources=plr_sources,
        extract_reader=extract_reader,
    )

    def pyramid_oereb_processor(request):
        return processor

    config.add_request_method(pyramid_oereb_processor, reify=True)

    config.add_renderer('pyramid_oereb_extract_json',
                        'pyramid_oereb.lib.renderer.extract.json_.Renderer')
    config.add_renderer('pyramid_oereb_extract_xml',
                        'pyramid_oereb.lib.renderer.extract.xml_.Renderer')
    config.add_renderer('pyramid_oereb_extract_print',
                        Config.get('print').get('renderer'))
    config.add_renderer('pyramid_oereb_versions_xml',
                        'pyramid_oereb.lib.renderer.versions.xml_.Renderer')
    config.add_renderer(
        'pyramid_oereb_capabilities_xml',
        'pyramid_oereb.lib.renderer.capabilities.xml_.Renderer')
    config.add_renderer('pyramid_oereb_getegrid_xml',
                        'pyramid_oereb.lib.renderer.getegrid.xml_.Renderer')

    config.include('pyramid_oereb.routes')
示例#10
0
def create_tables_from_standard_configuration(
        configuration_yaml_path, section='pyramid_oereb', tables_only=False, sql_file=None):
    """
    Creates all schemas which are defined in the passed yaml file: <section>.<plrs>.[<plr>.<code>]. The code
    must be camel case. It will be transformed to snake case and used as schema name.
    Creates all tables inside the created schemas. This only affects the sqlalchemy models which are defined
    with the Base class from pyramid_oereb.standard.models.

    Args:
        configuration_yaml_path (str): The absolute path to the yaml file which contains the plr
            definitions.
        section (str): The section in yaml file where the plrs are configured in. Default is 'pyramid_oereb'.
        tables_only (bool): True to skip creation of schema. Default is False.
        sql_file (file): the file to generate. Default is None (in the database).
    """
    config = parse(configuration_yaml_path, section)

    main_schema_engine = create_engine(config.get('app_schema').get('db_connection'), echo=True)
    if sql_file is None:
        if not tables_only:
            main_schema_connection = main_schema_engine.connect()
            try:
                main_schema_connection.execute(
                    'CREATE SCHEMA IF NOT EXISTS {name};'.format(name=config.get('app_schema').get('name'))
                )
            finally:
                main_schema_connection.close()
    else:
        sql_file.write('CREATE SCHEMA {name};\n'.format(name=config.get('app_schema').get('name')))

    main_base_class = DottedNameResolver().maybe_resolve('{package}.Base'.format(
        package=config.get('app_schema').get('models')
    ))
    if sql_file is None:
        main_base_class.metadata.create_all(main_schema_engine)
    else:
        for table in main_base_class.metadata.sorted_tables:
            create_table = str(CreateTable(table).compile(main_schema_engine))\
                .replace('DATETIME', 'timestamp')
            sql_file.write('{};\n'.format(create_table))

    for schema in config.get('plrs'):

        plr_schema_engine = create_engine(schema.get('source').get('params').get('db_connection'), echo=True)

        if sql_file is None:
            if schema.get('standard'):

                if not tables_only:
                    plr_schema_connection = plr_schema_engine.connect()
                    try:
                        plr_schema_connection.execute('CREATE SCHEMA IF NOT EXISTS {name};'.format(
                            name=convert_camel_case_to_snake_case(schema.get('code')))
                        )
                    finally:
                        plr_schema_connection.close()

                plr_base = DottedNameResolver().maybe_resolve('{package}.Base'.format(
                    package=schema.get('source').get('params').get('models')
                ))
                plr_base.metadata.create_all(plr_schema_engine)

        else:
            plr_base = DottedNameResolver().maybe_resolve('{package}.Base'.format(
                package=schema.get('source').get('params').get('models')
            ))
            sql_file.write('CREATE SCHEMA {name};\n'.format(
                name=convert_camel_case_to_snake_case(schema.get('code')))
            )
            for table in plr_base.metadata.sorted_tables:
                create_table = str(CreateTable(table).compile(plr_schema_engine))\
                    .replace('DATETIME', 'timestamp')
                sql_file.write('{};\n'.format(create_table))