Changeset 579

Show
Ignore:
Timestamp:
07/21/08 00:06:37 (3 months ago)
Author:
athomas
Message:

Automatically cull candidates by default. This can be disabled with cull_candidates=False.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cly/trunk/cly/builder.py

    r577 r579  
    1919from xml.dom import minidom 
    2020from inspect import isclass, getargspec 
    21 from cly.extra import cull_candidates 
    2221from cly.exceptions import * 
    2322from cly.parser import Context 
     
    3837__docformat__ = 'restructuredtext en' 
    3938 
     39 
    4040class Node(object): 
    4141    """The base class for all grammar nodes. 
     
    104104            (eg.  files in the current directory). 
    105105 
     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 
    106117        :traversals: 
    107118            The number of times this node can match in any parse context. 
     
    118129    order = 0 
    119130    match_candidates = False 
     131    cull_candidates = True 
    120132    traversals = 1 
    121133    label = None 
     
    136148            self.separator = kwargs.pop('separator') 
    137149        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 
    139155            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 
    140162        if self.pattern is not None: 
    141163            self._pattern = re.compile(self.pattern) 
     
    420442        """ 
    421443        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 
    424446 
    425447    def find(self, path): 
     
    16811703        def parse(self, context, match): 
    16821704            return self.STATIC_TIMEZONES[match.group()] 
     1705 
     1706 
     1707def 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  
    8989        parser = Parser(grammar, data={'echo': self._echo}) 
    9090        context = parser.parse('echo ') 
    91         self.assertEqual(list(context.candidates()), ['monkey', 'muppet']) 
     91        self.assertEqual(list(context.candidates()), ['monkey ', 'muppet ']) 
    9292 
    9393    def test_node_extension(self): 
     
    215215    import cly.interactive 
    216216    import cly.console 
    217     import cly.extra 
    218217    import cly.parser 
    219218    import cly.builder 
     
    225224    suite.addTest(doctest.DocTestSuite(cly.interactive)) 
    226225    suite.addTest(doctest.DocTestSuite(cly.console)) 
    227     suite.addTest(doctest.DocTestSuite(cly.extra)) 
    228226    suite.addTest(doctest.DocTestSuite(cly.parser)) 
    229227    suite.addTest(doctest.DocTestSuite(cly.builder))