def init2(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["-funtime"], } completable_param = [ "-helloworld", "-funtime" ] param_descript = { "create -funtime": "There is no work life balance, it's just your life" } command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False)
def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", '--fun', "--helloworld"], } completable_param = ["--helloworld", "--funtimes", "-f", '--fun'] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life", "create --fun": "There is no work life balance, it's just your life" } same_param_doubles = { "create": [set(["-f", "--funtimes", '--fun']), set(['--unhap', '-u'])] } command_description = {"create": '', "command can": ''} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description) self.completer = AzCompleter(commands, global_params=False, outstream=six.StringIO())
def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) commands = _Commands(command_tree=com_tree3) self.completer = AzCompleter(commands)
def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_description = {"create": '', "command can": ''} commands = _Commands(command_tree=com_tree3, descrip=command_description) self.completer = AzCompleter(commands, global_params=False)
def main(style=None): if APPLICATION.session["az_interactive_active"]: logger.warning("You're in the interactive shell already.\n") return os.environ[ENV_ADDITIONAL_USER_AGENT] = 'AZURECLISHELL/' + __version__ azure_folder = cli_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) config = azclishell.configuration.CONFIGURATION shell_config_dir = azclishell.configuration.get_config_dir try: commands = GatherCommands() az_completer = AzCompleter(commands) except IOError: # if there is no cache az_completer = None if style: given_style = style config.set_style(given_style) else: given_style = config.get_style() style_obj = style_factory(given_style) if config.BOOLEAN_STATES[config.config.get('DEFAULT', 'firsttime')]: config.firsttime() ask_feedback = False if not config.has_feedback() and frequent_user: print( "\n\nAny comments or concerns? You can use the \'feedback\' command!" + " We would greatly appreciate it.\n") ask_feedback = True shell_app = Shell(completer=az_completer, lexer=AzLexer, history=FileHistory( os.path.join(shell_config_dir(), config.get_history())), app=APPLICATION, styles=style_obj, user_feedback=ask_feedback) shell_app.app.session["az_interactive_active"] = True shell_app.run() shell_app.app.session["az_interactive_active"] = False
def __init__(self, cli_ctx, style=None, completer=None, styles=None, lexer=None, history=InMemoryHistory(), app=None, input_custom=sys.stdin, output_custom=None, user_feedback=False, intermediate_sleep=.25, final_sleep=4): from azclishell.color_styles import style_factory self.cli_ctx = cli_ctx self.config = Configuration(cli_ctx.config, style=style) self.config.set_style(style) self.styles = style or style_factory(self.config.get_style()) self.lexer = lexer or get_az_lexer(self.config) if self.styles else None try: from azclishell.gather_commands import GatherCommands from azclishell.az_completer import AzCompleter self.completer = completer or AzCompleter(self, GatherCommands(self.config)) except IOError: # if there is no cache self.completer = None self.history = history or FileHistory(os.path.join(self.config.config_dir, self.config.get_history())) os.environ[ENV_ADDITIONAL_USER_AGENT] = 'AZURECLISHELL/' + __version__ self.telemetry = Telemetry(self.cli_ctx) # OH WHAT FUN TO FIGURE OUT WHAT THESE ARE! self._cli = None self.refresh_cli = False self.layout = None self.description_docs = u'' self.param_docs = u'' self.example_docs = u'' self._env = os.environ self.last = None self.last_exit = 0 self.user_feedback = user_feedback self.input = input_custom self.output = output_custom self.config_default = "" self.default_command = "" self.threads = [] self.curr_thread = None self.spin_val = -1 self.intermediate_sleep = intermediate_sleep self.final_sleep = final_sleep # try to consolidate state information here... # These came from key bindings... self._section = 1 self.is_prompting = False self.is_example_repl = False self.is_showing_default = False self.is_symbols = True
def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", "--helloworld"], } completable_param = ["--helloworld", "--funtimes", "-f"] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life" } same_param_doubles = {"-f": "--funtimes", "--funtimes": '-f'} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles) self.completer = AzCompleter(commands)
def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False)
def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", '--fun', "--helloworld"], } completable_param = [ "--helloworld", "--funtimes", "-f", '--fun' ] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life", "create --fun": "There is no work life balance, it's just your life" } same_param_doubles = { "create": [set(["-f", "--funtimes", '--fun']), set(['--unhap', '-u'])] } command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False)
class CompletionTest(unittest.TestCase): """ tests the completion generator """ def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_description = {"create": '', "command can": ''} commands = _Commands(command_tree=com_tree3, descrip=command_description) self.completer = AzCompleter(commands, global_params=False, outstream=six.StringIO()) def init2(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["-funtime"], } completable_param = ["-helloworld", "-funtime"] param_descript = { "create -funtime": "There is no work life balance, it's just your life" } command_description = {"create": '', "command can": ''} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, descrip=command_description) self.completer = AzCompleter(commands, global_params=False, outstream=six.StringIO()) def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", '--fun', "--helloworld"], } completable_param = ["--helloworld", "--funtimes", "-f", '--fun'] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life", "create --fun": "There is no work life balance, it's just your life" } same_param_doubles = { "create": [set(["-f", "--funtimes", '--fun']), set(['--unhap', '-u'])] } command_description = {"create": '', "command can": ''} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description) self.completer = AzCompleter(commands, global_params=False, outstream=six.StringIO()) def init4(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("createmore can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", "--helloworld"], } completable_param = ["--helloworld", "--funtimes", "-f"] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life" } same_param_doubles = {"create": [set(["-f", "--funtimes", '--fun'])]} command_description = {"create": '', "createmore can": ''} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description) self.completer = AzCompleter(commands, global_params=False, outstream=six.StringIO()) def test_command_completion(self): # tests general command completion self.init1() doc = Document(u'') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("command")) self.assertEqual(six.next(gen), Completion("create")) doc = Document(u'c') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("command", -1)) self.assertEqual(six.next(gen), Completion("create", -1)) doc = Document(u'cr') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("create", -2)) doc = Document(u'command ') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("can")) doc = Document(u'create ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_completion(self): # tests param completion self.init2() doc = Document(u'create -') gen = self.completer.get_completions(doc, None) self.assertEqual( six.next(gen), Completion("-funtime", -1, display_meta= "There is no work life balance, it's just your life")) doc = Document(u'command can -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_double(self): # tests not generating doubles for parameters self.init3() doc = Document(u'create -f --') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("--helloworld", -2)) doc = Document(u'create -f -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --fun -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --funtimes -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_second_completion(self): self.init3() doc = Document(u'crea ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --fun ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'command d ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --funtimes "life" --hello') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("--helloworld", -7)) def test_substring_completion(self): self.init4() doc = Document(u'create') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("createmore", -6))
class CompletionTest(unittest.TestCase): """ tests the completion generator """ def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) commands = _Commands(command_tree=com_tree3) self.completer = AzCompleter(commands) def init2(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["-funtime"], } completable_param = ["-helloworld", "-funtime"] param_descript = { "create -funtime": "There is no work life balance, it's just your life" } commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript) self.completer = AzCompleter(commands) def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", "--helloworld"], } completable_param = ["--helloworld", "--funtimes", "-f"] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life" } same_param_doubles = {"-f": "--funtimes", "--funtimes": '-f'} commands = _Commands(command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles) self.completer = AzCompleter(commands) def test_command_completion(self): """ tests general command completion """ self.init1() doc = Document(u'') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("create")) self.assertEqual(six.next(gen), Completion("command")) doc = Document(u'c') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("create", -1)) self.assertEqual(six.next(gen), Completion("command", -1)) doc = Document(u'cr') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("create", -2)) doc = Document(u'command ') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("can")) doc = Document(u'create ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_completion(self): """ tests param completion """ self.init2() doc = Document(u'create -') gen = self.completer.get_completions(doc, None) self.assertEqual( six.next(gen), Completion("-funtime", -1, display_meta= "There is no work life balance, it's just your life")) doc = Document(u'command can -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_double(self): """ tests not generating doubles for parameters """ self.init3() doc = Document(u'create -f --') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("--helloworld", -2)) doc = Document(u'create -f -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen)
def restart_completer(shell): shell.completer = AzCompleter(GatherCommands()) shell.refresh_cli = True
class CompletionTest(unittest.TestCase): """ tests the completion generator """ def init1(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False) def init2(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["-funtime"], } completable_param = [ "-helloworld", "-funtime" ] param_descript = { "create -funtime": "There is no work life balance, it's just your life" } command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False) def init3(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("command can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", '--fun', "--helloworld"], } completable_param = [ "--helloworld", "--funtimes", "-f", '--fun' ] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life", "create --fun": "There is no work life balance, it's just your life" } same_param_doubles = { "create": [set(["-f", "--funtimes", '--fun']), set(['--unhap', '-u'])] } command_description = { "create": '', "command can": '' } commands = _Commands( command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False) def init4(self): """ a variation of initializing """ com_tree1 = tree.generate_tree("createmore can") com_tree2 = tree.generate_tree("create") com_tree3 = tree.CommandHead() com_tree3.add_child(com_tree2) com_tree3.add_child(com_tree1) command_param = { "create": ["--funtimes", "-f", "--helloworld"], } completable_param = [ "--helloworld", "--funtimes", "-f" ] param_descript = { "create -f": "There is no work life balance, it's just your life", "create --funtimes": "There is no work life balance, it's just your life" } same_param_doubles = { "create": [set(["-f", "--funtimes", '--fun'])] } command_description = { "create": '', "createmore can": '' } commands = _Commands( command_tree=com_tree3, command_param=command_param, completable_param=completable_param, param_descript=param_descript, same_param_doubles=same_param_doubles, descrip=command_description ) self.completer = AzCompleter(commands, global_params=False) def test_command_completion(self): # tests general command completion self.init1() doc = Document(u'') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("command")) self.assertEqual(six.next(gen), Completion("create")) doc = Document(u'c') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("command", -1)) self.assertEqual(six.next(gen), Completion("create", -1)) doc = Document(u'cr') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("create", -2)) doc = Document(u'command ') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion("can")) doc = Document(u'create ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_completion(self): # tests param completion self.init2() doc = Document(u'create -') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion( "-funtime", -1, display_meta="There is no work life balance, it's just your life")) doc = Document(u'command can -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_param_double(self): # tests not generating doubles for parameters self.init3() doc = Document(u'create -f --') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion( "--helloworld", -2)) doc = Document(u'create -f -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --fun -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --funtimes -') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) def test_second_completion(self): self.init3() doc = Document(u'crea ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --fun ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'command d ') gen = self.completer.get_completions(doc, None) with self.assertRaises(StopIteration): six.next(gen) doc = Document(u'create --funtimes "life" --hello') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion( "--helloworld", -7)) def test_substring_completion(self): self.init4() doc = Document(u'create') gen = self.completer.get_completions(doc, None) self.assertEqual(six.next(gen), Completion( "createmore", -6))
def _build_completer(commands, global_params): from azure.cli.testsdk import TestCli from azclishell.app import AzInteractiveShell shell_ctx = AzInteractiveShell(TestCli(), None) return AzCompleter(shell_ctx, commands, global_params)
def restart_completer(shell_ctx): shell_ctx.completer = AzCompleter(shell_ctx, GatherCommands(shell_ctx.config)) shell_ctx.refresh_cli = True
azclishell from azclishell.gather_commands import GatherCommands from azclishell.app import Shell from azclishell.az_completer import AzCompleter from azclishell.az_lexer import AzLexer import azure.cli.core.azlogging as azlogging import azure.cli.core.telemetry as telemetry import azure.cli.core.telemetry as telemetry from azure.cli.core.application import APPLICATION, Configuration from azure.cli.core._session import ACCOUNT, CONFIG, SESSION from azure.cli.core._util import (show_version_info_exit, handle_exception) from azure.cli.core._environment import get_config_dir as cli_config_dir from azure.cli.core.application import APPLICATION AZCOMPLETER = AzCompleter(GatherCommands()) CONFIGURATION = azclishell.configuration.CONFIGURATION def main(): """ the main function """ azure_folder = cli_config_dir() if not os.path.exists(azure_folder): os.makedirs(azure_folder) ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json')) CONFIG.load(os.path.join(azure_folder, 'az.json')) SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600) config = CONFIGURATION