##// END OF EJS Templates
registrar: introduce new class for registration to replace funcregistrar...
FUJIWARA Katsunori -
r28392:b983a2f0 default
parent child Browse files
Show More
@@ -126,3 +126,82 b' class delayregistrar(object):'
126 while self._list:
126 while self._list:
127 func, decorator = self._list.pop(0)
127 func, decorator = self._list.pop(0)
128 decorator(func)
128 decorator(func)
129
130 class _funcregistrarbase(object):
131 """Base of decorator to register a fuction for specific purpose
132
133 This decorator stores decorated functions into own dict 'table'.
134
135 The least derived class can be defined by overriding 'formatdoc',
136 for example::
137
138 class keyword(_funcregistrarbase):
139 _docformat = ":%s: %s"
140
141 This should be used as below:
142
143 keyword = registrar.keyword()
144
145 @keyword('bar')
146 def barfunc(*args, **kwargs):
147 '''Explanation of bar keyword ....
148 '''
149 pass
150
151 In this case:
152
153 - 'barfunc' is stored as 'bar' in '_table' of an instance 'keyword' above
154 - 'barfunc.__doc__' becomes ":bar: Explanation of bar keyword"
155 """
156 def __init__(self, table=None):
157 if table is None:
158 self._table = {}
159 else:
160 self._table = table
161
162 def __call__(self, decl, *args, **kwargs):
163 return lambda func: self._doregister(func, decl, *args, **kwargs)
164
165 def _doregister(self, func, decl, *args, **kwargs):
166 name = self._getname(decl)
167
168 if func.__doc__ and not util.safehasattr(func, '_origdoc'):
169 doc = func.__doc__.strip()
170 func._origdoc = doc
171 func.__doc__ = self._formatdoc(decl, doc)
172
173 self._table[name] = func
174 self._extrasetup(name, func, *args, **kwargs)
175
176 return func
177
178 def _parsefuncdecl(self, decl):
179 """Parse function declaration and return the name of function in it
180 """
181 i = decl.find('(')
182 if i >= 0:
183 return decl[:i]
184 else:
185 return decl
186
187 def _getname(self, decl):
188 """Return the name of the registered function from decl
189
190 Derived class should override this, if it allows more
191 descriptive 'decl' string than just a name.
192 """
193 return decl
194
195 _docformat = None
196
197 def _formatdoc(self, decl, doc):
198 """Return formatted document of the registered function for help
199
200 'doc' is '__doc__.strip()' of the registered function.
201 """
202 return self._docformat % (decl, doc)
203
204 def _extrasetup(self, name, func):
205 """Execute exra setup for registered function, if needed
206 """
207 pass
General Comments 0
You need to be logged in to leave comments. Login now