- Timestamp:
- 06/02/05 21:03:02 (3 years ago)
- Files:
-
- manage/branches/python/CLI.py (modified) (9 diffs)
- manage/branches/python/__init__.py (deleted)
- manage/branches/python/README (added)
- manage/branches/python/setup.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
manage/branches/python/CLI.py
r204 r205 45 45 # Merge the given target at this location. Target can either be a sub-grammar 46 46 # 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". 48 49 MERGE = 'MERGE' 49 # A hook allows external grammars to be merged50 HOOK = 'HOOK'51 50 52 51 # Context states … … 297 296 for m in merge: 298 297 mtype = type(m) 299 if mtype is types.FunctionType:298 if callable(m): 300 299 m = m(self, grammar) 301 300 mtype = type(m) … … 339 338 elif htype is str: 340 339 return { key : help } 341 elif htype is types.FunctionType:340 elif callable(help): 342 341 return self.parse_help_node(key, {'HELP' : help(self) }) 343 342 else: … … 380 379 help = self.parse_help_node(k, v) 381 380 if not help: 382 if type(k) is not types.FunctionType:381 if callable(k): 383 382 out[group].append([ k, "No HELP for this grammar node." ]) 384 383 else: … … 403 402 404 403 def executable(self): 405 return type(self.result) is types.FunctionType404 return callable(self.result) 406 405 407 406 def __call__(self, grammar): … … 410 409 def execute(self, grammar): 411 410 if ACTION in grammar: 412 if type(grammar[ACTION]) is types.FunctionType:411 if callable(grammar[ACTION]): 413 412 return grammar[ACTION](self, **self.__var) 414 413 elif type(grammar[ACTION]) is dict: 415 414 if ACTION in grammar[ACTION]: 416 if type(grammar[ACTION][ACTION]) is types.FunctionType:415 if callable(grammar[ACTION][ACTION]): 417 416 return grammar[ACTION][ACTION](self, **self.__var) 418 417 else: … … 440 439 if not tokens: 441 440 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]): 443 442 return Result(Result.OK, self, grammar) 444 443 else: … … 451 450 for match in self.sort_grammar_keys(self.filter_grammar(grammar), grammar): 452 451 # Otherwise it's a command node 453 if type(match) is types.FunctionType:452 if callable(match): 454 453 if match(self, token.group()): 455 454 nodes.append(ParseNode(self, grammar[match], match, token)) … … 474 473 self.__grammar = grammar 475 474 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 out486 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 475 493 476 def parse(self, command):
