def test_subbranch(self): tree3 = CommandBranch("Again") tree2 = CommandBranch("World") tree2.add_child(tree3) tree1 = CommandBranch("Hello") tree1.add_child(tree2) tree = CommandHead() tree.add_child(tree1) self.assertEqual(tree.get_subbranch("Hello"), ["World", "Again"])
def add_exit(self): """ adds the exits from the application """ self.completable.append("quit") self.completable.append("exit") self.descrip["quit"] = "Exits the program" self.descrip["exit"] = "Exits the program" self.command_tree.children.append(CommandBranch("quit")) self.command_tree.children.append(CommandBranch("exit")) self.command_param["quit"] = "" self.command_param["exit"] = ""
def _gather_from_files(self, config): """ gathers from the files in a way that is convienent to use """ command_file = config.get_help_files() cache_path = os.path.join(config.config_dir, 'cache') cols = _get_window_columns() with open(os.path.join(cache_path, command_file), 'r') as help_file: data = json.load(help_file) self.add_exit() commands = data.keys() for command in commands: branch = self.command_tree for word in command.split(): if word not in self.completable: self.completable.append(word) if branch.children is None: branch.children = [] if not branch.has_child(word): branch.children.append(CommandBranch(word)) branch = branch.get_child(word, branch.children) description = data[command]['help'] self.descrip[command] = add_new_lines(description, line_min=int(cols) - 2 * TOLERANCE) if 'examples' in data[command]: examples = [] for example in data[command]['examples']: examples.append([ add_new_lines(example[0], line_min=int(cols) - 2 * TOLERANCE), add_new_lines(example[1], line_min=int(cols) - 2 * TOLERANCE)]) self.command_example[command] = examples all_params = [] for param in data[command]['parameters']: suppress = False if data[command]['parameters'][param]['help'] and \ '==SUPPRESS==' in data[command]['parameters'][param]['help']: suppress = True if data[command]['parameters'][param]['help'] and not suppress: param_aliases = set() for par in data[command]['parameters'][param]['name']: param_aliases.add(par) self.param_descript[command + " " + par] = \ add_new_lines( data[command]['parameters'][param]['required'] + " " + data[command]['parameters'][param]['help'], line_min=int(cols) - 2 * TOLERANCE) if par not in self.completable_param: self.completable_param.append(par) all_params.append(par) if len(param_aliases) > 1: param_list = self.same_param_doubles.get(command, []) param_list.append(param_aliases) self.same_param_doubles[command] = param_list self.command_param[command] = all_params
def gather_from_files(self): """ gathers from the files in a way that is convienent to use """ command_file = CONFIGURATION.get_help_files() cache_path = os.path.join(azclishell.configuration.get_config_dir(), 'cache') with open(os.path.join(cache_path, command_file), 'r') as help_file: data = json.load(help_file) self.add_exit() commands = data.keys() for command in commands: branch = self.command_tree for word in command.split(): if word not in self.completable: self.completable.append(word) if branch.children is None: branch.children = [] if not branch.has_child(word): branch.children.append(CommandBranch(word)) branch = branch.get_child(word, branch.children) description = data[command]['help'] self.descrip[command] = add_random_new_lines(description) if 'examples' in data[command]: examples = [] for example in data[command]['examples']: examples.append([ add_random_new_lines(example[0], line_min=int(COLS) - 2 * TOLERANCE), add_random_new_lines(example[1], line_min=int(COLS) - 2 * TOLERANCE)]) self.command_example[command] = examples all_params = [] for param in data[command]['parameters']: suppress = False if data[command]['parameters'][param]['help'] and \ '==SUPPRESS==' in data[command]['parameters'][param]['help']: suppress = True if data[command]['parameters'][param]['help'] and not suppress: param_double = None for par in data[command]['parameters'][param]['name']: if not param_double: param_double = par else: self.same_param_doubles[par] = param_double self.same_param_doubles[param_double] = par self.param_descript[command + " " + par] = \ add_random_new_lines( data[command]['parameters'][param]['required'] + " " + data[command]['parameters'][param]['help']) if par not in self.completable_param: self.completable_param.append(par) all_params.append(par) self.command_param[command] = all_params
def test_in_tree(self): """ tests in tree """ tree4 = CommandBranch("CB1") tree3 = CommandBranch("Again") tree2 = CommandBranch("World") tree2.add_child(tree3) tree2.add_child(tree4) tree1 = CommandBranch("Hello") tree1.add_child(tree2) tree = CommandHead() tree.add_child(tree1) self.assertTrue(in_tree(tree3, 'Again')) self.assertTrue(in_tree(tree2, 'World Again')) self.assertTrue(in_tree(tree1, 'Hello World Again')) self.assertTrue(in_tree(tree1, 'Hello World CB1')) self.assertFalse(in_tree(tree1, 'World Hello CB1')) self.assertFalse(in_tree(tree, 'World Hello CB1')) self.assertFalse(in_tree(tree, 'Hello World Again CB1'))
def test_basic_children(self): tree = CommandTree(data=None) self.assertFalse(tree.has_child("")) with self.assertRaises(ValueError): tree.get_child("nothing", tree.children) tree.add_child(CommandBranch("Kid1")) self.assertTrue(tree.has_child("Kid1")) self.assertFalse(tree.has_child("Kid2")) self.assertFalse( isinstance(tree.get_child("Kid1", tree.children), six.string_types)) self.assertTrue( isinstance(tree.get_child("Kid1", tree.children), CommandTree))
def test_in_tree(self): # tests in tree tree4 = CommandBranch("CB1") tree3 = CommandBranch("Again") tree2 = CommandBranch("World") tree2.add_child(tree3) tree2.add_child(tree4) tree1 = CommandBranch("Hello") tree1.add_child(tree2) tree = CommandHead() tree.add_child(tree1) self.assertTrue(in_tree(tree3, 'Again')) self.assertTrue(in_tree(tree2, 'World Again')) self.assertTrue(in_tree(tree1, 'Hello World Again')) self.assertTrue(in_tree(tree1, 'Hello World CB1')) self.assertFalse(in_tree(tree1, 'World Hello CB1')) self.assertFalse(in_tree(tree, 'World Hello CB1')) self.assertFalse(in_tree(tree, 'Hello World Again CB1'))