~bcsaller/pyjuju/subordinate-endpoints

« back to all changes in this revision

Viewing changes to juju/state/endpoint.py

  • Committer: Benjamin Saller
  • Date: 2011-12-16 02:20:56 UTC
  • Revision ID: bcsaller@gmail.com-20111216022056-1j7jiaic8l31tkgz
move to non-namedtuple and default kw arguments

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
low-level perspective of service states and relation states.
4
4
"""
5
5
 
6
 
from collections import namedtuple
7
 
 
8
 
 
9
 
class RelationEndpoint(
10
 
    # This idiom allows for the efficient and simple construction of
11
 
    # value types based on tuples; notably __eq__ and __hash__ are
12
 
    # correctly defined for value types. In addition, the storage
13
 
    # overhead is that of an ordinary tuple (although not so important
14
 
    # for this usage). See urlib/parse.py in Python 3.x for other
15
 
    # examples.
16
 
    namedtuple(
17
 
        "RelationEndpoint",
18
 
        "service_name relation_type relation_name relation_role subordinate")):
19
 
 
20
 
    __slots__ = ()
 
6
# Relation Scopes
 
7
GLOBAL = 1
 
8
CONTAINED = 2
 
9
 
 
10
 
 
11
class RelationEndpoint(object):
 
12
    def __init__(self, service_name,
 
13
                 relation_type,
 
14
                 relation_name,
 
15
                 relation_role,
 
16
                 relation_scope=GLOBAL):
 
17
        self.service_name = service_name
 
18
        self.relation_type = relation_type
 
19
        self.relation_name = relation_name
 
20
        self.relation_role = relation_role
 
21
        self.relation_scope = relation_scope
 
22
 
 
23
    def _tuple(self):
 
24
        return (self.service_name,
 
25
                self.relation_type,
 
26
                self.relation_role,
 
27
                self.relation_scope)
 
28
 
 
29
    def __hash__(self):
 
30
        return hash(self._tuple())
 
31
 
 
32
    def __cmp__(self, other):
 
33
        if not isinstance(other, RelationEndpoint):
 
34
            raise TypeError("Expected RelationEndpoint, got %s" %
 
35
                            type(other))
 
36
        return cmp(self._tuple(), other._tuple())
21
37
 
22
38
    def may_relate_to(self, other):
23
39
        """Test whether the `other` endpoint may be used in a common relation.