Changeset 204

Show
Ignore:
Timestamp:
05/18/05 20:04:06 (4 years ago)
Author:
athomas
Message:

Added help header/footer and started adding hooks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • manage/branches/python/CLI.py

    r203 r204  
    1111HELP = 'HELP' 
    1212HELP_HEADER = 'HELP_HEADER' 
     13HELP_FOOTER = 'HELP_FOOTER' 
    1314ERROR = 'ERROR' 
    1415FLAGS = 'FLAGS' 
     
    4647# the grammar at the current depth. 
    4748MERGE = 'MERGE' 
     49# A hook allows external grammars to be merged 
     50HOOK = 'HOOK' 
    4851 
    4952# Context states 
     
    258261                                        out[key] = branch 
    259262                        return out 
    260                                          
    261263 
    262264                def sort_grammar_keys(self, keys, grammar): 
     
    283285                                return cmp(l, r) 
    284286 
    285                         k = keys.keys() 
    286                         k.sort(cmp = key_sort) 
    287                         return k 
     287                        return sorted(keys.keys(), key_sort) 
    288288 
    289289                def grammar_branches(self, grammar): 
     
    346346 
    347347                def extract_help(self, grammar): 
     348                        """ Extract help from the given grammar node. Returns a dictionary 
     349                        with the keys of the dictionary being either a tuple of 
     350                        (order, group), or HELP_HEADER or HELP_FOOTER. If the key is a 
     351                        tuple, the value is an array of (command, help) pairs. If the key 
     352                        is HELP_HEADER or HELP_FOOTER then the value is the actual help  
     353                        text. """ 
    348354                        out = {(9999, None) : []} 
    349355                        grammar = self.copy_grammar_shallow(grammar) 
     356                        if HELP_HEADER in grammar: out[HELP_HEADER] = grammar[HELP_HEADER] 
     357                        if HELP_FOOTER in grammar: out[HELP_FOOTER] = grammar[HELP_FOOTER] 
    350358                        for k in self.sort_grammar_keys(self.filter_grammar(grammar), grammar): 
    351359                                group = (9999, None) 
     
    380388                        # Do a real sort? 
    381389                        for k in out: 
    382                                 out[k].sort() 
     390                                if type(out[k]) is list: 
     391                                        out[k].sort() 
    383392                        return out 
    384393 
    385394                def candidates(self, grammar): 
     395                        """ The flattened list of command candidates for grammar. Excludes 
     396                        help headers and footers. """ 
    386397                        out = [] 
    387398                        for group in self.extract_help(grammar).values(): 
    388                                 for help in group: 
    389                                         out.append(help[0]) 
     399                                if type(group) is list: 
     400                                        for help in group: 
     401                                                out.append(help[0]) 
    390402                        return out 
    391403 
     
    463475                self.__tokenise = re.compile(r'(\'(?:[^\\\']|.)*\'|"(?:[^\\"]|.)*")|(\S+)') 
    464476 
     477        def __walk(self, source_grammar, callback): 
     478                out = [] 
     479                grammar = self.grammar_branches(self.copy_grammar_shallow(source_grammar)) 
     480                for k, v in grammar.iteritems(): 
     481                        if callback(k, v): 
     482                                out.append((k, v)) 
     483                        if type(v) is dict: 
     484                                out.extend(self.__walk(v, callback)) 
     485                return out 
     486 
     487        def walk(self, callback): 
     488                self.__walk(self.__grammar, callback) 
     489 
     490        def hook(self, hook, grammar): 
     491                print self.walk(lambda k, v: k == HOOK and v == hook) 
     492 
    465493        def parse(self, command): 
    466494                tokens = [] 
  • manage/branches/python/__init__.py

    r199 r204  
    22'GROUP_HEADING', 'HELP', 'IF', 'IF_LABEL', 'IF_VAR', 'JUMP', 'JUMP_TO', 
    33'LABEL', 'MERGE', 'ORDER', 'ParseNode', 'RANGE', 'RETURN', 'Result', 'UNLESS', 
    4 'UNLESS_LABEL', 'UNLESS_VAR', 'VAR'
     4'UNLESS_LABEL', 'UNLESS_VAR', 'VAR', 'HOOK'