Example #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'.
    """
    if Config.get_config() is None:
        Config.init(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()
Example #2
0
    def __init__(self,
                 configuration_file,
                 topic_code,
                 section='pyramid_oereb',
                 c2ctemplate_style=False,
                 arc_max_diff=0.001,
                 arc_precision=3,
                 tmp_dir='.',
                 srid=None):
        """

        Args:
            configuration_file (str): Path to the configuration file to be used.
            topic_code (str): The code of the federal topic to be updated.
            section (str): The section within the configuration file. (default: 'pyramid_oereb')
            c2ctemplate_style (bool): True if the yaml use a c2c template style (vars.[section]).
                Default is False.
            arc_max_diff (float): Maximum difference between arc and line segment for stroking.
                (default: 0.001)
            arc_precision (int): Coordinate precision for generated arc points. (default: 3)
            tmp_dir (str): Directory used as temporary working directory. (default: '.')
        """
        self._log = logging.getLogger('import_federal_topic')
        Config.init(configuration_file, section, c2ctemplate_style)
        self._settings = Config.get_config()

        topic_settings = None
        for topic in self._settings.get('plrs'):
            if topic.get('code') == topic_code:
                topic_settings = topic
        if topic_settings is None:
            self._log.error('Cannot find topic {0} in {1}'.format(
                topic_code, configuration_file))
            exit(1)
        self._srid = srid or self._settings.get('srid')
        if self._srid is None:
            self._log.error(
                'No SRID defined in configuration or passed as argument')
            exit(1)
        self._tmp_dir = tmp_dir
        self._arc_max_diff = arc_max_diff
        self._arc_precision = arc_precision
        self._topic_settings = topic_settings
        self._connection = topic_settings.get('source').get('params').get(
            'db_connection')
        models_path = topic_settings.get('source').get('params').get('models')
        self._models = DottedNameResolver().maybe_resolve(models_path)
        self._file_id = '{0}'.format(uuid4())
        self._checksum = None
        self._data_integration_office_id = None
Example #3
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

    # 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)

    settings.update({'pyramid_oereb': Config.get_config()})

    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')
Example #4
0
def create_tables_from_standard_configuration(configuration_yaml_path,
                                              section='pyramid_oereb',
                                              c2ctemplate_style=False,
                                              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'.
        c2ctemplate_style (bool): True if the yaml use a c2c template style (vars.[section]).
            Default is False.
        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).
    """
    if Config.get_config() is None:
        Config.init(configuration_yaml_path, section, c2ctemplate_style)

    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))