Ejemplo n.º 1
0
 def get_packages(self, pkg_name=None):
     """
     Return all public packages.
     """
     if pkg_name and is_protected(pkg_name):
         pkg_name = None
     if pkg_name and pkg_name in self:
         pkgs = [pkg_name]
     else:
         pkgs = set(pk.name for pk in self.values() if not is_protected(pk.name))
     return [self[p] for p in pkgs]
Ejemplo n.º 2
0
    def update_category(self, package):
        """ Update the category dictionary with package contents """

        for nf in package.values():
            # skip the deprecated name (starting with #)
            if is_protected(nf.name):
                continue

            # empty category
            if not nf.category:
                nf.category = "Unclassified"

            # parse the category
            for c in nf.category.split(","):
                # we work in lower case by convention
                c = c.strip().lower()

                # search for the sub category (split by .)
                try:
                    c_root, c_others = c.split('.', 1)
                except:  # if there is no '.', c_others is empty
                    c_root = c
                    c_others = ''

                if c_root in self.user_category.keywords:
                    # reconstruct the name of the category
                    c_temp = self.user_category.keywords[c_root] + '.' + c_others.title()
                    self.category.add_name(c_temp, nf)
                else:
                    self.category.add_name(c.title(), nf)
Ejemplo n.º 3
0
    def _dependencies(self, factory):
        f = factory
        if not f.is_composite_node():
            return

        for p, n in f.elt_factory.values():
            if is_protected(p) or is_protected(n):
                continue
            try:
                fact = self[p][n]
            except:
                # print p, n
                continue
            yield fact

            for df in self._dependencies(fact):
                yield df
Ejemplo n.º 4
0
 def get_data(self, pattern='*.*', pkg_name=None, as_paths=False):
     """ Return all data that match the pattern. """
     pkgs = self.get_packages(pkg_name)
     datafiles = [(pj(p.path, f.name) if as_paths else f) for p in pkgs
                  for f in p.values()
                  if (not is_protected(f.name) and f.is_data()
                      and fnmatch(f.name, pattern))]
     return datafiles
Ejemplo n.º 5
0
    def _missing_dependencies(self, factory, l=[]):

        f = factory
        if not f.is_composite_node():
            return

        for p, n in f.elt_factory.values():
            if is_protected(p) or is_protected(n):
                continue
            try:
                fact = self[p][n]
            except:
                l.append((p, n))
                continue
            yield fact

            for df in self._missing_dependencies(fact, l):
                yield df
Ejemplo n.º 6
0
    def get_pseudo_pkg(self):
        """ Return a pseudopackage structure """

        pt = PseudoPackage('Root')

        # Build the name tree (on uniq objects)
        for k, v in self.pkgs.items():
            if(not is_protected(k)):
                pt.add_name(k, v)

        return pt
Ejemplo n.º 7
0
    def search_node(self, search_str, nb_inputs=-1, nb_outputs=-1):
        """
        Return a list of Factory corresponding to search_str
        If nb_inputs or nb_outputs is specified,
        return only node with the same number of (in/out) ports

        The results are sorted in the following way:
          1 - Highest Priority : presence of search_str in factory name
                           and position in the name (closer to the
                           begining = higher score)
          2 - Then : Number of occurences of search_str in the factory
              description.
          3 - Then : Number of occurences of search_str in the category name
          4 - Finally : presence of search_str in package name and position
              in the name (close to the begining = higher score)
        """

        search_str = search_str.upper()

        match = []

        # Search for each package and for each factory
        for name, pkg in self.items():
            if is_protected(name):
                continue  # alias

            for fname, factory in pkg.items():
                if is_protected(fname): 
                    continue  # alias

                # -- The scores for each string that is explored.
                # They are long ints because we make a 96 bits bitshift
                # to compute the final score --
                facNameScore = 0
                facDescScore = 0
                facCateScore = 0
                pkgNameScore = 0

                fname = factory.name.upper()
                if search_str in fname:
                    l = float(len(fname))
                    facNameScore = int(100 * (1 - fname.index(search_str) / l))

                facDescScore = int(factory.description.upper().count(search_str))
                facCateScore = int(factory.category.upper().count(search_str))

                pname = pkg.name.upper()
                if search_str in pname:
                    l = float(len(pname))
                    pkgNameScore = int(100 * (1 - pname.index(search_str) / l))
                # A left shift by n bits is equivalent to multiplication by pow(2, n)
                score = (int(facNameScore * pow(2, 32 * 3)) | int(facDescScore * pow(2, 32 * 2)) | 
                         int(facCateScore * pow(2, 32 * 1)) | pkgNameScore)
                if score > 0:
                    match.append((score, factory))

        # Filter ports
        if(nb_inputs >= 0):
            match = [sc_x for sc_x in match if sc_x[1] and sc_x[1].inputs and len(sc_x[1].inputs) == nb_inputs]
        if(nb_outputs >= 0):
            match = [sc_x for sc_x in match if sc_x[1] and sc_x[1].outputs and len(sc_x[1].outputs) == nb_outputs]

        if not len(match):
            return match

        match.sort(reverse=True)
        match = list(zip(*match))[1]

        return match