Beispiel #1
0
    def parse_for_resource(self, resource: str, config: dict):
        """
        Get the resource data for a single resource
        """

        dummy_resource = config.get(resource_constants.DUMMY_DEF)

        # Dummy resources are auto-generated by program.
        # It is expected that they would get their data through Automated Workflow
        # And so we do not need to fetch their data at this point
        if dummy_resource:
            result = []
        else:
            source = config.get(resource_constants.SOURCE,
                                resource_constants.DB_TABLE)

            if source == resource_constants.DB_TABLE:
                result = self.parse_db_source(
                    config, self.resource_map_resolver.globals)
            elif source == resource_constants.SCRIPT:
                result = self.parse_python_source(config)
            else:
                raise exceptions.ResourcesException(
                    f"Incorrect source defined for {resource}")

            if not isinstance(result, (list, tuple, set)):
                raise exceptions.ResourcesException(
                    f"Result for {resource} must be Built-in iterable")

        return result
Beispiel #2
0
    def get_function_from_mapping_file(func_name):
        map_hook_file = f"{settings.INPUT_FOLDER}.{settings.RES_MAPPING_HOOKS_FILE}"[:-len(
            ".py")]

        try:
            func = getattr(importlib.import_module(map_hook_file), func_name)
        except AttributeError:
            raise exceptions.ResourcesException(
                f"Function {func_name} not defined in Map Hooks File")

        return func
Beispiel #3
0
    def resolve_primary_resource(self, candidates: set):
        candidates = candidates or self.associated_resources

        # Only one candidate is there, so we have clear primary resource
        if len(candidates) == 1:
            self.primary_resource = candidates.pop()
            self.associated_resources.discard(self.primary_resource)
        elif not candidates:
            # There are no candidates, so just name resource after reference
            self.primary_resource = self.name
        else:
            # We cannot determine the best candidate
            raise exceptions.ResourcesException(
                f"Could not determine primary resource for {self.name}")
Beispiel #4
0
    def parse_python_source(self, config):

        func_name = config.get(resource_constants.FUNCTION)

        if not func_name:
            raise exceptions.ResourcesException(
                f"Function must be declared for {config}")

        func = self.get_function_from_mapping_file(func_name)

        args = config.get(resource_constants.ARGS, ())

        if not isinstance(args, (tuple, list)):
            raise exceptions.ResourcesException(
                f"Function {func_name} Args should be tuple/list")

        kwargs = config.get(resource_constants.KWARGS, {})

        if not isinstance(kwargs, dict):
            raise exceptions.ResourcesException(
                f"Function {func_name} Keyword Args should be dict")

        return func(*args, **kwargs)
Beispiel #5
0
    def construct_query(self, config, global_settings):
        table = config.get(resource_constants.TABLE,
                           global_settings.get(resource_constants.TABLE))

        if not table:
            raise exceptions.ResourcesException(
                f"Table not defined for {config}")

        column = config.get(resource_constants.COLUMN,
                            resource_constants.DEFAULT_COLUMN)
        filters = config.get(resource_constants.FILTERS,
                             global_settings.get(resource_constants.FILTERS))

        return self.construct_fetch_query(table, column, filters)