Changeset 579
- Timestamp:
- 07/21/08 00:06:37 (3 months ago)
- Files:
-
- cly/trunk/cly/builder.py (modified) (7 diffs)
- cly/trunk/cly/extra.py (deleted)
- cly/trunk/cly/test.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/builder.py
r577 r579 19 19 from xml.dom import minidom 20 20 from inspect import isclass, getargspec 21 from cly.extra import cull_candidates22 21 from cly.exceptions import * 23 22 from cly.parser import Context … … 38 37 __docformat__ = 'restructuredtext en' 39 38 39 40 40 class Node(object): 41 41 """The base class for all grammar nodes. … … 104 104 (eg. files in the current directory). 105 105 106 :cull_candidates: 107 If ``True`` (the default) :meth:`candidates` may return a static 108 list of candidates that is automatically culled based on the text 109 being matched. This avoids a lot of boiler plate code. 110 111 >>> a = Node(candidates=['one', 'two']) 112 >>> print list(a.candidates(None, '')) 113 ['one ', 'two '] 114 >>> print list(a.candidates(None, 'o')) 115 ['one '] 116 106 117 :traversals: 107 118 The number of times this node can match in any parse context. … … 118 129 order = 0 119 130 match_candidates = False 131 cull_candidates = True 120 132 traversals = 1 121 133 label = None … … 136 148 self.separator = kwargs.pop('separator') 137 149 if 'candidates' in kwargs: 138 self.candidates = kwargs.pop('candidates') 150 candidates = kwargs.pop('candidates') 151 if callable(candidates): 152 self.candidates = candidates 153 else: 154 self.candidates = lambda c, t: candidates 139 155 self.match_candidates = True 156 self.cull_candidates = kwargs.pop('cull_candidates', self.cull_candidates) 157 if self.cull_candidates: 158 def cull(context, text): 159 return cull_candidates(cull.candidates(context, text), text) 160 cull.candidates = self.candidates 161 self.candidates = cull 140 162 if self.pattern is not None: 141 163 self._pattern = re.compile(self.pattern) … … 420 442 """ 421 443 for key, help in self.help(context): 422 if key[0] != '<' and key.startswith(text):423 yield key + ' '444 if key[0] != '<': 445 yield key 424 446 425 447 def find(self, path): … … 1681 1703 def parse(self, context, match): 1682 1704 return self.STATIC_TIMEZONES[match.group()] 1705 1706 1707 def cull_candidates(candidates, text, sep=' '): 1708 """Cull candidates that do not start with ``text``. 1709 1710 Returned candidates also have a space appended. 1711 1712 Arguments: 1713 :candidates: Sequence of match candidates. 1714 :text: Text to match. 1715 :sep: Separator to append to match. 1716 1717 >>> cull_candidates(['bob', 'fred', 'barry', 'harry'], 'b') 1718 ['bob ', 'barry '] 1719 """ 1720 return [c + sep for c in candidates if c and c.startswith(text)] 1721 1722 cly/trunk/cly/test.py
r576 r579 89 89 parser = Parser(grammar, data={'echo': self._echo}) 90 90 context = parser.parse('echo ') 91 self.assertEqual(list(context.candidates()), ['monkey ', 'muppet'])91 self.assertEqual(list(context.candidates()), ['monkey ', 'muppet ']) 92 92 93 93 def test_node_extension(self): … … 215 215 import cly.interactive 216 216 import cly.console 217 import cly.extra218 217 import cly.parser 219 218 import cly.builder … … 225 224 suite.addTest(doctest.DocTestSuite(cly.interactive)) 226 225 suite.addTest(doctest.DocTestSuite(cly.console)) 227 suite.addTest(doctest.DocTestSuite(cly.extra))228 226 suite.addTest(doctest.DocTestSuite(cly.parser)) 229 227 suite.addTest(doctest.DocTestSuite(cly.builder))
