1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 __docformat__ = 'reStructuredText'
20
21 from . import Profile, Delegator
22
24 """
25 Allows easy creation of delegators with `Profile`s.
26
27 By default already has a default profile.
28
29 Instance methods:
30
31 - `set_profile`: Add/replace a `Profile`
32 - `Delegator`: Construct a `Delegator` from a stored profile
33 - `has_profile`: See if we have a particular profile
34 - `get_profile`: Get profile by name
35
36 Class invariants:
37
38 - Every profile has a unique name
39 - There's always a name='default' profile
40 """
41
43 self.__profiles = {'default': Profile()}
44
46 """
47 Add/replace a delegator factory `Profile` to/on the factory.
48
49 Parameters:
50
51 `name` :: string
52 name of the profile
53
54 `profile` :: Profile
55 the profile for addition/replacement
56 """
57 self.__profiles[name] = profile
58
59 - def Delegator(self, profile_name = "default", target=None):
60 """
61 Construct a delegator with a stored `Profile`.
62
63 Parameters:
64
65 `profile_name` :: string
66 name of the `Profile` to use to set up the delegator with
67
68 `target` = None
69 target of the newly created delegator
70
71 Returns newly created delegator :: Delegator
72
73 Raises:
74
75 - `ValueError` when no profile with name `profile_name` exists
76 """
77 return Delegator(self.get_profile(profile_name), target)
78
80 """
81 See if profile with given name exists.
82
83 Parameters:
84
85 `name` :: string
86 name of the profile to look for
87
88 Returns True if the factory can find a profile by that name, False
89 otherwise
90 """
91 return name in self.__profiles
92
94 """
95 Get profile by name.
96
97 Parameters:
98
99 `name` :: string
100 name of the profile
101
102 Returns bound profile by name :: Profile
103
104 Raises:
105 - `ValueError` when profile isn't found
106 """
107 try:
108 return self.__profiles[name]
109 except KeyError:
110 raise ValueError("Failed to find profile: %s" % name)
111