Python ruamel.yaml.dump() Examples
The following are 30
code examples of ruamel.yaml.dump().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ruamel.yaml
, or try the search function
.
Example #1
Source File: box.py From lcnn with MIT License | 6 votes |
def to_json(self, filename=None, encoding="utf-8", errors="strict", multiline=False, **json_kwargs): """ Transform the BoxList object into a JSON string. :param filename: If provided will save to file :param encoding: File encoding :param errors: How to handle encoding errors :param multiline: Put each item in list onto it's own line :param json_kwargs: additional arguments to pass to json.dump(s) :return: string of JSON or return of `json.dump` """ if filename and multiline: lines = [_to_json(item, filename=False, encoding=encoding, errors=errors, **json_kwargs) for item in self] with open(filename, 'w', encoding=encoding, errors=errors) as f: f.write("\n".join(lines).decode('utf-8') if sys.version_info < (3, 0) else "\n".join(lines)) else: return _to_json(self.to_list(), filename=filename, encoding=encoding, errors=errors, **json_kwargs)
Example #2
Source File: oc_obj.py From ansible-redhat_openshift_utils with Apache License 2.0 | 6 votes |
def create_tmp_file_from_contents(rname, data, ftype='yaml'): ''' create a file in tmp with name and contents''' tmp = Utils.create_tmpfile(prefix=rname) if ftype == 'yaml': # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage # pylint: disable=no-member if hasattr(yaml, 'RoundTripDumper'): Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper)) else: Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False)) elif ftype == 'json': Utils._write(tmp, json.dumps(data)) else: Utils._write(tmp, data) # Register cleanup when module is done atexit.register(Utils.cleanup, [tmp]) return tmp
Example #3
Source File: run_tests.py From nni with MIT License | 6 votes |
def prepare_config_file(test_case_config, it_config, args): config_path = args.nni_source_dir + test_case_config['configFile'] test_yml_config = get_yml_content(config_path) # apply test case specific config if test_case_config.get('config') is not None: deep_update(test_yml_config, test_case_config['config']) # hack for windows if sys.platform == 'win32' and args.ts == 'local': test_yml_config['trial']['command'] = test_yml_config['trial']['command'].replace('python3', 'python') # apply training service config # user's gpuNum, logCollection config is overwritten by the config in training_service.yml # the hack for kubeflow should be applied at last step update_training_service_config(test_yml_config, args.ts) # generate temporary config yml file to launch experiment new_config_file = config_path + '.tmp' dump_yml_content(new_config_file, test_yml_config) print(yaml.dump(test_yml_config, default_flow_style=False), flush=True) return new_config_file
Example #4
Source File: oc_obj.py From ansible-redhat_openshift_utils with Apache License 2.0 | 6 votes |
def create(self, files=None, content=None): ''' Create a config NOTE: This creates the first file OR the first conent. TODO: Handle all files and content passed in ''' if files: return self._create(files[0]) # pylint: disable=no-member # The purpose of this change is twofold: # - we need a check to only use the ruamel specific dumper if ruamel is loaded # - the dumper or the flow style change is needed so openshift is able to parse # the resulting yaml, at least until gopkg.in/yaml.v2 is updated if hasattr(yaml, 'RoundTripDumper'): content['data'] = yaml.dump(content['data'], Dumper=yaml.RoundTripDumper) else: content['data'] = yaml.safe_dump(content['data'], default_flow_style=False) content_file = Utils.create_tmp_files_from_contents(content)[0] return self._create(content_file['path']) # pylint: disable=too-many-function-args
Example #5
Source File: oc_obj.py From ansible-redhat_openshift_utils with Apache License 2.0 | 6 votes |
def create_tmp_file_from_contents(rname, data, ftype='yaml'): ''' create a file in tmp with name and contents''' tmp = Utils.create_tmpfile(prefix=rname) if ftype == 'yaml': # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage # pylint: disable=no-member if hasattr(yaml, 'RoundTripDumper'): Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper)) else: Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False)) elif ftype == 'json': Utils._write(tmp, json.dumps(data)) else: Utils._write(tmp, data) # Register cleanup when module is done atexit.register(Utils.cleanup, [tmp]) return tmp
Example #6
Source File: item_conversion.py From smarthome with GNU General Public License v3.0 | 6 votes |
def convert_yaml(data): """ ***Converter Special *** Convert data structure to yaml format :param data: OrderedDict to convert :return: yaml formated data """ ordered = (type(data).__name__ == 'OrderedDict') if ordered: sdata = _ordered_dump(data, Dumper=yaml.SafeDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=2, width=32768, allow_unicode=True, default_flow_style=False) else: sdata = yaml.dump(data, Dumper=yaml.SafeDumper, indent=indent_spaces, block_seq_indent=2, width=32768, allow_unicode=True, default_flow_style=False) sdata = _format_yaml_dump(sdata) return sdata
Example #7
Source File: item_conversion.py From smarthome with GNU General Public License v3.0 | 6 votes |
def _ordered_dump(data, stream=None, Dumper=yaml.Dumper, **kwds): """ Ordered yaml dumper Use this instead ot yaml.Dumper/yaml.SaveDumper to get an Ordereddict :param stream: stream to write to :param Dumper: yaml-dumper to use :**kwds: Additional keywords :return: OrderedDict structure """ # usage example: ordered_dump(data, Dumper=yaml.SafeDumper) class OrderedDumper(Dumper): pass def _dict_representer(dumper, data): return dumper.represent_mapping( yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items()) OrderedDumper.add_representer(OrderedDict, _dict_representer) return yaml.dump(data, stream, OrderedDumper, **kwds)
Example #8
Source File: engine.py From ansible-playbook-bundle with GNU General Public License v2.0 | 6 votes |
def print_service(service): cmap = ruamel.yaml.comments.CommentedMap() if 'name' in service: cmap['name'] = service['name'] if 'id' in service: cmap['id'] = service['id'] if 'description' in service: cmap['description'] = service['description'] if 'bindable' in service: cmap['bindable'] = service['bindable'] if 'metadata' in service: cmap['metadata'] = service['metadata'] if 'plans' in service: cmap['plans'] = pretty_plans(service['plans']) print(ruamel.yaml.dump(cmap, Dumper=ruamel.yaml.RoundTripDumper))
Example #9
Source File: test_config.py From conda-press with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_populate_config_by_yaml(config_content, tmpdir): yaml_path = tmpdir.join("TEST.yaml") yaml_path.write(yaml.dump(config_content)) config_read = get_config_by_yaml(str(yaml_path)) assert config_read.channels == ["FOO-CHANNEL", "CHANNEL2"] assert config_read.get_all_channels() == [ "FOO-CHANNEL", "CHANNEL2", "conda-forge", "anaconda", "main", "r", ] assert config_read.subdir == "SUBDIR" assert config_read.get_all_subdir() == ["SUBDIR", "noarch"] assert config_read.output == "OUTPUT" assert config_read.fatten assert config_read.skip_python assert not config_read.strip_symbols assert config_read.merge assert config_read.exclude_deps == {"EXCLUDE1", "EXCLUDE2"} assert config_read.add_deps == {"ADD1", "ADD2"} assert config_read.only_pypi assert not config_read.include_requirements
Example #10
Source File: box.py From lcnn with MIT License | 6 votes |
def to_yaml(self, filename=None, default_flow_style=False, encoding="utf-8", errors="strict", **yaml_kwargs): """ Transform the Box object into a YAML string. :param filename: If provided will save to file :param default_flow_style: False will recursively dump dicts :param encoding: File encoding :param errors: How to handle encoding errors :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML or return of `yaml.dump` """ return _to_yaml(self.to_dict(), filename=filename, default_flow_style=default_flow_style, encoding=encoding, errors=errors, **yaml_kwargs)
Example #11
Source File: box.py From lcnn with MIT License | 6 votes |
def to_yaml(self, filename=None, default_flow_style=False, encoding="utf-8", errors="strict", **yaml_kwargs): """ Transform the BoxList object into a YAML string. :param filename: If provided will save to file :param default_flow_style: False will recursively dump dicts :param encoding: File encoding :param errors: How to handle encoding errors :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML or return of `yaml.dump` """ return _to_yaml(self.to_list(), filename=filename, default_flow_style=default_flow_style, encoding=encoding, errors=errors, **yaml_kwargs)
Example #12
Source File: shyaml.py From smarthome with GNU General Public License v3.0 | 6 votes |
def _format_yaml_dump(data): """ Format yaml-dump to make file more readable (yaml structure must be dumped to a stream before using this function) | Currently does the following: | - Add an empty line before a new item :param data: string to format :return: formatted string """ data = data.replace('\n\n', '\n') ldata = data.split('\n') rdata = [] for index, line in enumerate(ldata): if line[-1:] == ':': # no empty line before list attributes if ldata[index+1].strip()[0] != '-': rdata.append('') rdata.append(line) else: rdata.append(line) fdata = '\n'.join(rdata) return fdata
Example #13
Source File: migration.py From eNMS with GNU General Public License v3.0 | 6 votes |
def update_property(project, property, value=None, types=None): if not types: types = import_classes path = ( Path.cwd() / "Desktop" / "shared" / "project" / "eNMS" / "files" / "migrations" / project ) for instance_type in types: with open(path / f"{instance_type}.yaml", "r") as migration_file: objects = yaml.load(migration_file) for obj in objects: obj["devices"] = [] obj["pools"] = [] with open(path / f"{instance_type}.yaml", "w") as migration_file: yaml.dump(objects, migration_file)
Example #14
Source File: shyaml.py From smarthome with GNU General Public License v3.0 | 6 votes |
def yaml_save_roundtrip(filename, data, create_backup=False): """ Dump yaml using the RoundtripDumper and correct linespacing in output file :param filename: name of the yaml file to save to :param data: data structure to save """ if not EDITING_ENABLED: return sdata = yaml.dump(data, Dumper=yaml.RoundTripDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=block_seq_indent, width=12288, allow_unicode=True) sdata = _format_yaml_dump2( sdata ) if not filename.lower().endswith('.yaml'): filename += YAML_FILE if create_backup: if os.path.isfile(filename): shutil.copy2(filename, filename+'.bak') with open(filename, 'w') as outfile: outfile.write( sdata )
Example #15
Source File: script_posts_export.py From TorCMS with MIT License | 6 votes |
def run_export(): all_recs = MPost.query_all(kind='m', limit=10000) out_arr = [] for postinfo in all_recs: out_arr.append( { 'uid': postinfo.uid, 'title': postinfo.title, 'keywords': postinfo.keywords, 'date': postinfo.date, 'extinfo': postinfo.extinfo, 'cnt_md': postinfo.cnt_md, 'cnt_html': postinfo.cnt_html, 'kind': postinfo.kind, 'user_name': postinfo.user_name, 'logo': '', } ) dumper = ruamel.yaml.RoundTripDumper with open('xx_posts.yaml', 'w') as fo: yaml.dump(out_arr, fo, Dumper=dumper, allow_unicode=True)
Example #16
Source File: administration.py From eNMS with GNU General Public License v3.0 | 6 votes |
def export_service(self, service_id): service = db.fetch("service", id=service_id) path = Path(self.path / "files" / "services" / service.filename) path.mkdir(parents=True, exist_ok=True) services = service.deep_services if service.type == "workflow" else [service] services = [service.to_dict(export=True) for service in services] for service_dict in services: for relation in ("devices", "pools", "events"): service_dict.pop(relation) with open(path / "service.yaml", "w") as file: yaml.dump(services, file) if service.type == "workflow": with open(path / "workflow_edge.yaml", "w") as file: yaml.dump( [edge.to_dict(export=True) for edge in service.deep_edges], file ) with open_tar(f"{path}.tgz", "w:gz") as tar: tar.add(path, arcname=service.filename) rmtree(path, ignore_errors=True)
Example #17
Source File: utility.py From soccer-matlab with BSD 2-Clause "Simplified" License | 6 votes |
def save_config(config, logdir): """Save a new configuration by name. If a logging directory is specified, is will be created and the configuration will be stored there. Otherwise, a log message will be printed. Args: config: Configuration object. logdir: Location for writing summaries and checkpoints if specified. Returns: Configuration object. """ message = 'Start a new run and write summaries and checkpoints to {}.' print(message.format(logdir)) config_path = os.path.join(logdir, 'config.yaml') yaml.dump(config, config_path, default_flow_style=False) return config
Example #18
Source File: pipelined_model.py From FATE with Apache License 2.0 | 6 votes |
def update_component_meta(self, component_name, component_module_name, model_alias, model_proto_index): """ update meta info yaml TODO: with lock :param component_name: :param component_module_name: :param model_alias: :param model_proto_index: :return: """ with open(self.define_meta_path, "r", encoding="utf-8") as fr: define_index = yaml.safe_load(fr) with open(self.define_meta_path, "w", encoding="utf-8") as fw: define_index["component_define"] = define_index.get("component_define", {}) define_index["component_define"][component_name] = define_index["component_define"].get(component_name, {}) define_index["component_define"][component_name].update({"module_name": component_module_name}) define_index["model_proto"] = define_index.get("model_proto", {}) define_index["model_proto"][component_name] = define_index["model_proto"].get(component_name, {}) define_index["model_proto"][component_name][model_alias] = define_index["model_proto"][component_name].get(model_alias, {}) define_index["model_proto"][component_name][model_alias].update(model_proto_index) yaml.dump(define_index, fw, Dumper=yaml.RoundTripDumper)
Example #19
Source File: administration.py From eNMS with GNU General Public License v3.0 | 5 votes |
def save_settings(self, **kwargs): self.settings = kwargs["settings"] if kwargs["save"]: with open(self.path / "setup" / "settings.json", "w") as file: dump(kwargs["settings"], file, indent=2)
Example #20
Source File: test_apply_manifest.py From tsrc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_repo_to_manifest(manifest_path: Path, dest: str, url: str) -> None: yaml = ruamel.yaml.YAML() data = yaml.load(manifest_path.read_text()) repos = data["repos"] to_add = {"dest": dest, "url": url} repos.append(to_add) with manifest_path.open("w") as fileobj: yaml.dump(data, fileobj)
Example #21
Source File: config.py From tsrc with BSD 3-Clause "New" or "Revised" License | 5 votes |
def save_to_file(self, cfg_path: Path) -> None: cfg_path.parent.makedirs_p() yaml = ruamel.yaml.YAML(typ="rt") yaml.register_class(Path) as_dict = attr.asdict(self) with cfg_path.open("w") as fp: yaml.dump(as_dict, fp)
Example #22
Source File: exceptions.py From strictyaml with MIT License | 5 votes |
def context_mark(self): context_line = self._chunk.start_line() - 1 str_document = dump(self._chunk.whole_document, Dumper=RoundTripDumper) context_index = len(u"\n".join(str_document.split(u"\n")[:context_line])) return StringMark( self._chunk.label, context_index, context_line, 0, str_document, context_index + 1, )
Example #23
Source File: config.py From InplusTrader_Linux with MIT License | 5 votes |
def dump_config(config_path, config, dumper=yaml.RoundTripDumper): with codecs.open(config_path, mode='w', encoding='utf-8') as file: file.write(yaml.dump(config, Dumper=dumper))
Example #24
Source File: administration.py From eNMS with GNU General Public License v3.0 | 5 votes |
def migration_export(self, **kwargs): for cls_name in kwargs["import_export_types"]: path = self.path / "files" / "migrations" / kwargs["name"] if not exists(path): makedirs(path) with open(path / f"{cls_name}.yaml", "w") as migration_file: yaml.dump(db.export(cls_name), migration_file)
Example #25
Source File: utility.py From batch-ppo with Apache License 2.0 | 5 votes |
def save_config(config, logdir=None): """Save a new configuration by name. If a logging directory is specified, is will be created and the configuration will be stored there. Otherwise, a log message will be printed. Args: config: Configuration object. logdir: Location for writing summaries and checkpoints if specified. Returns: Configuration object. """ if logdir: with config.unlocked: config.logdir = logdir message = 'Start a new run and write summaries and checkpoints to {}.' tf.logging.info(message.format(config.logdir)) tf.gfile.MakeDirs(config.logdir) config_path = os.path.join(config.logdir, 'config.yaml') with tf.gfile.FastGFile(config_path, 'w') as file_: yaml.dump(config, file_, default_flow_style=False) else: message = ( 'Start a new run without storing summaries and checkpoints since no ' 'logging directory was specified.') tf.logging.info(message) return config
Example #26
Source File: yamlutils.py From scriptcwl with Apache License 2.0 | 5 votes |
def yaml2string(wf, pack, relpath, wd): s = [u'#!/usr/bin/env cwl-runner', yaml.dump(wf.to_obj(pack=pack, relpath=relpath, wd=wd), Dumper=yaml.RoundTripDumper)] return u'\n'.join(s)
Example #27
Source File: engine.py From ansible-playbook-bundle with GNU General Public License v2.0 | 5 votes |
def cmdrun_serviceinstance(**kwargs): project = kwargs['base_path'] spec = get_spec(project) defaultValue = "ansibleplaybookbundle" params = {} plan_names = "(Plans->" first_plan = 0 for plan in spec['plans']: plan_names = "%s|%s" % (plan_names, plan['name']) # Only save the vars from the first plan if first_plan == 0: print("Only displaying vars from the '%s' plan." % plan['name']) for param in plan['parameters']: try: if param['required']: # Save a required param name and set a defaultValue params[param['name']] = defaultValue except Exception: pass first_plan += 1 plan_names = "%s)" % plan_names serviceInstance = dict(apiVersion="servicecatalog.k8s.io/v1beta1", kind="ServiceInstance", metadata=dict( name=spec['name'] ), spec=dict( clusterServiceClassExternalName="dh-" + spec['name'], clusterServicePlanExternalName=plan_names, parameters=params ) ) with open(spec['name'] + '.yaml', 'w') as outfile: yaml.dump(serviceInstance, outfile, default_flow_style=False)
Example #28
Source File: yolo_file.py From yolo with Apache License 2.0 | 5 votes |
def render(self, **variables): # Render variables into the yolo file. template = jinja2.Template( yaml.dump(self._raw_content, Dumper=yaml.RoundTripDumper) ) rendered_content = template.render(**variables) new_content = yaml.safe_load(rendered_content) return YoloFile(new_content)
Example #29
Source File: yolo_file.py From yolo with Apache License 2.0 | 5 votes |
def to_fileobj(self): """Dump this `YoloFile` contents to file-like object.""" fp = utils.StringIO() yaml.dump(self._raw_content, fp, encoding='utf-8', Dumper=yaml.RoundTripDumper) fp.seek(0) return fp
Example #30
Source File: attr_dict.py From planet with Apache License 2.0 | 5 votes |
def save(self, filename): assert str(filename).endswith('.yaml') directory = os.path.dirname(str(filename)) os.makedirs(directory, exist_ok=True) with open(filename, 'w') as f: yaml.dump(collections.OrderedDict(self), f)