~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
3
4
"""Package relationships."""
5
1864 by Canonical.com Patch Queue Manager
moved images into their correct place. removed some crap from canonical/soyuz. [trivial]
6
__metaclass__ = type
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
7
__all__ = [
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
8
    'relationship_builder',
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
9
    'PackageRelationship',
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
10
    'PackageRelationshipSet',
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
11
    ]
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
12
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
13
import operator as std_operator
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
14
from zope.interface import implements
15
13130.1.12 by Curtis Hovey
Sorted imports.
16
from canonical.launchpad.webapp import canonical_url
13130.1.5 by Curtis Hovey
Moved packagesets to soyuz.
17
from lp.soyuz.interfaces.packagerelationship import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
18
    IPackageRelationship,
19
    IPackageRelationshipSet,
20
    )
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
21
22
23
def relationship_builder(relationship_line, parser, getter):
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
24
    """Parse relationship_line into a IPackageRelationshipSet.
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
25
26
    'relationship_line' is parsed via given 'parser' funcion
27
    It also lookup the corresponding URL via the given 'getter'.
28
    Return empty list if no line is given.
29
    """
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
30
    relationship_set = PackageRelationshipSet()
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
31
32
    if not relationship_line:
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
33
        return relationship_set
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
34
35
    parsed_relationships = [
36
        token[0] for token in parser(relationship_line)]
37
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
38
    for name, version, operator in parsed_relationships:
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
39
        target_object = getter(name)
40
        if target_object is not None:
41
            url = canonical_url(target_object)
42
        else:
43
            url = None
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
44
        relationship_set.add(name, operator, version, url)
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
45
46
    return relationship_set
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
47
48
49
class PackageRelationship:
50
    """See IPackageRelationship."""
51
52
    implements(IPackageRelationship)
53
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
54
    def __init__(self, name, operator, version, url=None):
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
55
        self.name = name
56
        self.version = version
3147.5.59 by Celso Providelo
Improve and test IPackageRelationship features.
57
        self.url = url
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
58
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
59
        if len(operator.strip()) == 0:
60
            self.operator = None
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
61
        else:
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
62
            self.operator = operator
2903.2.4 by Dafydd Harries
rename builddepsSet -> PackageRelationship, move IDownloadURL, other cleanups
63
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
64
65
class PackageRelationshipSet:
66
    """See IPackageRelationshipSet."""
67
    implements(IPackageRelationshipSet)
68
69
    def __init__(self):
70
        self.contents = []
71
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
72
    def add(self, name, operator, version, url):
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
73
        """See IPackageRelationshipSet."""
74
        self.contents.append(
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
75
            PackageRelationship(name, operator, version, url))
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
76
77
    def has_items(self):
78
        """See IPackageRelationshipSet."""
79
        return len(self.contents) is not 0
80
81
    def __iter__(self):
3147.5.61 by Celso Providelo
apply review comments (r=spiv), add 'parse_relationship_section' pagetest helper, order relationship set alphabetically and refactored the rest of related pagetemplates to use the new system.
82
        return iter(sorted(
83
            self.contents, key=std_operator.attrgetter('name')))
3147.5.60 by Celso Providelo
Land IPackageRelationshipSet and use it render relationship in parent pages.
84