def read_help_file(self, help_file_path): with utils.open_for_read_file_or_url(help_file_path) as file_fd: for a_node in yaml.compose_all(file_fd): if a_node.isMapping(): for topic_name, topic_items_node in a_node.items(): for item_name, item_value_node in topic_items_node.items(): newItem = HelpItem(topic_name, item_name) newItem.read_from_yaml(item_value_node) self.help_items[item_name] = newItem
def provision_public_key_text(self): if "PUBLIC_KEY" not in var_stack: if "PUBLIC_KEY_FILE" in var_stack: public_key_file = var_stack.ResolveVarToStr("PUBLIC_KEY_FILE") with utils.open_for_read_file_or_url(public_key_file, connectionBase.translate_url, self.path_searcher) as file_fd: public_key_text = file_fd.read() var_stack.set_var("PUBLIC_KEY", "from " + public_key_file).append(public_key_text) else: raise ValueError("No public key, variables PUBLIC_KEY & PUBLIC_KEY_FILE are not defined") resolved_public_key = var_stack.ResolveVarToStr("PUBLIC_KEY") return resolved_public_key
def init_sync_vars(self): """ Prepares variables for sync. Will raise ValueError if a mandatory variable is not defined. """ prerequisite_vars = var_stack.ResolveVarToList("__SYNC_PREREQUISITE_VARIABLES__") self.instlObj.check_prerequisite_var_existence(prerequisite_vars) if "PUBLIC_KEY" not in var_stack: if "PUBLIC_KEY_FILE" in var_stack: public_key_file = var_stack.ResolveVarToStr("PUBLIC_KEY_FILE") with utils.open_for_read_file_or_url(public_key_file, connectionBase.translate_url, self.instlObj.path_searcher) as file_fd: public_key_text = file_fd.read() var_stack.set_var("PUBLIC_KEY", "from " + public_key_file).append(public_key_text) self.instlObj.calc_user_cache_dir_var() # this will set USER_CACHE_DIR if it was not explicitly defined
def read_from_file(self, in_file, a_format="guess"): """ Reads from file. All previous sub items are cleared before reading, unless the a_format is 'props' in which case the properties are added to existing sub items. raises ValueError is a_format is not supported. """ if a_format == "guess": _, extension = os.path.splitext(in_file) a_format = map_info_extension_to_format[extension[1:]] self.comments.append("Original file " + in_file) if a_format in list(self.read_func_by_format.keys()): with utils.open_for_read_file_or_url(in_file) as rfd: if a_format not in ("props", "file-sizes"): self.clear_all() self.read_func_by_format[a_format](rfd) self.files_read_list.append(in_file) else: raise ValueError("Unknown read a_format " + a_format)
def read_yaml_file(self, file_path, *args, **kwargs): try: with utils.open_for_read_file_or_url(file_path, self.url_translator, self.path_searcher) as file_fd: buffer = file_fd.read() buffer = utils.unicodify(buffer) # make sure text is unicode buffer = io.StringIO(buffer) # turn text to a stream buffer.name = file_path # this will help identify the file for debugging and messages kwargs['path-to-file'] = file_path self.read_yaml_from_stream(buffer, *args, **kwargs) except (FileNotFoundError, urllib.error.URLError) as ex: ignore = kwargs.get('ignore_if_not_exist', False) if ignore: print("'ignore_if_not_exist' specified, ignoring FileNotFoundError for", file_path) else: print("Exception reading file:", file_path, ex) raise except Exception as ex: print("Exception reading file:", file_path, ex) raise
def nodeToYamlDumpWrap(a_node): retVal = None if a_node.isScalar(): retVal = YamlDumpWrap(str(a_node.value)) elif a_node.isSequence(): seq = [nodeToYamlDumpWrap(item) for item in a_node.value] retVal = YamlDumpWrap(seq) elif a_node.isMapping(): amap = { str(_key.value): nodeToYamlDumpWrap(_val) for (_key, _val) in a_node.value } retVal = YamlDumpWrap(amap) return retVal if __name__ == "__main__": try: import utils for afile in sys.argv[1:]: with utils.open_for_read_file_or_url( afile, config_vars=None) as open_file: for a_node in yaml.compose_all(open_file.fd): a_node_as_tdw = nodeToYamlDumpWrap(a_node) docWrap = YamlDumpDocWrap(a_node_as_tdw) writeAsYaml(docWrap) except Exception as ex: import traceback tb = traceback.format_exc() print(tb)
retVal = {str(_key.value): nodeToPy(_val) for (_key, _val) in a_node.value} return retVal def nodeToYamlDumpWrap(a_node): retVal = None if a_node.isScalar(): retVal = YamlDumpWrap(str(a_node.value)) elif a_node.isSequence(): seq = [nodeToYamlDumpWrap(item) for item in a_node.value] retVal = YamlDumpWrap(seq) elif a_node.isMapping(): amap = {str(_key.value): nodeToYamlDumpWrap(_val) for (_key, _val) in a_node.value} retVal = YamlDumpWrap(amap) return retVal if __name__ == "__main__": try: import utils for afile in sys.argv[1:]: with utils.open_for_read_file_or_url(afile) as fd: for a_node in yaml.compose_all(fd): a_node_as_tdw = nodeToYamlDumpWrap(a_node) docWrap = YamlDumpDocWrap(a_node_as_tdw) writeAsYaml(docWrap) except Exception as ex: import traceback tb = traceback.format_exc() print(tb)