Michael Pasternak has uploaded a new change for review. Change subject: cli: disable /history while not connected ......................................................................
cli: disable /history while not connected Change-Id: I1bb7e7e2d631587bebc8dd27fef577a48dbe43d1 Signed-off-by: Michael Pasternak <mpast...@redhat.com> --- M src/ovirtcli/command/connect.py M src/ovirtcli/command/disconnect.py M src/ovirtcli/command/history.py M src/ovirtcli/historymanager.py 4 files changed, 53 insertions(+), 18 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/53/8053/1 diff --git a/src/ovirtcli/command/connect.py b/src/ovirtcli/command/connect.py index 75af762..49784b2 100644 --- a/src/ovirtcli/command/connect.py +++ b/src/ovirtcli/command/connect.py @@ -108,6 +108,7 @@ self.context._set_prompt() stdout.write(OvirtCliSettings.CONNECTED_TEMPLATE % \ self.context.settings.get('ovirt-shell:version')) + self.context.history.enable() except RequestError, e: self.__cleanContext() diff --git a/src/ovirtcli/command/disconnect.py b/src/ovirtcli/command/disconnect.py index 01c84d2..e50917d 100644 --- a/src/ovirtcli/command/disconnect.py +++ b/src/ovirtcli/command/disconnect.py @@ -49,5 +49,7 @@ self.context.status = ExecutionContext.OK except Exception: self.context.status = ExecutionContext.COMMAND_ERROR + finally: + self.context.history.disable() stdout.write(OvirtCliSettings.DISCONNECTED_TEMPLATE) self.context.connection = None diff --git a/src/ovirtcli/command/history.py b/src/ovirtcli/command/history.py index 5dcacf9..7e3c51b 100644 --- a/src/ovirtcli/command/history.py +++ b/src/ovirtcli/command/history.py @@ -57,14 +57,17 @@ slide = int(indx) h_item = context.history.get(slide) if h_item: + self.write('') self.write(hformat % (slide , str(h_item))) + self.write('') except Exception, e: self.error(str(e)) else: i = 0 history = context.history.list() if history: + self.write('') for item in history: - self.write(hformat % (i , str(item))) + self.write(hformat % (i + 1 , str(item))) i += 1 self.write('') diff --git a/src/ovirtcli/historymanager.py b/src/ovirtcli/historymanager.py index 1875235..a696590 100644 --- a/src/ovirtcli/historymanager.py +++ b/src/ovirtcli/historymanager.py @@ -19,28 +19,27 @@ import atexit import os from ovirtcli.settings import OvirtCliSettings +import threading -HISTORY_METAFILE = "~/." + OvirtCliSettings.PRODUCT.lower() + "-shell.history" +HISTORY_METAFILE = "~/." + OvirtCliSettings.PRODUCT.lower() + "shellhistory" +TMP_HISTORY_METAFILE = HISTORY_METAFILE + ".tmp" class HistoryManager(code.InteractiveConsole): """ Provides history management capabilities """ - def __init__(self, locals=None, filename="<console>", - histfile=os.path.expanduser(HISTORY_METAFILE)): - code.InteractiveConsole.__init__(self, locals, filename) - self.__init_history(histfile) + def __init__(self, + locals=None, + filename="<console>", + histfile=os.path.expanduser(HISTORY_METAFILE), + temp_histfile=os.path.expanduser(TMP_HISTORY_METAFILE), + enabled=False): + + self.lock = threading.RLock() + self.enabled = enabled self.histfile = histfile - - def __init_history(self, histfile): - if hasattr(readline, "read_history_file"): - try: - readline.read_history_file(histfile) - except IOError: - pass - atexit.register(self.__register_history_metafile, histfile) - - def __register_history_metafile(self, histfile): - readline.write_history_file(histfile) + self.tmp_histfile = temp_histfile + code.InteractiveConsole.__init__(self, locals, filename) + if enabled: self.enable() def get(self, indx): return readline.get_history_item(indx) @@ -53,7 +52,7 @@ ln = self.length() if ln > 0: for i in range(ln): - buff.append(self.get(i)) + if i > 0: buff.append(self.get(i)) return buff def export(self, filename): @@ -61,3 +60,33 @@ def clear(self): readline.clear_history() + + def enable(self): + with self.lock: + self.enabled = True + self.__register_file(self.histfile) + + def disable(self): + with self.lock: + self.enabled = False + self.__dump_callback(self.histfile) + self.__register_file(self.tmp_histfile) + self.clear() + + def __unregister_dump_callback(self): + for item in atexit._exithandlers: + if hasattr(item[0], 'func_name') and \ + item[0].func_name == '__dump_callback': + atexit._exithandlers.remove(item) + + def __register_file(self, filename): + self.__unregister_dump_callback() + if hasattr(readline, "read_history_file"): + try: + readline.read_history_file(filename) + except IOError: + pass + atexit.register(self.__dump_callback, filename) + + def __dump_callback(self, histfile): + readline.write_history_file(histfile) -- To view, visit http://gerrit.ovirt.org/8053 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1bb7e7e2d631587bebc8dd27fef577a48dbe43d1 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Michael Pasternak <mpast...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches