Beispiel #1
0
    def resolve_base(self, options):
        """
        Resolves a base object from a set of parent identifier options.
        """

        collection_candidate = self.check_connection()
        parnet_candidate_locator = 0
        base = None

        parnet_candidates = [
                key for key in options.keys()
                if OptionHelper.is_parent_id_option(key)
        ]
        parnet_candidates_permutations = list(itertools.permutations(parnet_candidates))

        if parnet_candidates_permutations[0]:
            for combination in parnet_candidates_permutations:
                for item in combination:
                    key = item
                    val = options[key]
                    parnet_candidate_locator += 1
                    typename = OptionHelper.get_parent_id_type(key)

                    coll = TypeHelper.to_plural(typename)
                    if not (TypeHelper.isKnownType(typename) or  TypeHelper.isKnownType(coll)):
                        self.error(Messages.Error.NO_SUCH_TYPE % typename)

                    if hasattr(collection_candidate, coll):
                        coll_ins = getattr(collection_candidate, coll)
                        if hasattr(coll_ins, 'get'):
                            if not val:
                                self.error(Messages.Error.INVALID_OPTION % key)
                            if key.endswith('-identifier'):
                                base = coll_ins.get(id=val)
                            elif key.endswith('-name'):
                                base = coll_ins.get(name=val)

                            if not base:
                                if len(combination) > parnet_candidate_locator:
                                    continue
                                else:
                                    self.error(Messages.Error.NO_SUCH_OBJECT % (typename, val))

                    if len(parnet_candidates) == parnet_candidate_locator:
                        return base
                    else:
                        collection_candidate = base

            self.error(Messages.Error.CANNOT_CONSTRUCT_COLLECTION_MEMBER_VIEW % str(parnet_candidates_permutations))
Beispiel #2
0
    def _resolve_base(self, args):
        """
        Resolves a base object from a set of '--parent-type-identifier'
        or '--parent-type-name' options.
        """
        parent_candidates = [item for item in args
                if OptionHelper.is_parent_id_option(item)]
        parent_candidates_permutations = list(itertools.permutations(parent_candidates))

        if parent_candidates_permutations[0]:
            for combination in parent_candidates_permutations:
                candidates = [OptionHelper.get_parent_id_type(item)
                        for item in combination
                        if OptionHelper.is_parent_id_option(item)]
                candidate = (''.join(candidates) + args[0]).lower()
                dt = TypeHelper.getDecoratorType(candidate)
                if dt: return dt

        return None
Beispiel #3
0
    def _resolve_base(self, args):
        """
        Resolves a base object from a set of '--parent-type-identifier'
        or '--parent-type-name' options.
        """
        parent_candidates = [
            item for item in args if OptionHelper.is_parent_id_option(item)
        ]
        parent_candidates_permutations = list(
            itertools.permutations(parent_candidates))

        if parent_candidates_permutations[0]:
            for combination in parent_candidates_permutations:
                candidates = [
                    OptionHelper.get_parent_id_type(item)
                    for item in combination
                    if OptionHelper.is_parent_id_option(item)
                ]
                candidate = (''.join(candidates) + args[0]).lower()
                dt = TypeHelper.getDecoratorType(candidate)
                if dt: return dt

        return None
Beispiel #4
0
    def resolve_base(self, options):
        """
        Resolves a base object from a set of parent identifier options.
        """

        # Initially the base is the connection:
        connection = self.check_connection()
        base = connection

        # Find all the options that are parent identifiers, and sort them
        # so that the process will be always the same regardless of the
        # order of the options dictionary:
        identifiers = [
            key for key in options.keys()
            if OptionHelper.is_parent_id_option(key)
        ]
        identifiers.sort()

        # Calculate the set of permutations of all the parent identifiers, as
        # we are going to try each permutation till we find one that results
        # in a valid base:
        permutations = list(itertools.permutations(identifiers))

        for permutation in permutations:

            # Restart the search from the connection for each permutation:
            base = connection

            for item in permutation:
                # Get the name and value of the current parent identifier:
                key = item
                value = options[key]

                # Calculate the type and collection names:
                type_name = OptionHelper.get_parent_id_type(key)
                collection_name = TypeHelper.to_plural(type_name)
                if not (TypeHelper.isKnownType(type_name)
                        or TypeHelper.isKnownType(collection_name)):
                    self.error(Messages.Error.NO_SUCH_TYPE % type_name)

                # Try to extract the next base from the current one:
                if hasattr(base, collection_name):
                    collection = getattr(base, collection_name)
                    if hasattr(collection, 'get'):
                        if not value:
                            self.error(Messages.Error.INVALID_OPTION % key)
                        if key.endswith('-identifier'):
                            base = collection.get(id=value)
                        elif key.endswith('-name'):
                            base = collection.get(name=value)
                        else:
                            base = None
                    else:
                        base = None
                else:
                    base = None

                # If we haven't been able to find a valid base for the current
                # parent identifier, then we should discard this permutation:
                if base is None:
                    break

            # If we already found a valid base, then we should discard the
            # rest of the permutations, i.e., the first valid permutation is
            # the winner:
            if base != None:
                break

        # Generate an error message if no permutation results in a valid base:
        if base is None:
            self.error(Messages.Error.CANNOT_CONSTRUCT_COLLECTION_MEMBER_VIEW %
                       str(permutations))

        # The connection is the starting point for the search, so if the
        # result is the connection we can conclude that there is no
        # base:
        if base == connection:
            base = None
        return base
Beispiel #5
0
    def resolve_base(self, options):
        """
        Resolves a base object from a set of parent identifier options.
        """

        # Initially the base is the connection:
        connection = self.check_connection()
        base = connection

        # Find all the options that are parent identifiers, and sort them
        # so that the process will be always the same regardless of the
        # order of the options dictionary:
        identifiers = [
            key for key in options.keys()
            if OptionHelper.is_parent_id_option(key)
        ]
        identifiers.sort()

        # Calculate the set of permutations of all the parent identifiers, as
        # we are going to try each permutation till we find one that results
        # in a valid base:
        permutations = list(itertools.permutations(identifiers))

        for permutation in permutations:

            # Restart the search from the connection for each permutation:
            base = connection

            for item in permutation:
                # Get the name and value of the current parent identifier:
                key = item
                value = options[key]

                # Calculate the type and collection names:
                type_name = OptionHelper.get_parent_id_type(key)
                collection_name = TypeHelper.to_plural(type_name)
                if not (TypeHelper.isKnownType(type_name) or TypeHelper.isKnownType(collection_name)):
                    self.error(Messages.Error.NO_SUCH_TYPE % type_name)

                # Try to extract the next base from the current one:
                if hasattr(base, collection_name):
                    collection = getattr(base, collection_name)
                    if hasattr(collection, 'get'):
                        if not value:
                            self.error(Messages.Error.INVALID_OPTION % key)
                        if key.endswith('-identifier'):
                            base = collection.get(id=value)
                        elif key.endswith('-name'):
                            base = collection.get(name=value)
                        else:
                            base = None
                    else:
                        base = None
                else:
                    base = None

                # If we haven't been able to find a valid base for the current
                # parent identifier, then we should discard this permutation:
                if base is None:
                    break

            # If we already found a valid base, then we should discard the
            # rest of the permutations, i.e., the first valid permutation is
            # the winner:
            if base != None:
                break

        # Generate an error message if no permutation results in a valid base:
        if base is None:
            self.error(Messages.Error.CANNOT_CONSTRUCT_COLLECTION_MEMBER_VIEW % str(permutations))

        # The connection is the starting point for the search, so if the
        # result is the connection we can conclude that there is no
        # base:
        if base == connection:
            base = None
        return base