Package pytilities :: Package delegation :: Module delegatorfactory
[hide private]
[frames] | no frames]

Source Code for Module pytilities.delegation.delegatorfactory

  1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
  2  #  
  3  # This file is part of pytilities. 
  4  #  
  5  # pytilities is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  #  
 10  # pytilities is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  #  
 15  # You should have received a copy of the GNU General Public License 
 16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
 17  # 
 18   
 19  __docformat__ = 'reStructuredText' 
 20   
 21  from . import Profile, Delegator 
 22   
23 -class DelegatorFactory(object):
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
42 - def __init__(self):
43 self.__profiles = {'default': Profile()}
44
45 - def set_profile(self, name, profile):
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
79 - def has_profile(self, name):
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
93 - def get_profile(self, name):
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