<s> from googkit . commands . build import BuildCommand <EOL> from googkit . commands . candidates import CandidatesCommand <EOL> from googkit . commands . init import InitCommand <EOL> from googkit . commands . lint import LintCommand <EOL> from googkit . commands . ready import ReadyCommand <EOL> from googkit . commands . setup import SetupCommand <EOL> from googkit . commands . update_deps import UpdateDepsCommand <EOL> class CommandTree ( object ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> DEFAULT_TREE = { <EOL> '<STR_LIT>' : CandidatesCommand , <EOL> '<STR_LIT>' : BuildCommand , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ReadyCommand <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : UpdateDepsCommand <EOL> } , <EOL> '<STR_LIT>' : InitCommand , <EOL> '<STR_LIT>' : LintCommand , <EOL> '<STR_LIT>' : ReadyCommand , <EOL> '<STR_LIT>' : SetupCommand , <EOL> } <EOL> def __init__ ( self , tree = None ) : <EOL> """<STR_LIT>""" <EOL> if tree is None : <EOL> self . _tree = CommandTree . DEFAULT_TREE . copy ( ) <EOL> else : <EOL> self . _tree = tree <EOL> def right_commands ( self , args ) : <EOL> """<STR_LIT>""" <EOL> command_dict = self . _tree <EOL> result = [ ] <EOL> for arg in args : <EOL> next_dict = command_dict . get ( arg ) <EOL> if next_dict is None : <EOL> break <EOL> result . append ( arg ) <EOL> if not isinstance ( next_dict , dict ) : <EOL> break <EOL> command_dict = next_dict <EOL> return result <EOL> @ classmethod <EOL> def is_internal_command ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> return name [ <NUM_LIT:0> ] == '<STR_LIT:_>' <EOL> def available_commands ( self , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> command_dict = self . _tree <EOL> for arg in args : <EOL> next_dict = command_dict . get ( arg ) <EOL> if next_dict is None : <EOL> return [ ] <EOL> command_dict = next_dict <EOL> if not isinstance ( command_dict , dict ) : <EOL> return [ ] <EOL> commands = command_dict . keys ( ) <EOL> return sorted ( [ cmd for cmd in commands if not CommandTree . is_internal_command ( cmd ) ] ) <EOL> def command_class ( self , args ) : <EOL> """<STR_LIT>""" <EOL> value = self . _tree <EOL> if len ( args ) > <NUM_LIT:1> and args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return self . _tree [ '<STR_LIT>' ] <EOL> depth = <NUM_LIT:0> <EOL> for arg in args : <EOL> next_value = value . get ( arg ) <EOL> depth += <NUM_LIT:1> <EOL> if not isinstance ( next_value , dict ) : <EOL> if depth != len ( args ) : <EOL> return None <EOL> return next_value <EOL> value = next_value <EOL> return None <EOL> def register ( self , names , commands ) : <EOL> """<STR_LIT>""" <EOL> command_dict = self . _tree <EOL> for name in names [ : - <NUM_LIT:1> ] : <EOL> command_dict [ name ] = { } <EOL> command_dict = command_dict [ name ] <EOL> command_dict [ names [ - <NUM_LIT:1> ] ] = commands </s>
