1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
from configshell_fb import ConfigNode
from gwcli.utils import console_message
import logging
__author__ = 'Paul Cuzner'
class UICommon(ConfigNode):
def __init__(self, name, parent=None, shell=None):
ConfigNode.__init__(self, name, parent, shell)
self.logger = logging.getLogger('gwcli')
def ui_command_goto(self, shortcut='/'):
'''
cd to the bookmark at shortcut.
See 'help bookmarks' for more info on bookmarks.
'''
if shortcut in self.shell.prefs['bookmarks']:
return self.ui_command_cd(self.shell.prefs['bookmarks'][shortcut])
else:
pass
def get_ui_root(self):
found = False
obj = self
while not found:
if obj.__class__.__name__ == 'ISCSIRoot':
break
obj = obj.parent
return obj
class UIGroup(UICommon):
def __init__(self, name, parent=None, shell=None):
UICommon.__init__(self, name, parent, shell)
self.http_mode = self.parent.http_mode
def reset(self):
children = set(self.children) # set of child objects
for child in children:
self.remove_child(child)
class UINode(UIGroup):
display_attributes = None
def __init__(self, name, parent):
UIGroup.__init__(self, name, parent)
self.http_mode = self.parent.http_mode
def ui_command_info(self):
"""
Show the attributes of the current object.
"""
text = self.get_info()
console_message(text)
def get_info(self):
"""
extract the relevant display fields from the object and format
ready for printing
:return: (str) object meta data based on object's display_attributes
list
"""
display_text = ''
if not self.display_attributes:
return "'info' not available for this item"
field_list = self.display_attributes
max_field_size = len(max(field_list, key=len))
for k in field_list:
attr_label = k.replace('_', ' ').title()
attr_value = getattr(self, k)
if isinstance(attr_value, dict):
if attr_value:
display_text += "{}\n".format(attr_label)
max_dict_field = len(max(attr_value.keys(), key=len))
for dict_key in sorted(attr_value):
if isinstance(attr_value[dict_key], dict):
inner_dict = attr_value[dict_key]
display_value = ", ".join(["=".join(
[key, str(val)]) for key, val in inner_dict.items()])
display_text += ("- {:<{}} .. {}\n".format(dict_key,
max_dict_field,
display_value))
else:
display_text += ("- {} .. {}\n".format(dict_key,
attr_value[dict_key]))
continue
else:
attr_value = 'UNDEFINED\n'
if isinstance(attr_value, list):
item_1 = True
attr_string = ''
for item in attr_value:
if item_1:
attr_string = "{}\n".format(str(item))
item_1 = False
else:
attr_string += "{}{}\n".format(" " * (max_field_size + 4),
str(item))
attr_value = attr_string[:-1]
display_text += ("{:<{}} .. {}\n".format(attr_label,
max_field_size,
attr_value))
return display_text
class UIRoot(UICommon):
"""
The gwcli hierarchy root node.
"""
def __init__(self, shell, as_root=False):
UICommon.__init__(self, '/', shell=shell)
self.as_root = as_root
|