Esempio n. 1
0
    def testUniq(self):
        in_lst = ["B", "A", "B", "a", "1", "b", "/", "", "2"]
        expected = ["B", "A", "a", "1", "b", "/", "", "2"]
        result = Array.uniq(in_lst)
        self.assertEqual(result, expected)

        in_lst = {
            "B": 0,
            "A": 1,
            "B": 0,
            "a": 2,
            "1": 3,
            "b": 4,
            "/": 5,
            "": "d",
            "2": 6
        }
        expected = {
            "B": 0,
            "A": 1,
            "a": 2,
            "1": 3,
            "b": 4,
            "/": 5,
            "": "d",
            "2": 6
        }
        result = Array.uniq(in_lst)
        self.assertEqual(result, expected)
Esempio n. 2
0
    def testUniq(self):
        in_lst = ["B", "A", "B", "a", "1", "b", "/", "", "2"];
        expected = ["B", "A", "a", "1", "b", "/", "", "2"];
        result = Array.uniq(in_lst);
        self.assertEqual(result, expected);

        in_lst = {"B":0, "A":1, "B":0, "a":2, "1":3, "b":4, "/":5, "":"d", "2":6};
        expected = {"B":0, "A":1, "a":2, "1":3, "b":4, "/":5, "":"d", "2":6};
        result = Array.uniq(in_lst);
        self.assertEqual(result, expected);
Esempio n. 3
0
    def __isInlineableDefinition(self, container, identifier, definition):
        """Checks if the definition is inlineable.:

        @param ContainerBuilder container
        @param string           identifier
        @param Definition       definition

        @return Boolean If the definition is inlineable

        """
        assert isinstance(definition, Definition);
        assert isinstance(container, ContainerBuilder);

        if (ContainerInterface.SCOPE_PROTOTYPE == definition.getScope()) :
            return True;


        if (definition.isPublic()) :
            return False;


        if ( not self.__graph.hasNode(identifier)) :
            return True;


        ids = list();
        for edge in self.__graph.getNode(identifier).getInEdges():
            ids.append(edge.getSourceNode().getId());


        if (len(Array.uniq(ids)) > 1) :
            return False;


        return container.getDefinition(ids[0]).getScope() == definition.getScope();
Esempio n. 4
0
    def __isInlineableDefinition(self, container, identifier, definition):
        """Checks if the definition is inlineable.:

        @param ContainerBuilder container
        @param string           identifier
        @param Definition       definition

        @return Boolean If the definition is inlineable

        """
        assert isinstance(definition, Definition)
        assert isinstance(container, ContainerBuilder)

        if (ContainerInterface.SCOPE_PROTOTYPE == definition.getScope()):
            return True

        if (definition.isPublic()):
            return False

        if (not self.__graph.hasNode(identifier)):
            return True

        ids = list()
        for edge in self.__graph.getNode(identifier).getInEdges():
            ids.append(edge.getSourceNode().getId())

        if (len(Array.uniq(ids)) > 1):
            return False

        return container.getDefinition(
            ids[0]).getScope() == definition.getScope()
Esempio n. 5
0
    def getResources(self):
        """Returns an array of resources loaded to build this collection.

        @return: ResourceInterface[] An array of resources

        """

        return Array.uniq(self.__resources, str)
Esempio n. 6
0
    def getServiceIds(self):
        """Gets all service ids.

        @return: list A list of all defined service ids

        """
        return Array.uniq(
            list(self.getDefinitions().keys()) +\
            list(self.getAliases().keys()) +\
            Container.getServiceIds(self)
        )
Esempio n. 7
0
    def values(self, values):
        assert isinstance(values, list);

        values = Array.uniq(values);

        if (len(values) <= 1) :
            raise InvalidArgumentException('.values() must be called with at least two distinct values.');


        self.__values = values;

        return self;
Esempio n. 8
0
    def process(self, container):
        """Processes the ContainerBuilder to remove unused definitions.

        @param ContainerBuilder container

        """
        assert isinstance(container, ContainerBuilder);

        compiler = container.getCompiler();
        formatter = compiler.getLoggingFormatter();
        graph = compiler.getServiceReferenceGraph();

        hasChanged = False;
        definitions = container.getDefinitions().copy();
        for identifier, definition in definitions.items():
            if (definition.isPublic()) :
                continue;


            if (graph.hasNode(identifier)) :
                edges = graph.getNode(identifier).getInEdges();
                referencingAliases = list();
                sourceIds = list();
                for edge in edges:
                    node = edge.getSourceNode();
                    sourceIds.append(node.getId());

                    if (node.isAlias()) :
                        referencingAliases.append(node.getValue());


                isReferenced = (len(Array.uniq(sourceIds)) - len(referencingAliases)) > 0;
            else :
                referencingAliases = list();
                isReferenced = False;


            if (1 == len(referencingAliases) and False is isReferenced) :
                container.setDefinition(str(referencingAliases[0]), definition);
                definition.setPublic(True);
                container.removeDefinition(identifier);
                compiler.addLogMessage(formatter.formatRemoveService(self, identifier, 'replaces alias '+referencingAliases[0]));
            elif (0 == len(referencingAliases) and False is isReferenced) :
                container.removeDefinition(identifier);
                compiler.addLogMessage(formatter.formatRemoveService(self, identifier, 'unused'));
                hasChanged = True;



        if (hasChanged) :
            self.__repeatedPass.setRepeat();
Esempio n. 9
0
    def process(self, container):
        """Processes the ContainerBuilder to remove unused definitions.

        @param ContainerBuilder container

        """
        assert isinstance(container, ContainerBuilder)

        compiler = container.getCompiler()
        formatter = compiler.getLoggingFormatter()
        graph = compiler.getServiceReferenceGraph()

        hasChanged = False
        definitions = container.getDefinitions().copy()
        for identifier, definition in definitions.items():
            if (definition.isPublic()):
                continue

            if (graph.hasNode(identifier)):
                edges = graph.getNode(identifier).getInEdges()
                referencingAliases = list()
                sourceIds = list()
                for edge in edges:
                    node = edge.getSourceNode()
                    sourceIds.append(node.getId())

                    if (node.isAlias()):
                        referencingAliases.append(node.getValue())

                isReferenced = (len(Array.uniq(sourceIds)) -
                                len(referencingAliases)) > 0
            else:
                referencingAliases = list()
                isReferenced = False

            if (1 == len(referencingAliases) and False is isReferenced):
                container.setDefinition(str(referencingAliases[0]), definition)
                definition.setPublic(True)
                container.removeDefinition(identifier)
                compiler.addLogMessage(
                    formatter.formatRemoveService(
                        self, identifier,
                        'replaces alias ' + referencingAliases[0]))
            elif (0 == len(referencingAliases) and False is isReferenced):
                container.removeDefinition(identifier)
                compiler.addLogMessage(
                    formatter.formatRemoveService(self, identifier, 'unused'))
                hasChanged = True

        if (hasChanged):
            self.__repeatedPass.setRepeat()
Esempio n. 10
0
    def getServiceIds(self):
        """Gets all service ids.

        @return: list An array of all defined service ids

        """

        ids = list()
        r = ReflectionObject(self)
        for method in r.getMethods():
            match = re.search('^get(.+)Service$', method.getName())
            if match:
                ids.append(self.underscore(match.group(1)))

        return Array.uniq(ids + list(self._services.keys()))
Esempio n. 11
0
    def __init__(self, name, parent = None, values = None):
        if values is None:
            values = list();
        assert isinstance(values, list);
        if parent:
            assert isinstance(parent, NodeInterface);

        self.__values = None;

        values = Array.uniq(values);
        if (len(values) <= 1) :
            raise InvalidArgumentException('values must contain at least two distinct elements.');


        ScalarNode.__init__(self, name, parent);
        self.__values = values;
Esempio n. 12
0
    def __init__(self, name, parent=None, values=None):
        if values is None:
            values = list()
        assert isinstance(values, list)
        if parent:
            assert isinstance(parent, NodeInterface)

        self.__values = None

        values = Array.uniq(values)
        if (len(values) <= 1):
            raise InvalidArgumentException(
                'values must contain at least two distinct elements.')

        ScalarNode.__init__(self, name, parent)
        self.__values = values
Esempio n. 13
0
    def getServiceConditionals(cls, value):
        """Returns the Service Conditionals.

        @param: mixed value An array of conditionals to return.

        @return array An array of Service conditionals

        """
        services = list()

        if isinstance(value, list):
            for v in value:
                iterable = cls.getServiceConditionals(v)
                services.extend(iterable)
                services = Array.uniq(services)
        elif isinstance(value, Reference):
            services.append(str(value))

        return services
Esempio n. 14
0
    def locate(self, name, currentPath = None, first = True):
        """Returns a full path for a given file name.

        @param name: mixed The file name to locate
        @param currentPath: string The current path
        @param first: boolean Whether to return the first occurrence 
                      or an array of filenames

        @return: string|list The full path to the file|A list of file paths

        @raise InvalidArgumentException: When file is not found

        """
        if self.__isAbsolutePath(name):
            if not os.path.exists(name):
                raise InvalidArgumentException(
                    'The file "{0}" does not exist.'.format(name)
                );
            return name;

        filepaths = list();

        paths = [];
        if currentPath:
            paths.append(currentPath);
        paths.extend(self._paths);

        for path in paths:
            filename = os.path.join(path, name);
            if os.path.exists(filename):
                if first:
                    return filename;
                filepaths.append(filename);

        if not filepaths:
            raise InvalidArgumentException(
                'The file "{0}" does not exist (in: {1}).'
                ''.format(name, ", ".join(paths))
            );

        return Array.uniq(filepaths);
Esempio n. 15
0
    def locate(self, name, currentPath=None, first=True):
        """Returns a full path for a given file name.

        @param name: mixed The file name to locate
        @param currentPath: string The current path
        @param first: boolean Whether to return the first occurrence 
                      or an array of filenames

        @return: string|list The full path to the file|A list of file paths

        @raise InvalidArgumentException: When file is not found

        """
        if self.__isAbsolutePath(name):
            if not os.path.exists(name):
                raise InvalidArgumentException(
                    'The file "{0}" does not exist.'.format(name))
            return name

        filepaths = list()

        paths = []
        if currentPath:
            paths.append(currentPath)
        paths.extend(self._paths)

        for path in paths:
            filename = os.path.join(path, name)
            if os.path.exists(filename):
                if first:
                    return filename
                filepaths.append(filename)

        if not filepaths:
            raise InvalidArgumentException(
                'The file "{0}" does not exist (in: {1}).'
                ''.format(name, ", ".join(paths)))

        return Array.uniq(filepaths)