annotate mercurial/registrar.py @ 28392:b983a2f04987

registrar: introduce new class for registration to replace funcregistrar _funcregistrarbase differs from funcregistrar in points below: - every code paths should use same class derived from _funcregistrarbase to register functions in a same category funcregistrar expects (3rd party) extensions to use (a class derived from) delayregistrar. - actual extra setup should be executed in another function For example, marking revset predicate as "safe" is executed in a class derived from _funcregistrarbase, but putting name of "safe" predicate into safesymbols is executed in another function for it. funcregistrar expects derived classes to do so. New class is named as module private one, because code paths, which register functions, should use not it directly but one derived from it.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Tue, 08 Mar 2016 23:04:53 +0900
parents 60bf90eb8bf8
children ac11ba7c2e56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27583
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
1 # registrar.py - utilities to register function for specific purpose
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
2 #
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
3 # Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
4 #
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
5 # This software may be used and distributed according to the terms of the
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
6 # GNU General Public License version 2 or any later version.
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
7
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
8 from __future__ import absolute_import
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
9
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
10 from . import (
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
11 util,
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
12 )
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
13
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
14 class funcregistrar(object):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
15 """Base of decorator to register a fuction for specific purpose
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
16
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
17 The least derived class can be defined by overriding 'table' and
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
18 'formatdoc', for example::
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
19
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
20 symbols = {}
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
21 class keyword(funcregistrar):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
22 table = symbols
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
23 formatdoc = ":%s: %s"
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
24
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
25 @keyword('bar')
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
26 def barfunc(*args, **kwargs):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
27 '''Explanation of bar keyword ....
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
28 '''
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
29 pass
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
30
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
31 In this case:
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
32
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
33 - 'barfunc' is registered as 'bar' in 'symbols'
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
34 - online help uses ":bar: Explanation of bar keyword"
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
35 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
36
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
37 def __init__(self, decl):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
38 """'decl' is a name or more descriptive string of a function
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
39
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
40 Specification of 'decl' depends on registration purpose.
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
41 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
42 self.decl = decl
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
43
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
44 table = None
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
45
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
46 def __call__(self, func):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
47 """Execute actual registration for specified function
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
48 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
49 name = self.getname()
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
50
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
51 if func.__doc__ and not util.safehasattr(func, '_origdoc'):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
52 doc = func.__doc__.strip()
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
53 func._origdoc = doc
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
54 if callable(self.formatdoc):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
55 func.__doc__ = self.formatdoc(doc)
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
56 else:
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
57 # convenient shortcut for simple format
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
58 func.__doc__ = self.formatdoc % (self.decl, doc)
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
59
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
60 self.table[name] = func
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
61 self.extraaction(name, func)
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
62
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
63 return func
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
64
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
65 def getname(self):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
66 """Return the name of the registered function from self.decl
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
67
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
68 Derived class should override this, if it allows more
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
69 descriptive 'decl' string than just a name.
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
70 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
71 return self.decl
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
72
27584
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
73 def parsefuncdecl(self):
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
74 """Parse function declaration and return the name of function in it
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
75 """
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
76 i = self.decl.find('(')
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
77 if i > 0:
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
78 return self.decl[:i]
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
79 else:
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
80 return self.decl
fc7c8cac6a4b revset: use decorator to register a function as revset predicate
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27583
diff changeset
81
27583
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
82 def formatdoc(self, doc):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
83 """Return formatted document of the registered function for help
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
84
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
85 'doc' is '__doc__.strip()' of the registered function.
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
86
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
87 If this is overridden by non-callable object in derived class,
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
88 such value is treated as "format string" and used to format
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
89 document by 'self.formatdoc % (self.decl, doc)' for convenience.
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
90 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
91 raise NotImplementedError()
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
92
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
93 def extraaction(self, name, func):
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
94 """Execute exra action for registered function, if needed
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
95 """
37d50250b696 registrar: add funcregistrar class to register function for specific purpose
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
96 pass
27585
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
97
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
98 class delayregistrar(object):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
99 """Decorator to delay actual registration until uisetup or so
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
100
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
101 For example, the decorator class to delay registration by
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
102 'keyword' funcregistrar can be defined as below::
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
103
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
104 class extkeyword(delayregistrar):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
105 registrar = keyword
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
106 """
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
107 def __init__(self):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
108 self._list = []
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
109
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
110 registrar = None
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
111
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
112 def __call__(self, *args, **kwargs):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
113 """Return the decorator to delay actual registration until setup
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
114 """
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
115 assert self.registrar is not None
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
116 def decorator(func):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
117 # invocation of self.registrar() here can detect argument
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
118 # mismatching immediately
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
119 self._list.append((func, self.registrar(*args, **kwargs)))
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
120 return func
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
121 return decorator
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
122
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
123 def setup(self):
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
124 """Execute actual registration
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
125 """
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
126 while self._list:
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
127 func, decorator = self._list.pop(0)
60bf90eb8bf8 registrar: add delayregistrar class to register function in extensions
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27584
diff changeset
128 decorator(func)
28392
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
129
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
130 class _funcregistrarbase(object):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
131 """Base of decorator to register a fuction for specific purpose
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
132
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
133 This decorator stores decorated functions into own dict 'table'.
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
134
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
135 The least derived class can be defined by overriding 'formatdoc',
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
136 for example::
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
137
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
138 class keyword(_funcregistrarbase):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
139 _docformat = ":%s: %s"
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
140
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
141 This should be used as below:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
142
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
143 keyword = registrar.keyword()
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
144
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
145 @keyword('bar')
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
146 def barfunc(*args, **kwargs):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
147 '''Explanation of bar keyword ....
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
148 '''
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
149 pass
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
150
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
151 In this case:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
152
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
153 - 'barfunc' is stored as 'bar' in '_table' of an instance 'keyword' above
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
154 - 'barfunc.__doc__' becomes ":bar: Explanation of bar keyword"
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
155 """
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
156 def __init__(self, table=None):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
157 if table is None:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
158 self._table = {}
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
159 else:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
160 self._table = table
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
161
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
162 def __call__(self, decl, *args, **kwargs):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
163 return lambda func: self._doregister(func, decl, *args, **kwargs)
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
164
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
165 def _doregister(self, func, decl, *args, **kwargs):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
166 name = self._getname(decl)
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
167
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
168 if func.__doc__ and not util.safehasattr(func, '_origdoc'):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
169 doc = func.__doc__.strip()
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
170 func._origdoc = doc
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
171 func.__doc__ = self._formatdoc(decl, doc)
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
172
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
173 self._table[name] = func
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
174 self._extrasetup(name, func, *args, **kwargs)
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
175
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
176 return func
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
177
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
178 def _parsefuncdecl(self, decl):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
179 """Parse function declaration and return the name of function in it
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
180 """
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
181 i = decl.find('(')
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
182 if i >= 0:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
183 return decl[:i]
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
184 else:
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
185 return decl
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
186
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
187 def _getname(self, decl):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
188 """Return the name of the registered function from decl
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
189
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
190 Derived class should override this, if it allows more
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
191 descriptive 'decl' string than just a name.
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
192 """
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
193 return decl
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
194
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
195 _docformat = None
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
196
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
197 def _formatdoc(self, decl, doc):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
198 """Return formatted document of the registered function for help
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
199
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
200 'doc' is '__doc__.strip()' of the registered function.
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
201 """
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
202 return self._docformat % (decl, doc)
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
203
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
204 def _extrasetup(self, name, func):
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
205 """Execute exra setup for registered function, if needed
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
206 """
b983a2f04987 registrar: introduce new class for registration to replace funcregistrar
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27585
diff changeset
207 pass