1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 from .profile import Profile
22 from .delegator import Delegator
23
25 """
26 Allows easy creation of delegators with `Profile`s.
27
28 It also supports a form of inheritance. By specifying base
29 factories, a factory extends its bases' profiles (it takes the union of all
30 base profiles and its own). Using the inheritance you can add new profiles
31 and add attributes to existing profiles; you cannot, however, override
32 attributes.
33
34 Instance methods:
35
36 - `add_delegator_profile`: Add a `Profile`
37 - `Delegator`: Construct a `Delegator` from a stored profile
38 - `has_profile`: See if we have a particular profile
39 - `get_profile`: Get profile by name
40
41 Class invariants:
42
43 - Every profile has a unique name
44 """
45
47 """
48 Construct a `DelegatorFactory`, optionally with bases to inherit from.
49
50 Parameters:
51
52 `bases` :: (::DelegatorFactory...)
53 base factories for the created factory
54 """
55 self.__bases = bases
56 self.__profiles = {}
57
59 """
60 Add a new delegator factory `Profile` to the factory.
61
62 Parameters:
63
64 `name` :: string
65 name of the profile
66
67 `profile` :: Profile
68 the profile to add
69 """
70 assert name not in self.__profiles, \
71 "Tried to add profile that's already added to this list." + \
72 " A DelegatorFactory cannot contain the same profile twice"
73
74 self.__profiles[name] = profile
75
76 - def Delegator(self, profile_name = "default", target=None):
77 """
78 Construct a delegator with a stored `Profile`.
79
80 Parameters:
81
82 `profile_name` :: string
83 name of the `Profile` to use to set up the delegator with
84
85 `target` = None
86 target of the newly created delegator
87
88 Returns newly created delegator :: Delegator
89
90 Raises:
91
92 - `ValueError` when no profile with name `profile_name` exists
93 """
94 return Delegator(self.get_profile(profile_name), target)
95
97 """
98 See if profile with given name exists.
99
100 Parameters:
101
102 `name` :: string
103 name of the profile to look for
104
105 Returns True if the factory can find a profile by that name, False
106 otherwise
107 """
108 for base in self.__bases:
109 if base.has_profile(name):
110 return True
111
112 return name in self.__profiles
113
115 """
116 Get profile by name.
117
118 Parameters:
119
120 `name` :: string
121 name of the profile
122
123 Returns profile by name, taking into account base factories' profiles
124 :: Profile
125
126 Raises:
127 - `ValueError` when profile isn't found
128 """
129 profile = Profile()
130 found = False
131
132 if name in self.__profiles:
133 profile |= self.__profiles[name]
134 found = True
135
136 for base in self.__bases:
137 if base.has_profile(name):
138 profile |= base.get_profile(name)
139 found = True
140
141 if not found:
142 raise ValueError("Failed to find profile: %s" % name)
143
144 return profile
145