Mercurial > hg
comparison mercurial/registrar.py @ 28446:08bd09921102
registrar: remove useless base classes (API)
Previous patches make these classes useless by removing classes
derived from them.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Fri, 11 Mar 2016 04:14:54 +0900 |
parents | ac11ba7c2e56 |
children | 4eb5496c2bd4 |
comparison
equal
deleted
inserted
replaced
28445:d749b1832d2f | 28446:08bd09921102 |
---|---|
8 from __future__ import absolute_import | 8 from __future__ import absolute_import |
9 | 9 |
10 from . import ( | 10 from . import ( |
11 util, | 11 util, |
12 ) | 12 ) |
13 | |
14 class funcregistrar(object): | |
15 """Base of decorator to register a fuction for specific purpose | |
16 | |
17 The least derived class can be defined by overriding 'table' and | |
18 'formatdoc', for example:: | |
19 | |
20 symbols = {} | |
21 class keyword(funcregistrar): | |
22 table = symbols | |
23 formatdoc = ":%s: %s" | |
24 | |
25 @keyword('bar') | |
26 def barfunc(*args, **kwargs): | |
27 '''Explanation of bar keyword .... | |
28 ''' | |
29 pass | |
30 | |
31 In this case: | |
32 | |
33 - 'barfunc' is registered as 'bar' in 'symbols' | |
34 - online help uses ":bar: Explanation of bar keyword" | |
35 """ | |
36 | |
37 def __init__(self, decl): | |
38 """'decl' is a name or more descriptive string of a function | |
39 | |
40 Specification of 'decl' depends on registration purpose. | |
41 """ | |
42 self.decl = decl | |
43 | |
44 table = None | |
45 | |
46 def __call__(self, func): | |
47 """Execute actual registration for specified function | |
48 """ | |
49 name = self.getname() | |
50 | |
51 if func.__doc__ and not util.safehasattr(func, '_origdoc'): | |
52 doc = func.__doc__.strip() | |
53 func._origdoc = doc | |
54 if callable(self.formatdoc): | |
55 func.__doc__ = self.formatdoc(doc) | |
56 else: | |
57 # convenient shortcut for simple format | |
58 func.__doc__ = self.formatdoc % (self.decl, doc) | |
59 | |
60 self.table[name] = func | |
61 self.extraaction(name, func) | |
62 | |
63 return func | |
64 | |
65 def getname(self): | |
66 """Return the name of the registered function from self.decl | |
67 | |
68 Derived class should override this, if it allows more | |
69 descriptive 'decl' string than just a name. | |
70 """ | |
71 return self.decl | |
72 | |
73 def parsefuncdecl(self): | |
74 """Parse function declaration and return the name of function in it | |
75 """ | |
76 i = self.decl.find('(') | |
77 if i > 0: | |
78 return self.decl[:i] | |
79 else: | |
80 return self.decl | |
81 | |
82 def formatdoc(self, doc): | |
83 """Return formatted document of the registered function for help | |
84 | |
85 'doc' is '__doc__.strip()' of the registered function. | |
86 | |
87 If this is overridden by non-callable object in derived class, | |
88 such value is treated as "format string" and used to format | |
89 document by 'self.formatdoc % (self.decl, doc)' for convenience. | |
90 """ | |
91 raise NotImplementedError() | |
92 | |
93 def extraaction(self, name, func): | |
94 """Execute exra action for registered function, if needed | |
95 """ | |
96 pass | |
97 | |
98 class delayregistrar(object): | |
99 """Decorator to delay actual registration until uisetup or so | |
100 | |
101 For example, the decorator class to delay registration by | |
102 'keyword' funcregistrar can be defined as below:: | |
103 | |
104 class extkeyword(delayregistrar): | |
105 registrar = keyword | |
106 """ | |
107 def __init__(self): | |
108 self._list = [] | |
109 | |
110 registrar = None | |
111 | |
112 def __call__(self, *args, **kwargs): | |
113 """Return the decorator to delay actual registration until setup | |
114 """ | |
115 assert self.registrar is not None | |
116 def decorator(func): | |
117 # invocation of self.registrar() here can detect argument | |
118 # mismatching immediately | |
119 self._list.append((func, self.registrar(*args, **kwargs))) | |
120 return func | |
121 return decorator | |
122 | |
123 def setup(self): | |
124 """Execute actual registration | |
125 """ | |
126 while self._list: | |
127 func, decorator = self._list.pop(0) | |
128 decorator(func) | |
129 | 13 |
130 class _funcregistrarbase(object): | 14 class _funcregistrarbase(object): |
131 """Base of decorator to register a fuction for specific purpose | 15 """Base of decorator to register a fuction for specific purpose |
132 | 16 |
133 This decorator stores decorated functions into own dict 'table'. | 17 This decorator stores decorated functions into own dict 'table'. |