Beispiel #1
0
def create_module_config(module_name, mod_path=None, build=True):
    """
        Create the frontend config
    """
    if not mod_path:
        mod_path = str(GN_EXTERNAL_MODULE / module_name)
    manifest_path = os.path.join(mod_path, 'manifest.toml')

    # Create the frontend config for a module and rebuild if build=True
    conf_manifest = utilstoml.load_and_validate_toml(manifest_path,
                                                     ManifestSchemaProdConf)

    # import du module dans le sys.path
    module_parent_dir = str(Path(mod_path).parent)
    module_schema_conf = "{}.config.conf_schema_toml".format(
        Path(mod_path).name)  # noqa
    sys.path.insert(0, module_parent_dir)
    module = __import__(module_schema_conf, globals=globals())
    front_module_conf_file = os.path.join(mod_path,
                                          'config/conf_gn_module.toml')  # noqa
    config_module = utilstoml.load_and_validate_toml(
        front_module_conf_file,
        module.config.conf_schema_toml.GnModuleSchemaConf)

    frontend_config_path = os.path.join(
        mod_path, 'frontend/app/module.config.ts')  # noqa
    try:
        with open(str(ROOT_DIR / frontend_config_path), 'w') as outputfile:
            outputfile.write("export const ModuleConfig = ")
            json.dump(config_module, outputfile, indent=True, sort_keys=True)
    except FileNotFoundError:
        log.info('No frontend config file')
    if build:
        build_geonature_front()
Beispiel #2
0
def import_legacy_module(module_object):
    sys.path.insert(
        0,
        str(GN_EXTERNAL_MODULE))  # to be able to import non-packaged modules
    try:
        # module dist is module_code.lower() because the symlink is created like this
        # in utils.gn_module_import.copy_in_external_mods
        module_dist = module_object.module_code.lower()
        module_dir = GN_EXTERNAL_MODULE / module_dist
        manifest_path = module_dir / 'manifest.toml'
        if not manifest_path.is_file():
            raise NoManifestFound()
        module_manifest = load_and_validate_toml(manifest_path,
                                                 ManifestSchemaProdConf)
        module_blueprint = import_module(
            f'{module_dist}.backend.blueprint').blueprint
        module_config = {
            'ID_MODULE': module_object.id_module,
            'MODULE_CODE': module_object.module_code,
            'MODULE_URL': '/' + module_object.module_path.replace(' ', ''),
            'FRONTEND_PATH': str(module_dir / 'frontend'),
        }
        module_schema = import_module(
            f'{module_object.module_code.lower()}.config.conf_schema_toml'
        ).GnModuleSchemaConf
        config_path = module_dir / "config/conf_gn_module.toml"
        module_config.update(load_and_validate_toml(config_path,
                                                    module_schema))
        module_blueprint.config = module_config
        return module_config, module_blueprint
    finally:
        sys.path.pop(0)
Beispiel #3
0
def list_and_import_gn_modules(app, mod_path=GN_EXTERNAL_MODULE):
    """
        Get all the module enabled from gn_commons.t_modules
        register the configuration and import the module programaticly
    """
    with app.app_context():
        from geonature.core.gn_commons.models import TModules

        modules = DB.session.query(TModules).filter(TModules.active_backend == True)
        module_info = {}
        enabled_modules_name = []
        for mod in modules:
            enabled_modules_name.append(mod.module_code)
            module_info[mod.module_code] = {
                "ID_MODULE": mod.id_module,
                "MODULE_URL": "/" + mod.module_path.replace(" ", ""),
                "MODULE_CODE": mod.module_code,
            }
    # iter over external_modules dir
    # and import only modules which are enabled
    for f in mod_path.iterdir():
        if f.is_dir():
            conf_manifest = load_and_validate_toml(
                str(f / "manifest.toml"), ManifestSchemaProdConf
            )
            # set module code upper because module call is always upper in gn_commons.t_modules
            module_code = conf_manifest["module_code"].upper()
            if module_code in enabled_modules_name:
                # import du module dans le sys.path
                module_path = Path(GN_EXTERNAL_MODULE / module_code.lower())
                module_parent_dir = str(module_path.parent)
                module_import_name = "{}.config.conf_schema_toml".format(
                    module_path.name
                )
                sys.path.insert(0, module_parent_dir)
                module = __import__(module_import_name)
                # get and validate the module config
                class GnModuleSchemaProdConf(
                    module.config.conf_schema_toml.GnModuleSchemaConf
                ):
                    pass

                conf_module = load_and_validate_toml(
                    str(f / "config/conf_gn_module.toml"), GnModuleSchemaProdConf
                )

                # add id_module and url_path to the module config
                update_module_config = dict(conf_module, **module_info.get(module_code))
                # register the module conf in the app config
                app.config[module_code] = update_module_config

                # import the blueprint
                python_module_name = "{}.backend.blueprint".format(module_path.name)
                module_blueprint = __import__(python_module_name, globals=globals())
                # register the confif in bluprint.config
                module.backend.blueprint.blueprint.config = update_module_config
                sys.path.pop(0)

                yield update_module_config, conf_manifest, module_blueprint
Beispiel #4
0
def load_config(config_file=None):
    """ Load the geonature configuration from a given file """
    # load and validate configuration
    configs_py = load_and_validate_toml(str(get_config_file_path(config_file)),
                                        GnPySchemaConf)

    # Settings also exported to backend
    configs_gn = load_and_validate_toml(str(get_config_file_path(config_file)),
                                        GnGeneralSchemaConf)

    return ChainMap({}, configs_py, configs_gn)
Beispiel #5
0
def run_install_gn_module(app, module_path, module_name, url):
    '''
        Installation du module en executant :
            configurations
            install_env.sh
            installation des dépendances python
            install_db.py
            install_app.py
    '''
    #   configs
    try:
        from conf_schema_toml import GnModuleSchemaConf
        load_and_validate_toml(
            Path(module_path) / "config/conf_gn_module.toml",
            GnModuleSchemaConf)
    except ImportError:
        log.info('No specific config file')
        pass

    #   requirements
    gn_module_import_requirements(module_path)

    #   ENV
    gn_file = Path(module_path) / "install_env.sh"
    log.info("run install_env.sh")

    try:
        subprocess.call([str(gn_file)], cwd=str(module_path))
        log.info("...%s\n", MSG_OK)
    except FileNotFoundError:
        pass
    except OSError as ex:

        if ex.errno == 8:
            raise GNModuleInstallError(
                ("Unable to execute '{}'. One possible reason is "
                 "the lack of shebang line.").format(gn_file))

        if os.access(str(gn_file), os.X_OK):
            # TODO: try to make it executable
            # TODO: change exception type
            # TODO: make error message
            # TODO: change print to log
            raise GNModuleInstallError("File {} not excecutable".format(
                str(gn_file)))

    #   APP
    gn_file = Path(module_path) / "install_gn_module.py"
    if gn_file.is_file():
        log.info("run install_gn_module.py")
        from install_gn_module import gnmodule_install_app
        gnmodule_install_app(DB, app)
        log.info("...%s\n", MSG_OK)
Beispiel #6
0
def load_config(config_file=None):
    """ Load the geonature configuration from a given file """
    # load and validate configuration
    configs_py = load_and_validate_toml(
        str(get_config_file_path(config_file)), GnPySchemaConf
    )

    # Settings also exported to backend
    configs_gn = load_and_validate_toml(
        str(get_config_file_path(config_file)), GnGeneralSchemaConf
    )

    return ChainMap({}, configs_py, configs_gn)
Beispiel #7
0
def import_gn_module(mod):
    sys.path.insert(
        0,
        str(GN_EXTERNAL_MODULE))  # to be able to import non-packaged modules
    try:
        module_name = mod.module_path
        module = import_module(module_name)
        module_path = Path(module.__file__).parent
        manifest_path = module_path / 'manifest.toml'
        module_config = {
            'ID_MODULE': mod.id_module,
            'MODULE_CODE': mod.module_code,
            'BACKEND_PATH': str(module_path / 'backend'),
            'FRONTEND_PATH': str(module_path / 'frontend'),
        }
        if manifest_path.is_file():  # non-packaged module
            module_manifest = load_and_validate_toml(
                module_path / 'manifest.toml', ManifestSchemaProdConf)
            module_schema = import_module(
                f'{module_name}.config.conf_schema_toml').GnModuleSchemaConf
            module_blueprint = import_module(
                f'{module_name}.backend.blueprint').blueprint
            config_path = str(module_path / "config/conf_gn_module.toml")
            module_config.update({
                'MODULE_URL':
                '/' + mod.module_path.replace(' ', ''),
            })
        else:
            module_blueprint = load_entry_point(module_name, 'gn_module',
                                                'blueprint')
            module_schema = load_entry_point(module_name, 'gn_module',
                                             'config_schema')
            config_path = os.environ.get(
                f'GEONATURE_{mod.module_code}_CONFIG_FILE')
            if not config_path:  # fallback to legacy conf path guessing
                # .parent.parent goes up 'backend/{module_name}'
                config_path = str(module_path.parent.parent /
                                  'config/conf_gn_module.toml')
            module_config.update({
                'MODULE_URL': '/' + mod.module_code.lower(),
            })
        module_config.update(load_and_validate_toml(config_path,
                                                    module_schema))
        #app.config[mod.module_code] = module_config
        module_blueprint.config = module_config
        return module, module_blueprint
    finally:
        sys.path.pop(0)
Beispiel #8
0
def check_manifest(module_path):
    """
        Verification de la version de geonature par rapport au manifest
        Retourne le code du module en majuscule
    """
    log.info("checking manifest")
    configs_py = utilstoml.load_and_validate_toml(
        str(Path(module_path) / "manifest.toml"), ManifestSchemaConf
    )

    gn_v = version.parse(GEONATURE_VERSION)
    if gn_v < version.parse(
        configs_py["min_geonature_version"]
    ) and gn_v > version.parse(configs_py["max_geonature_version"]):
        raise GeoNatureError(
            "Geonature version {} is imcompatible with module".format(GEONATURE_VERSION)
        )
    for e_gn_v in configs_py["exclude_geonature_versions"]:
        if gn_v == version.parse(e_gn_v):
            raise GeoNatureError(
                "Geonature version {} is imcompatible with module".format(
                    GEONATURE_VERSION
                )
            )
    log.info("...%s\n", MSG_OK)
    return configs_py["module_code"].upper()
Beispiel #9
0
def check_manifest(module_path):
    """
        Verification de la version de geonature par rapport au manifest
        Retourne le code du module en majuscule
    """
    log.info("checking manifest")
    configs_py = utilstoml.load_and_validate_toml(
        str(Path(module_path) / "manifest.toml"), ManifestSchemaConf
    )

    gn_v = version.parse(GEONATURE_VERSION)
    if gn_v < version.parse(
        configs_py["min_geonature_version"]
    ) and gn_v > version.parse(configs_py["max_geonature_version"]):
        raise GeoNatureError(
            "Geonature version {} is imcompatible with module".format(
                GEONATURE_VERSION)
        )
    for e_gn_v in configs_py["exclude_geonature_versions"]:
        if gn_v == version.parse(e_gn_v):
            raise GeoNatureError(
                "Geonature version {} is imcompatible with module".format(
                    GEONATURE_VERSION
                )
            )
    log.info("...%s\n", MSG_OK)
    return configs_py["module_code"].upper()
Beispiel #10
0
def import_packaged_module(module_dist, module_object):
    module_code = module_object.module_code
    module_dir = GN_EXTERNAL_MODULE / module_object.module_path
    frontend_path = os.environ.get(f'GEONATURE_{module_code}_FRONTEND_PATH',
                                   str(module_dir / 'frontend'))
    module_config = {
        'MODULE_CODE': module_code,
        'MODULE_URL': '/' + module_object.module_path,
        'FRONTEND_PATH': frontend_path,
    }

    try:
        module_schema = load_entry_point(module_dist, 'gn_module',
                                         'config_schema')
    except ImportError:
        pass
    else:
        config_path = os.environ.get(
            f'GEONATURE_{module_object.module_code}_CONFIG_FILE')
        if not config_path:  # fallback to legacy conf path guessing
            config_path = str(module_dir / 'config/conf_gn_module.toml')
        module_config.update(load_and_validate_toml(config_path,
                                                    module_schema))

    blueprint_entry_point = get_entry_info(module_dist, 'gn_module',
                                           'blueprint')
    if blueprint_entry_point:
        module_blueprint = blueprint_entry_point.load()
        module_blueprint.config = module_config
    else:
        module_blueprint = None
    return (module_object, module_config, module_blueprint)
Beispiel #11
0
def create_frontend_config(conf_file):
    configs_gn = load_and_validate_toml(conf_file, GnGeneralSchemaConf)

    with open(str(ROOT_DIR / 'frontend/src/conf/app.config.ts'),
              'w') as outputfile:
        outputfile.write("export const AppConfig = ")
        json.dump(configs_gn, outputfile, indent=True)
Beispiel #12
0
def create_frontend_config(conf_file):
    log.info("Generating configuration")
    configs_gn = load_and_validate_toml(conf_file, GnGeneralSchemaConf)

    with open(str(ROOT_DIR / "frontend/src/conf/app.config.ts"),
              "w") as outputfile:
        outputfile.write("export const AppConfig = ")
        json.dump(configs_gn, outputfile, indent=True)
    log.info("...%s\n", MSG_OK)
Beispiel #13
0
def create_frontend_config(conf_file):
    log.info('Generating configuration')
    configs_gn = load_and_validate_toml(conf_file, GnGeneralSchemaConf)

    with open(
        str(ROOT_DIR / 'frontend/src/conf/app.config.ts'), 'w'
    ) as outputfile:
        outputfile.write("export const AppConfig = ")
        json.dump(configs_gn, outputfile, indent=True)
    log.info("...%s\n", MSG_OK)
Beispiel #14
0
def list_and_import_gn_modules(app, mod_path=GN_EXTERNAL_MODULE):
    """
        Get all the module enabled from gn_commons.t_modules
    """
    with app.app_context():
        data = DB.session.query(TModules).filter(
            TModules.active_backend == True)
        enabled_modules = [d.as_dict()['module_name'] for d in data]

    # iter over external_modules dir
    #   and import only modules which are enabled
    for f in mod_path.iterdir():
        if f.is_dir():
            conf_manifest = load_and_validate_toml(str(f / 'manifest.toml'),
                                                   ManifestSchemaProdConf)
            module_name = conf_manifest['module_name']
            if module_name in enabled_modules:
                # TODO CHECK WHAT MODULE NAME BELOW MEAN
                # import du module dans le sys.path
                module_path = Path(GN_EXTERNAL_MODULE / module_name)
                module_parent_dir = str(module_path.parent)
                module_name = "{}.config.conf_schema_toml".format(
                    module_path.name)
                sys.path.insert(0, module_parent_dir)
                module = __import__(module_name, globals=globals())
                module_name = "{}.backend.blueprint".format(module_path.name)
                module_blueprint = __import__(module_name, globals=globals())
                sys.path.pop(0)

                class GnModuleSchemaProdConf(
                        module.config.conf_schema_toml.GnModuleSchemaConf,
                        GnModuleProdConf):
                    pass

                conf_module = load_and_validate_toml(
                    str(f / 'config/conf_gn_module.toml'),
                    GnModuleSchemaProdConf)

                yield conf_module, conf_manifest, module_blueprint
Beispiel #15
0
def create_module_config(app, module_code, mod_path=None, build=True):
    """
        Create the frontend config
    """
    from geonature.core.gn_commons.models import TModules

    with app.app_context():
        if not mod_path:
            # get the symlink location to write the config file
            mod_path = os.readlink(
                str(GN_EXTERNAL_MODULE / module_code.lower()))

        # fetch the module in the DB from its name
        module_object = (
            DB.session.query(TModules)
            .filter(TModules.module_code == module_code.upper())
            .one()
        )

        # import du module dans le sys.path
        module_parent_dir = str(Path(mod_path).parent)
        module_schema_conf = "{}.config.conf_schema_toml".format(
            Path(mod_path).name
        )  # noqa
        sys.path.insert(0, module_parent_dir)
        module = __import__(module_schema_conf, globals=globals())
        front_module_conf_file = os.path.join(
            mod_path, "config/conf_gn_module.toml"
        )  # noqa
        config_module = utilstoml.load_and_validate_toml(
            front_module_conf_file, module.config.conf_schema_toml.GnModuleSchemaConf
        )
        # set id_module and module_code
        config_module["ID_MODULE"] = module_object.id_module
        config_module["MODULE_CODE"] = module_object.module_code
        config_module["MODULE_URL"] = module_object.module_path.replace(
            " ", "")

        frontend_config_path = os.path.join(
            mod_path, "frontend/app/module.config.ts"
        )  # noqa
        try:
            with open(str(ROOT_DIR / frontend_config_path), "w") as outputfile:
                outputfile.write("export const ModuleConfig = ")
                json.dump(config_module, outputfile,
                          indent=True, sort_keys=True)
        except FileNotFoundError:
            log.info("No frontend config file")
        if build:
            build_geonature_front()
Beispiel #16
0
def create_frontend_config(conf_file):
    log.info("Generating configuration")
    configs_gn = load_and_validate_toml(conf_file, GnGeneralSchemaConf)

    with open(str(ROOT_DIR / "frontend/src/conf/app.config.ts.sample"),
              "r") as input_file:
        template = Template(input_file.read())
        parameters = json.dumps(configs_gn, indent=True)
        app_config_template = template.render(parameters=parameters)

        with open(str(ROOT_DIR / "frontend/src/conf/app.config.ts"),
                  "w") as output_file:
            output_file.write(app_config_template)

    log.info("...%s\n", MSG_OK)
Beispiel #17
0
def list_frontend_enabled_modules(mod_path=GN_EXTERNAL_MODULE):
    """
        Get all the module frontend enabled from gn_commons.t_modules
    """
    from geonature.utils.command import get_app_for_cmd
    from geonature.core.gn_commons.models import TModules

    app = get_app_for_cmd(DEFAULT_CONFIG_FILE, with_external_mods=False)
    with app.app_context():
        data = DB.session.query(TModules).filter(
            TModules.active_frontend == True).all()
        enabled_modules = [d.as_dict()['module_name'] for d in data]
    for f in mod_path.iterdir():
        if f.is_dir():
            conf_manifest = load_and_validate_toml(str(f / 'manifest.toml'),
                                                   ManifestSchemaProdConf)

            class GnModuleSchemaProdConf(GnModuleProdConf):
                pass

            conf_module = load_and_validate_toml(
                str(f / 'config/conf_gn_module.toml'), GnModuleSchemaProdConf)
            if conf_manifest['module_name'] in enabled_modules:
                yield conf_module, conf_manifest
Beispiel #18
0
def create_module_config(app, module_code, mod_path=None, build=True):
    """
        Create the frontend config
    """
    from geonature.core.gn_commons.models import TModules

    with app.app_context():
        if not mod_path:
            mod_path = str(GN_EXTERNAL_MODULE / module_code.lower())

        # fetch the module in the DB from its name
        module_object = (
            DB.session.query(TModules)
            .filter(TModules.module_code == module_code.upper())
            .one()
        )

        # import du module dans le sys.path
        module_parent_dir = str(Path(mod_path).parent)
        module_schema_conf = "{}.config.conf_schema_toml".format(
            Path(mod_path).name
        )  # noqa
        sys.path.insert(0, module_parent_dir)
        module = __import__(module_schema_conf, globals=globals())
        front_module_conf_file = os.path.join(
            mod_path, "config/conf_gn_module.toml"
        )  # noqa
        config_module = utilstoml.load_and_validate_toml(
            front_module_conf_file, module.config.conf_schema_toml.GnModuleSchemaConf
        )
        # set id_module and module_code
        config_module["ID_MODULE"] = module_object.id_module
        config_module["MODULE_CODE"] = module_object.module_code
        config_module["MODULE_URL"] = module_object.module_path.replace(" ", "")

        frontend_config_path = os.path.join(
            mod_path, "frontend/app/module.config.ts"
        )  # noqa
        try:
            with open(str(ROOT_DIR / frontend_config_path), "w") as outputfile:
                outputfile.write("export const ModuleConfig = ")
                json.dump(config_module, outputfile, indent=True, sort_keys=True)
        except FileNotFoundError:
            log.info("No frontend config file")
        if build:
            build_geonature_front()
Beispiel #19
0
def check_manifest(module_path):
    '''
        Verification de la version de geonature par rapport au manifest
        Retourne le nom du module
    '''
    log.info("checking manifest")
    configs_py = utilstoml.load_and_validate_toml(
        str(Path(module_path) / "manifest.toml"), ManifestSchemaConf)

    gn_v = version.parse(GEONATURE_VERSION)
    if (gn_v < version.parse(configs_py['min_geonature_version'])
            and gn_v > version.parse(configs_py['max_geonature_version'])):
        raise GeoNatureError(
            "Geonature version {} is imcompatible with module".format(
                GEONATURE_VERSION))
    for e_gn_v in configs_py['exclude_geonature_versions']:
        if gn_v == version.parse(e_gn_v):
            raise GeoNatureError(
                "Geonature version {} is imcompatible with module".format(
                    GEONATURE_VERSION))
    log.info("...%s\n", MSG_OK)
    return configs_py['module_name']
Beispiel #20
0
import os
from collections import ChainMap

from geonature.utils.config_schema import (
    GnGeneralSchemaConf,
    GnPySchemaConf,
)
from geonature.utils.utilstoml import load_and_validate_toml
from geonature.utils.env import DEFAULT_CONFIG_FILE

config_path = os.environ.get("GEONATURE_CONFIG_FILE", DEFAULT_CONFIG_FILE)
config_backend = load_and_validate_toml(config_path, GnPySchemaConf)
config_frontend = load_and_validate_toml(config_path, GnGeneralSchemaConf)
config = ChainMap({}, config_frontend, config_backend)
Beispiel #21
0
def get_module_id(module_name):
    conf_path = '{}/{}/config/conf_gn_module.toml'.format(
        GN_EXTERNAL_MODULE, module_name)
    return load_and_validate_toml(conf_path,
                                  GnModuleProdConf)['id_application']