Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(14)

Side by Side Diff: MoinMoin/util/registry.py

Issue 6453060: Registry improvements (Closed)
Patch Set: Created 12 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« MoinMoin/util/decorator.py ('K') | « MoinMoin/util/decorator.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright: 2008-2010 MoinMoin:BastianBlank 1 # Copyright: 2008-2010 MoinMoin:BastianBlank
2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details. 2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
3 3
4 """ 4 """
5 MoinMoin - Module registry 5 MoinMoin - Module registry
6 6
7 Every module registers a factory for itself at the registry with a given 7 Every module registers a factory for itself at the registry with a given
8 priority. During the lookup each factory is called with the given arguments and 8 priority. During the lookup each factory is called with the given arguments and
9 can return a callable to consider itself as a match. 9 can return a callable to consider itself as a match.
10 """ 10 """
11 11
12 12
13 from collections import namedtuple
14
15
13 class RegistryBase(object): 16 class RegistryBase(object):
14 PRIORITY_REALLY_FIRST = -20 17 PRIORITY_REALLY_FIRST = -20
15 PRIORITY_FIRST = -10 18 PRIORITY_FIRST = -10
16 PRIORITY_MIDDLE = 0 19 PRIORITY_MIDDLE = 0
17 PRIORITY_LAST = 10 20 PRIORITY_LAST = 10
18 PRIORITY_REALLY_LAST = 20 21 PRIORITY_REALLY_LAST = 20
19 22
20 class Entry(object): 23 class Entry(namedtuple('Entry', 'factory priority')):
21 def __init__(self, factory, priority): 24 def __call__(self, *args, **kw):
22 self.factory, self.priority = factory, priority 25 return self.factory(*args, **kw)
23
24 def __eq__(self, other):
25 if isinstance(other, self.__class__):
26 return (self.factory == other.factory and
27 self.priority == other.priority)
28 return NotImplemented
29 26
30 def __lt__(self, other): 27 def __lt__(self, other):
31 if isinstance(other, self.__class__): 28 if isinstance(other, self.__class__):
32 return self.priority < other.priority 29 return self.priority < other.priority
33 return NotImplemented 30 return NotImplemented
34 31
35 def __repr__(self):
36 return '<{0}: prio {1} [{2!r}]>'.format(self.__class__.__name__,
37 self.priority,
38 self.factory)
39
40 def __init__(self): 32 def __init__(self):
41 self._entries = [] 33 self._entries = []
42 34
43 def __repr__(self): 35 def __repr__(self):
44 return '<{0}: {1!r}>'.format(self.__class__.__name__, self._entries) 36 return '<{0}: {1!r}>'.format(self.__class__.__name__, self._entries)
45 37
46 def get(self, *args, **kw): 38 def get(self, *args, **kw):
47 """ 39 """
48 Lookup a matching module 40 Lookup a matching module
49 41
50 Each registered factory is called with the given arguments and 42 Each registered factory is called with the given arguments and
51 the first matching wins. 43 the first matching wins.
52 """ 44 """
53 for entry in self._entries: 45 for entry in self._entries:
54 conv = entry.factory(*args, **kw) 46 conv = entry(*args, **kw)
xiaq 2012/07/29 04:30:08 This modification and addition of Entry.__call__ c
55 if conv is not None: 47 if conv is not None:
56 return conv 48 return conv
57 49
58 def _register(self, entry): 50 def _register(self, entry):
59 if entry not in self._entries: 51 if entry not in self._entries:
60 entries = self._entries[:] 52 entries = self._entries[:]
61 for i in xrange(len(entries)): 53 for i in xrange(len(entries)):
62 if entry < entries[i]: 54 if entry < entries[i]:
63 entries.insert(i, entry) 55 entries.insert(i, entry)
64 break 56 break
(...skipping 16 matching lines...) Expand all
81 73
82 74
83 class Registry(RegistryBase): 75 class Registry(RegistryBase):
84 def register(self, factory, priority=RegistryBase.PRIORITY_MIDDLE): 76 def register(self, factory, priority=RegistryBase.PRIORITY_MIDDLE):
85 """ 77 """
86 Register a factory 78 Register a factory
87 79
88 :param factory: Factory to register. Callable, have to return a class 80 :param factory: Factory to register. Callable, have to return a class
89 """ 81 """
90 return self._register(self.Entry(factory, priority)) 82 return self._register(self.Entry(factory, priority))
OLDNEW
« MoinMoin/util/decorator.py ('K') | « MoinMoin/util/decorator.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b