Package gofer :: Package rmi :: Module decorators
[hide private]
[frames] | no frames]

Source Code for Module gofer.rmi.decorators

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 14  # 
 15   
 16   
 17  """ 
 18  Provides decorator classes & funcitons. 
 19  """ 
 20   
 21  from gofer import NAME 
 22  from gofer.messaging import Options 
 23  from gofer.collator import Collator 
24 25 26 -class Remote:
27 """ 28 @cvar functions: The list of decorated functions. 29 """ 30 functions = [] 31 32 @classmethod
33 - def add(cls, fn):
34 cls.functions.append(fn)
35 36 @classmethod
37 - def purge(cls, mod):
38 purged = [] 39 for fn in cls.find(mod): 40 purged.append(fn) 41 for fn in purged: 42 cls.functions.remove(fn)
43 44 @classmethod
45 - def find(cls, mod):
46 for fn in cls.functions: 47 if fn.__module__ == mod: 48 yield fn
49 50 @classmethod
51 - def clear(cls):
52 cls.functions = []
53 54 @classmethod
55 - def collated(cls):
56 collated = [] 57 c = Collator() 58 classes, functions = c.collate(cls.functions) 59 for c in classes.keys(): 60 collated.append(c) 61 for m in functions.keys(): 62 collated.append(m) 63 return collated
64
65 -def __options(fn):
66 """ 67 Ensure funtion has the gofer options attribute 68 and return it. 69 @param fn: A function 70 @return: The funtion options object. 71 @rtype: L{Options} 72 """ 73 if not hasattr(fn, NAME): 74 opt = Options() 75 opt.security = [] 76 setattr(fn, NAME, opt) 77 else: 78 opt = getattr(fn, NAME) 79 return opt
80
81 82 -def remote(*args, **kwargs):
83 """ 84 Remote method decorator. 85 keywords: 86 - shared: method shared across plugins. 87 - secret: authorization secret. 88 """ 89 shared = bool(kwargs.get('shared', 1)) 90 secret = kwargs.get('secret',()) 91 def df(fn): 92 opt = __options(fn) 93 opt.shared = shared 94 if secret: 95 required = Options() 96 required.secret = secret 97 auth = ('secret', required) 98 opt.security.append(auth) 99 Remote.add(fn) 100 return fn
101 if args: 102 return df(args[0]) 103 else: 104 return df 105
106 107 -def pam(*args, **kwargs):
108 """ 109 PAM authentication method decorator. 110 keywords: 111 - user: user name. 112 - service: (optional) PAM service. 113 """ 114 user = kwargs.get('user') 115 service = kwargs.get('service') 116 if not user: 117 raise Exception('(user) must be specified') 118 def df(fn): 119 opt = __options(fn) 120 required = Options() 121 required.user = user 122 required.service = service 123 auth = ('pam', required) 124 opt.security.append(auth) 125 return fn
126 if args: 127 return df(args[0]) 128 else: 129 return df 130
131 132 -def user(*args, **kwargs):
133 """ 134 user (PAM) authentication method decorator. 135 keywords: 136 - name: user name. 137 """ 138 name = kwargs.get('name') 139 def df(fn): 140 opt = __options(fn) 141 required = Options() 142 required.user = name 143 auth = ('pam', required) 144 opt.security.append(auth) 145 return fn
146 if args: 147 return df(args[0]) 148 else: 149 return df 150