def _create_new_list_element_in_schema(self, path, string_composite_key): """ This method takes in a path and will extend the schema so that the list element includes the same schema data. e.g. if we have a path /root/simplelist which is a list with a __listelement describing it's schema. And this is called with a path such as /root/simplelist/glow then we will end up with /root/simplelist/glow with a matching schema. THe paths on the new listelement will be adjusted with the new keys. """ self.log.trace('%s %s ~extend-schema', convert_path_to_slash_string(path), string_composite_key) val = path.pop() list_element = dpath.util.get(self.schema, path) path.append(val) new_list_element = {} for list_item in list_element['__listelement']: new_list_element[list_item] = copy.deepcopy( list_element['__listelement'][list_item]) if '__schema' in new_list_element[list_item]: new_list_element[list_item]['__schema'][ '__path'] = new_list_element[list_item]['__schema'][ '__path'].replace('/__listelement', '{' + string_composite_key + '}', 1) dpath.util.new(self.schema, path, new_list_element)
def set(self, path, set_val, separator=' '): """Set the value of a leaf node. This function requires a decoded path as a string e.g. ['root', 'brewhouse', 'temperature', 'mash', 'setpoint'] -> 65 TODO: This needs to validate against the schema! """ path_string = convert_path_to_slash_string(path) self.log.trace('%s -> %s (separator %s)', path_string, set_val, separator) node_type = self.get_type(path_string, '/') self.log.trace('%s VALIDATE %s' % (path_string, node_type)) set_val = self.validate_against_schema({'__schema': node_type}, set_val, path_string) self.keyval[path_string] = set_val regex = re.compile("{([A-Za-z0-9]*)}\/?") path_string = regex.sub('/{\g<1>}/', path_string) self.log.trace('updated patH_string inside set %s' % (path_string)) path = decode_path_string(path_string, separator) self.log.trace('%s %s' % (path, set_val)) self.log.trace('%s' % (self.db)) dpath.util.new(self.db, path, set_val, separator='/') print(self.db) print(self.keyval)
def _auto_complete(self, line, text, cmd='show ', config=True, filter_blank_values=False): """ line - the full line of text (e.g. show fermentation text - the text fragment autom completing (e.g. fermentation) Note: cmd2 will swallow any exceptions and the command-line-completion won't behave as we expect. Examples: line text result 'show ' '' ['brewhouse ' ', 'ingredients ', 'recipes '] 'show br' 'br' ['brewhouse '] if text = '' then we search datastore for the full pth if text != '' then we have to search the datastore for everything except the prtial element. """ if config: database = 'config' else: database = 'oper' try: strip_partial_elements = 0 # Attempt to get the path which might not exist cmds = [] try: if not text == '': strip_partial_elements = 1 path_to_find = decode_path_string( line[len(cmd):], ignore_last_n=strip_partial_elements, ignore_root=True) slash_path = convert_path_to_slash_string(path_to_find) xcmds = self.pyconfhoarddata.list(path_to_find, separator='/') cmds = [] for key in xcmds: if key[0:len(text)] == text: cmds.append(key + ' ') except Exception as err: print(traceback.format_exc()) print(str(err)), '<auto-complete inner' pass cmds.sort() except Exception as err: print(traceback.format_exc()) print(str(err)) pass return cmds
def __init__(self, path, keys): message = 'Path %s is a list with the following keys required %s' % ( convert_path_to_slash_string(path), keys) super().__init__(message)
def __init__(self, path): message = 'Path %s is not a list: ' % ( convert_path_to_slash_string(path)) super().__init__(message)
def __init__(self, path): message = 'Path %s is not a configuration leaf.' % ( convert_path_to_slash_string(path)) super().__init__(message)