From a49347d49dcb7c129e88e493edb2162b0abfe07e Mon Sep 17 00:00:00 2001 From: Seblu Date: Thu, 6 Jan 2011 18:04:48 +0100 Subject: [PATCH] disable history inside ask expert has its own history --- README | 3 ++- bin/cc-cli | 1 + cccli/cli.py | 2 +- cccli/command.py | 8 +++++--- cccli/printer.py | 26 +++++++++++++++++++++----- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/README b/README index 144069a..6f7519b 100644 --- a/README +++ b/README @@ -41,7 +41,8 @@ alias vm "list vm&status=running$cpu" ======= History ======= -History is stored in ~/.local/share/cc-cli/history +CLI history is stored in ~/.local/share/cc-cli/history +Expoert mode history is stored in ~/.local/share/cc-cli/expert === TQL diff --git a/bin/cc-cli b/bin/cc-cli index 5fd87a5..2c2ff7c 100755 --- a/bin/cc-cli +++ b/bin/cc-cli @@ -25,6 +25,7 @@ settings = { "hsize": "100", "alias": "%s/alias"%BaseDirectory.save_config_path(cccli.canonical_name), "history": "%s/history"%BaseDirectory.save_data_path(cccli.canonical_name), + "expert": "%s/expert"%BaseDirectory.save_data_path(cccli.canonical_name), } try: diff --git a/cccli/cli.py b/cccli/cli.py index 4964d12..2e87f1a 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -61,7 +61,7 @@ class Cli(object): # parsing self._parse() # save history - self.printer.history.save(self.settings.get("history", "")) + self.printer.history.write(self.settings.get("history", "")) def _connect(self): '''Connect to a cloud control server''' diff --git a/cccli/command.py b/cccli/command.py index b29b939..0c19daa 100644 --- a/cccli/command.py +++ b/cccli/command.py @@ -453,16 +453,18 @@ class Command(object): def cmd_expert(self, argv): '''Switch in expert mode''' - self.printer.history.save(self.cli.settings.get("history", "")) + h = list(self.printer.history) + self.printer.history.read(self.cli.settings.get("expert", "")) try: local = dict() local["cli"] = self.cli local["rpc"] = self.cli.rpc local["proxy"] = ConnectionProxy(self.cli.rpc) c = code.InteractiveConsole(local) - c.interact("Use Ctrl+D to go back in CLI") + c.interact("Use Ctrl+D to go back in CLI. Type dir() to see variables.") finally: - self.printer.history.load(self.cli.settings.get("history", "")) + self.printer.history.write(self.cli.settings.get("expert", "")) + self.printer.history.load(h) cmd_expert.usage = "expert" def cmd_whoami(self, argv): diff --git a/cccli/printer.py b/cccli/printer.py index 4ed45ff..4edd905 100644 --- a/cccli/printer.py +++ b/cccli/printer.py @@ -67,7 +67,7 @@ class Printer(object): self.history = History(self.readline) else: self.history.readline = readline - self.history.load(self.historyfile) + self.history.read(self.historyfile) self.history.maxsize(self.historysize) def out(self, message="", fd=sys.stdout, nl=os.linesep, flush=True): @@ -101,6 +101,7 @@ class Printer(object): self.out(message, fd, nl) def getline(self, prompt, history=True): + '''Read a line from stdin''' try: s = raw_input(prompt) except EOFError: @@ -123,7 +124,13 @@ class Printer(object): '''Used to ask a question. Default answer not saved to history''' if self.readline is None: raise cliError("Unable to ask question in non-interactive mode") - return self.getline(prompt, history=False) + h = list(self.history) + self.history.clear() + try: + r = self.getline(prompt, history=False) + finally: + self.history.load(h) + return r class History(object): @@ -143,7 +150,9 @@ class History(object): return object.__getattribute__(self, name) def __iter__(self): - for i in range(1, len(self)): + if self.readline is None: + return + for i in range(1, self.readline.get_current_history_length() + 1): yield self.readline.get_history_item(i) def __len__(self): @@ -151,14 +160,21 @@ class History(object): return 0 return self.readline.get_current_history_length() - def load(self, path): + def load(self, items): + '''Load history from a list''' + self.clear() + for l in items: + self.readline.add_history(l) + + def read(self, path): '''Load history from a file''' + self.clear() try: self.readline.read_history_file(path) except IOError: pass - def save(self, path): + def write(self, path): '''Save history into path''' try: self.readline.write_history_file(path) -- GitLab