Changeset 205 for manage

Show
Ignore:
Timestamp:
06/02/05 21:03:02 (3 years ago)
Author:
athomas
Message:
  • Removed hooks as this can be done with MERGE.
  • Using callable() instead of type() as it catches all callable objects.
Files:

Legend:

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

    r204 r205  
    4545# Merge the given target at this location. Target can either be a sub-grammar 
    4646# or a callable with the signature function(context, grammar), where grammar is 
    47 # the grammar at the current depth. 
     47# the grammar at the current depth. MERGE's occur on the fly and so can be used 
     48# to implement grammar "hooks". 
    4849MERGE = 'MERGE' 
    49 # A hook allows external grammars to be merged 
    50 HOOK = 'HOOK' 
    5150 
    5251# Context states 
     
    297296                                        for m in merge: 
    298297                                                mtype = type(m) 
    299                                                 if mtype is types.FunctionType
     298                                                if callable(m)
    300299                                                        m = m(self, grammar) 
    301300                                                        mtype = type(m) 
     
    339338                                elif htype is str: 
    340339                                        return { key : help } 
    341                                 elif htype is types.FunctionType
     340                                elif callable(help)
    342341                                        return self.parse_help_node(key, {'HELP' : help(self) }) 
    343342                                else: 
     
    380379                                        help = self.parse_help_node(k, v) 
    381380                                if not help: 
    382                                         if type(k) is not types.FunctionType
     381                                        if callable(k)
    383382                                                out[group].append([ k, "No HELP for this grammar node." ]) 
    384383                                else: 
     
    403402 
    404403                def executable(self): 
    405                         return type(self.result) is types.FunctionType 
     404                        return callable(self.result) 
    406405 
    407406                def __call__(self, grammar): 
     
    410409                def execute(self, grammar): 
    411410                        if ACTION in grammar: 
    412                                 if type(grammar[ACTION]) is types.FunctionType
     411                                if callable(grammar[ACTION])
    413412                                        return grammar[ACTION](self, **self.__var) 
    414413                                elif type(grammar[ACTION]) is dict: 
    415414                                        if ACTION in grammar[ACTION]: 
    416                                                 if type(grammar[ACTION][ACTION]) is types.FunctionType
     415                                                if callable(grammar[ACTION][ACTION])
    417416                                                        return grammar[ACTION][ACTION](self, **self.__var) 
    418417                                                else: 
     
    440439                        if not tokens: 
    441440                                if ACTION in self.filter_grammar(grammar): 
    442                                         if type(grammar[ACTION]) is dict or type(grammar[ACTION]) is types.FunctionType
     441                                        if type(grammar[ACTION]) is dict or callable(grammar[ACTION])
    443442                                                return Result(Result.OK, self, grammar) 
    444443                                        else: 
     
    451450                        for match in self.sort_grammar_keys(self.filter_grammar(grammar), grammar): 
    452451                                # Otherwise it's a command node 
    453                                 if type(match) is types.FunctionType
     452                                if callable(match)
    454453                                        if match(self, token.group()): 
    455454                                                nodes.append(ParseNode(self, grammar[match], match, token)) 
     
    474473                self.__grammar = grammar 
    475474                self.__tokenise = re.compile(r'(\'(?:[^\\\']|.)*\'|"(?:[^\\"]|.)*")|(\S+)') 
    476  
    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) 
    492475 
    493476        def parse(self, command):