~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=E0611,W0212

__metaclass__ = type
__all__ = [
    'Component',
    'ComponentSelection',
    'ComponentSet'
    ]

from sqlobject import (
    ForeignKey,
    StringCol,
    )
from zope.interface import implements

from canonical.database.sqlbase import SQLBase
from lp.app.errors import NotFoundError
from lp.soyuz.interfaces.component import (
    IComponent,
    IComponentSelection,
    IComponentSet,
    )


class Component(SQLBase):
    """See IComponent."""

    implements(IComponent)

    _defaultOrder = ['id']

    name = StringCol(notNull=True, alternateID=True)

    def __repr__(self):
        return "<%s '%s'>" % (self.__class__.__name__, self.name)


class ComponentSelection(SQLBase):
    """See IComponentSelection."""

    implements(IComponentSelection)

    _defaultOrder = ['id']

    distroseries = ForeignKey(dbName='distroseries',
                               foreignKey='DistroSeries', notNull=True)
    component = ForeignKey(dbName='component',
                           foreignKey='Component', notNull=True)


class ComponentSet:
    """See IComponentSet."""

    implements(IComponentSet)

    def __iter__(self):
        """See IComponentSet."""
        return iter(Component.select())

    def __getitem__(self, name):
        """See IComponentSet."""
        component = Component.selectOneBy(name=name)
        if component is not None:
            return component
        raise NotFoundError(name)

    def get(self, component_id):
        """See IComponentSet."""
        return Component.get(component_id)

    def ensure(self, name):
        """See IComponentSet."""
        component = Component.selectOneBy(name=name)
        if component is not None:
            return component
        return self.new(name)

    def new(self, name):
        """See IComponentSet."""
        return Component(name=name)