[cura-tools] master: introduce class caching (28a0590)
by phatina@fedoraproject.org
Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
>---------------------------------------------------------------
commit 28a05908f38ca6a5da1cef5039137870dfb0c039
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Mar 13 10:56:19 2013 +0100
introduce class caching
>---------------------------------------------------------------
cli-tools/lmi/lmi_client_shell.py | 71 ++++++++++++++++++++++++++++++++-
cli-tools/lmishell | 78 ++++++++++++++++++++++--------------
2 files changed, 116 insertions(+), 33 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index 2f5bda2..986375e 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -967,18 +967,77 @@ class _LmiInstance(BaseObject):
rval = self._client._delete_instance(self.path)
self._deleted = rval.rval == 0
+class _LmiShellCache(object):
+ def __init__(self, active = True, classname_list = None, classes = {}):
+ self._classname_list = classname_list
+ self._classes = classes
+ self._active = active
+
+ def clear(self):
+ self._classname_list = None
+ self._classes = {}
+
+ def get_classes(self):
+ return self._classname_list
+
+ def set_classes(self, classname_list):
+ self._classname_list = classname_list
+
+ def get_class(self, classname):
+ if not classname in self._classes:
+ return None
+ return self._classes[classname]
+
+ def add_class(self, cim_class):
+ self._classes[cim_class.classname] = cim_class
+
+ @property
+ def active(self):
+ return self._active
+
+ @active.setter
+ def active(self, val):
+ self._active = val
+
class _LmiShellClient(LmiBaseClient):
- def __init__(self, hostname, username = "", password = "", interactive = False):
+ def __init__(self, hostname, username = "", password = "", interactive = False, use_cache = True):
super(self.__class__, self).__init__(hostname, username, password)
self._interactive = interactive
+ self._cache = _LmiShellCache(use_cache)
+
+ def _get_class_names(self, namespace = None, **kwargs):
+ if self._cache.active:
+ class_list = self._cache.get_classes()
+ if class_list is None:
+ (class_list, _, errorstr) = LmiBaseClient._get_class_names(self, namespace, **kwargs)
+ if not class_list:
+ return LmiReturnValue(rval = class_list, errorstr = errorstr)
+ self._cache.set_classes(class_list)
+ return LmiReturnValue(rval = class_list)
+ return LmiBaseClient._get_class_names(self, namespace, **kwargs)
+
+ def _get_class(self, class_name, namespace = None, **kwargs):
+ if self._cache.active:
+ klass = self._cache.get_class(class_name)
+ if klass is None:
+ (klass, _, errorstr) = LmiBaseClient._get_class(self, class_name, namespace, **kwargs)
+ if not klass:
+ return LmiReturnValue(rval = klass, errorstr = errorstr)
+ self._cache.add_class(klass)
+ return LmiReturnValue(rval = klass)
+ return LmiBaseClient._get_class(self, class_name, namespace, **kwargs)
@property
def interactive(self):
return self._interactive
+ @interactive.setter
+ def interactive(self, i):
+ self._interactive = bool(i)
+
class LmiConnection(object):
- def __init__(self, hostname, username = "", password = "", interactive = False):
- self._client = _LmiShellClient(hostname, username, password, interactive)
+ def __init__(self, hostname, username = "", password = "", interactive = False, use_cache = True):
+ self._client = _LmiShellClient(hostname, username, password, interactive, use_cache)
@property
def hostname(self):
@@ -991,6 +1050,12 @@ class LmiConnection(object):
def print_namespaces(self):
sys.stdout.write("root\n")
+ def clear_cache(self):
+ self._client._cache.clear()
+
+ def use_cache(self, active = True):
+ self._client._cache.active = active
+
@property
def root(self):
return _LmiNamespaceRoot(self._client)
diff --git a/cli-tools/lmishell b/cli-tools/lmishell
index 4bb3849..f50731b 100755
--- a/cli-tools/lmishell
+++ b/cli-tools/lmishell
@@ -179,10 +179,42 @@ class _LmiCompleter(rlcompleter.Completer):
return self._last_complete[state]
return rlcompleter.Completer.complete(self, text, state)
-class LmiInteractiveShell(code.InteractiveConsole):
+class LmiInteractiveShellConfig(object):
DEFAULT_CONFIG_FILE = "~/.lmishellrc"
DEFAULT_HISTORY_FILE = "~/.lmi_shell_history"
DEFAULT_HISTORY_LENGTH = -1
+ DEFAULT_USE_CACHE = True
+
+ def __init__(self):
+ try:
+ conf = {}
+ execfile(os.path.expanduser(LmiInteractiveShellConfig.DEFAULT_CONFIG_FILE), conf)
+ self._history_file = os.path.expanduser(get_item_or_default(conf, "history_file",
+ LmiInteractiveShellConfig.DEFAULT_HISTORY_FILE))
+ self._history_length = get_item_or_default(conf, "history_length",
+ LmiInteractiveShellConfig.DEFAULT_HISTORY_LENGTH)
+ self._use_cache = get_item_or_default(conf, "use_cache",
+ LmiInteractiveShellConfig.DEFAULT_USE_CACHE)
+ except (SyntaxError, IOError), e:
+ if isinstance(e, SyntaxError):
+ sys.stderr.write("Error: %s\n" % e)
+ self._history_file = os.path.expanduser(LmiInteractiveShellConfig.DEFAULT_HISTORY_FILE)
+ self._history_length = LmiInteractiveShellConfig.DEFAULT_HISTORY_LENGTH
+ self._use_cache = LmiInteractiveShellConfig.DEFAULT_USE_CACHE
+
+ @property
+ def history_file(self):
+ return self._history_file
+
+ @property
+ def history_length(self):
+ return self._history_length
+
+ @property
+ def use_cache(self):
+ return self._use_cache
+
+class LmiInteractiveShell(code.InteractiveConsole):
DEFAULT_LOCALS = {
"LmiConnection" : LmiConnection,
"use_exceptions" : LmiBaseClient._set_use_exceptions,
@@ -195,18 +227,25 @@ class LmiInteractiveShell(code.InteractiveConsole):
"indication_listeners" : _lmi_indication_listeners_list
}
- def __init__(self, prompt, more_prompt, locals = None, **kwargs):
+ def __init__(self, prompt, more_prompt, locals = None, use_cache = True):
sys.ps1 = prompt
sys.ps2 = more_prompt
if locals is None:
locals = {}
+ else:
+ for (k, v) in locals.iteritems():
+ if isinstance(v, LmiConnection):
+ locals[k]._client.interactive = True
locals = dict(locals.items() + LmiInteractiveShell.DEFAULT_LOCALS.items())
- locals["connect"] = _connect_interactive
locals["clear_history"] = self.clear_history
locals["use_display_sugar"] = _use_display_sugar
code.InteractiveConsole.__init__(self, locals)
self._completer = _LmiCompleter(self.locals)
- self.load_config(get_item_or_default(kwargs, "config", LmiInteractiveShell.DEFAULT_CONFIG_FILE))
+ config = LmiInteractiveShellConfig()
+ self._history_file = config.history_file
+ self._history_length = config.history_length
+ self._use_cache = config.use_cache
+ locals["connect"] = lambda h, u = "", p = "": _connect(h, u, p, True, self._use_cache)
readline.set_completer(self._completer.complete)
readline.parse_and_bind('tab: complete')
@@ -244,20 +283,6 @@ class LmiInteractiveShell(code.InteractiveConsole):
more = 0
self.save_history()
- def load_config(self, config_file):
- try:
- conf = {}
- execfile(os.path.expanduser(config_file), conf)
- self._history_file = os.path.expanduser(get_item_or_default(conf, "history_file",
- LmiInteractiveShell.DEFAULT_HISTORY_FILE))
- self._history_length = get_item_or_default(conf, "history_length",
- LmiInteractiveShell.DEFAULT_HISTORY_LENGTH)
- except (SyntaxError, IOError), e:
- if isinstance(e, SyntaxError):
- sys.stderr.write("Error: %s\n" % e)
- self._history_file = os.path.expanduser(LmiInteractiveShell.DEFAULT_HISTORY_FILE)
- self._history_length = LmiInteractiveShell.DEFAULT_HISTORY_LENGTH
-
def load_history(self):
if self._history_length == 0 or not os.path.exists(self._history_file):
return
@@ -281,13 +306,7 @@ def _lmi_interact(locals = None):
console = LmiInteractiveShell("> ", "... ", locals)
console.interact()
-def _connect_interactive(hostname, username = "", password = ""):
- return _connect(hostname, username, password, True)
-
-def _connect_noninteractive(hostname, username = "", password = ""):
- return _connect(hostname, username, password, False)
-
-def _connect(hostname, username = "", password = "", interactive = False):
+def _connect(hostname, username = "", password = "", interactive = False, use_cache = True):
if not username:
while True:
try:
@@ -315,7 +334,7 @@ def _connect(hostname, username = "", password = "", interactive = False):
sys.stdout.write("\n")
readline.remove_history_item(readline.get_current_history_length() - 1)
# Try to get some non-existing class as a login check
- connection = LmiConnection(hostname, username, password, interactive)
+ connection = LmiConnection(hostname, username, password, interactive, use_cache)
use_exceptions = LmiBaseClient._get_use_exceptions()
try:
LmiBaseClient._set_use_exceptions(True)
@@ -349,8 +368,10 @@ if __name__ == "__main__":
if options.interactive:
_lmi_interact()
else:
+ config = LmiInteractiveShellConfig()
+ use_cache = config.use_cache
locals = copy.deepcopy(LmiInteractiveShell.DEFAULT_LOCALS)
- locals["connect"] = _connect_noninteractive
+ locals["connect"] = lambda h, u = "", p = "": _connect(h, u, p, False, use_cache)
sys.argv = options.script_args
try:
execfile(options.script_name, locals)
@@ -358,8 +379,5 @@ if __name__ == "__main__":
if not options.inspect:
sys.exit(e.code)
if options.inspect:
- for (k, v) in locals.iteritems():
- if isinstance(v, LmiConnection):
- locals[k]._client._interactive = True
_lmi_interact(locals)
sys.exit(0)