Exemplo n.º 1
0
def generate_code(**kw):
    resource_to_name = {}
    resources = []
    for name, resource in compat.iteritems(kw):
        resource_to_name[(resource.library, resource.relpath)] = name
        resources.append(resource)

    # libraries with the same name are the same libraries
    libraries = {}
    for resource in resources:
        libraries[resource.library.name] = resource.library
        for mode_name, mode_resource in compat.iteritems(resource.modes):
            libraries[mode_resource.library.name] = mode_resource.library
    libraries = sorted(compat.itervalues(libraries),
                       key=lambda library: library.name)

    result = []
    # import on top
    result.append("from fanstatic import Library, Resource")
    result.append("")
    result.append("# This code is auto-generated and not PEP8 compliant")
    result.append("")
    # define libraries
    for library in libraries:
        result.append("%s = Library('%s', '%s')" %
                      (library.name, library.name, library.rootpath))
    result.append("")

    # sort resources in the order we want them to be
    resources = sort_resources_topological(resources)

    # now generate resource code
    for resource in resources:
        s = "%s = Resource(%s, '%s'" % (resource_to_name[
            (resource.library, resource.relpath)], resource.library.name,
                                        resource.relpath)
        if resource.depends:
            depends_s = ', depends=[%s]' % ', '.join([
                resource_to_name[(d.library, d.relpath)]
                for d in resource.depends
            ])
            s += depends_s
        if resource.supersedes:
            supersedes_s = ', supersedes=[%s]' % ', '.join([
                resource_to_name[(i.library, i.relpath)]
                for i in resource.supersedes
            ])
            s += supersedes_s
        if resource.modes:
            items = []
            for mode_name, mode in compat.iteritems(resource.modes):
                items.append(
                    (mode_name, generate_inline_resource(mode, resource)))
            items = sorted(items)
            modes_s = ', %s' % ', '.join(
                ["%s=%s" % (name, mode) for (name, mode) in items])
            s += modes_s
        s += ')'
        result.append(s)
    return '\n'.join(result)
Exemplo n.º 2
0
    def init_library_nr(self):
        """This can only be called once all resources are known.

        i.e. once sort_resources is called this can be called.
        once library numbers are calculated once this will be done
        very quickly.
        """
        # if there already is a known library nr, we're done
        if self.library_nr is not None:
            return
        # the maximum library number is the maximum number of the
        # depending libraries + 1
        max_library_nr = 0
        for resource in compat.itervalues(self.known_resources):
            for depend in resource.depends:
                for asset in depend.list_assets():
                    # we don't care about resources in the same library
                    if asset.library is self:
                        continue
                    # assign library number of library we are dependent on
                    # recursively if necessary
                    if asset.library.library_nr is None:
                        asset.library.init_library_nr()
                    max_library_nr = max(max_library_nr,
                                         asset.library.library_nr + 1)
        self.library_nr = max_library_nr
Exemplo n.º 3
0
def generate_code(**kw):
    resource_to_name = {}
    resources = []
    for name, resource in compat.iteritems(kw):
        resource_to_name[(resource.library, resource.relpath)] = name
        resources.append(resource)

    # libraries with the same name are the same libraries
    libraries = {}
    for resource in resources:
        libraries[resource.library.name] = resource.library
        for mode_name, mode_resource in compat.iteritems(resource.modes):
            libraries[mode_resource.library.name] = mode_resource.library
    libraries = sorted(compat.itervalues(libraries), key=lambda library: library.name)

    result = []
    # import on top
    result.append("from fanstatic import Library, Resource")
    result.append("")
    result.append("# This code is auto-generated and not PEP8 compliant")
    result.append("")
    # define libraries
    for library in libraries:
        result.append("%s = Library('%s', '%s')" %
                      (library.name, library.name, library.rootpath))
    result.append("")

    # sort resources in the order we want them to be
    resources = sort_resources_topological(resources)

    # now generate resource code
    for resource in resources:
        s = "%s = Resource(%s, '%s'" % (
            resource_to_name[(resource.library, resource.relpath)],
            resource.library.name,
            resource.relpath)
        if resource.depends:
            depends_s = ', depends=[%s]' % ', '.join(
                [resource_to_name[(d.library, d.relpath)] for d in resource.depends])
            s += depends_s
        if resource.supersedes:
            supersedes_s = ', supersedes=[%s]' % ', '.join(
                [resource_to_name[(i.library, i.relpath)] for i in resource.supersedes])
            s += supersedes_s
        if resource.modes:
            items = []
            for mode_name, mode in compat.iteritems(resource.modes):
                items.append((mode_name,
                              generate_inline_resource(mode, resource)))
            items = sorted(items)
            modes_s = ', %s' % ', '.join(["%s=%s" % (name, mode) for
                                          (name, mode) in items])
            s += modes_s
        s += ')'
        result.append(s)
    return '\n'.join(result)
Exemplo n.º 4
0
    def init_library_nr(self):
        """This can only be called once all resources are known.

        i.e. once sort_resources is called this can be called.
        once library numbers are calculated once this will be done
        very quickly.
        """
        # if there already is a known library nr, we're done
        if self.library_nr is not None:
            return
        # the maximum library number is the maximum number of the
        # depending libraries + 1
        max_library_nr = 0
        for resource in compat.itervalues(self.known_resources):
            for dep in resource.depends:
                # we don't care about resources in the same library
                if dep.library is self:
                    continue
                # assign library number of library we are dependent on
                # recursively if necessary
                if dep.library.library_nr is None:
                    dep.library.init_library_nr()
                max_library_nr = max(max_library_nr, dep.library.library_nr + 1)
        self.library_nr = max_library_nr