Python yaml.safe_dump() Examples
The following are 30
code examples of yaml.safe_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
yaml
, or try the search function
.
Example #1
Source File: commands.py From drydock with Apache License 2.0 | 8 votes |
def task_builddata(ctx, task_id=None, output='yaml'): """Show builddata assoicated with ``task_id``.""" if not task_id: ctx.fail('The task id must be specified by --task-id') task_bd = TaskBuildData(ctx.obj['CLIENT'], task_id=task_id).invoke() if output == 'json': click.echo(json.dumps(task_bd)) else: if output != 'yaml': click.echo( 'Invalid output format {}, defaulting to YAML.'.format(output)) click.echo( yaml.safe_dump( task_bd, allow_unicode=True, default_flow_style=False))
Example #2
Source File: convert-lgpo-policy.py From ash-windows-formula with Apache License 2.0 | 7 votes |
def main(src_file, dst_file, **kwargs): policies = [] with open(src_file, mode='rb') as f: raw = f.read() encoding = chardet.detect(raw)['encoding'] src = raw.decode(encoding).splitlines() if '[Unicode]' in src: policies = _convert_secedit(src) else: policies = _convert_regpol(src) with open(dst_file, mode='w') as dh_: yaml.safe_dump(policies, dh_, default_flow_style=False)
Example #3
Source File: data_file.py From BiblioPixel with MIT License | 6 votes |
def dumps(data, use_yaml=None, safe=True, **kwds): """ Dumps data into a nicely formatted JSON string. :param dict data: a dictionary to dump :param kwds: keywords to pass to json.dumps :returns: a string with formatted data :rtype: str """ if use_yaml is None: use_yaml = ALWAYS_DUMP_YAML if use_yaml: dumps = yaml.safe_dump if safe else yaml.dump else: dumps = json.dumps kwds.update(indent=4, sort_keys=True) if not safe: kwds.update(default=repr) return dumps(data, **kwds)
Example #4
Source File: init_repo.py From release-bot with GNU General Public License v3.0 | 6 votes |
def create_yaml(_dict, file_name): """ Create or overwrite yaml file :param dict: dict to be converted to yaml :param filename: name of the yaml file to create """ def dump_yaml(): """ Dumps the yaml into the file """ with open(file_name, 'w') as yaml_file: yaml.safe_dump(_dict, yaml_file, default_flow_style=False) if not os.path.isfile(file_name): dump_yaml() else: should_overrite = input( file_name+" already exists, would you like to overwrite it? (y/N):" ) if should_overrite.lower() == 'y': dump_yaml()
Example #5
Source File: test_experiment.py From ludwig with Apache License 2.0 | 6 votes |
def test_experiment_seq_seq_model_def_file(csv_filename, yaml_filename): # seq-to-seq test to use model definition file instead of dictionary input_features = [text_feature(reduce_output=None, encoder='embed')] output_features = [ text_feature(reduce_input=None, vocab_size=3, decoder='tagger') ] # Save the model definition to a yaml file model_definition = { 'input_features': input_features, 'output_features': output_features, 'combiner': {'type': 'concat', 'fc_size': 14}, 'training': {'epochs': 2} } with open(yaml_filename, 'w') as yaml_out: yaml.safe_dump(model_definition, yaml_out) rel_path = generate_data(input_features, output_features, csv_filename) run_experiment( None, None, data_csv=rel_path, model_definition_file=yaml_filename )
Example #6
Source File: config_unit_test.py From aws-service-catalog-puppet with Apache License 2.0 | 6 votes |
def test_get_config_without_a_default_region(sut, mocker): # setup expected_result = { "Foo": "Bar", } mocked_get_home_region = mocker.patch.object(sut, 'get_home_region') fake_home_region = 'eu-west-9' mocked_get_home_region.return_value = fake_home_region mocked_response = { 'Parameter': { "Value": yaml.safe_dump(expected_result) } } mocked_betterboto_client = mocker.patch.object(sut.betterboto_client, 'ClientContextManager') mocked_betterboto_client().__enter__().get_parameter.return_value = mocked_response # exercise actual_result = sut.get_config() # verify assert actual_result == expected_result args, kwargs = mocked_betterboto_client.call_args assert 'ssm' == args[0] assert {'region_name': fake_home_region} == kwargs
Example #7
Source File: chute.py From Paradrop with Apache License 2.0 | 6 votes |
def initialize(ctx, legacy): """ Interactively create a paradrop.yaml file. """ if legacy: chute = build_legacy_chute() using = chute.get('use', None) else: chute = build_chute() using = chute['services']['main'].get('image', None) with open("paradrop.yaml", "w") as output: yaml.safe_dump(chute, output, default_flow_style=False) # If this is a node.js chute, generate a package.json file from the # information that the user provided. if using == 'node': if not os.path.isfile('package.json'): data = { 'name': chute['name'], 'version': '1.0.0', 'description': chute['description'] } with open('package.json', 'w') as output: json.dump(data, output, sort_keys=True, indent=2)
Example #8
Source File: config_unit_test.py From aws-service-catalog-puppet with Apache License 2.0 | 6 votes |
def test_get_config_with_a_default_region(sut, mocker): # setup expected_result = { "Foo": "Bar", } fake_home_region = 'eu-west-10' mocked_response = { 'Parameter': { "Value": yaml.safe_dump(expected_result) } } mocked_betterboto_client = mocker.patch.object(sut.betterboto_client, 'ClientContextManager') mocked_betterboto_client().__enter__().get_parameter.return_value = mocked_response # exercise actual_result = sut.get_config(default_region=fake_home_region) # verify assert actual_result == expected_result args, kwargs = mocked_betterboto_client.call_args assert 'ssm' == args[0] assert {'region_name': fake_home_region} == kwargs
Example #9
Source File: aws.py From aws-service-catalog-puppet with Apache License 2.0 | 6 votes |
def terminate_provisioned_product(prefix, service_catalog, provisioned_product_id): logger.info(f"{prefix} :: about to terminate {provisioned_product_id}") record_detail = service_catalog.terminate_provisioned_product( ProvisionedProductId=provisioned_product_id ).get('RecordDetail') record_id = record_detail.get('RecordId') logger.info(f"{prefix} :: waiting for termination of {provisioned_product_id} to complete") status = "IN_PROGRESS" while status == "IN_PROGRESS": record_detail = service_catalog.describe_record(Id=record_id).get('RecordDetail') status = record_detail.get('Status') logger.info(f"{prefix} :: termination of {provisioned_product_id} current status: {status}") if status != "IN_PROGRESS": break else: time.sleep(3) if status not in ['CREATED', 'SUCCEEDED']: logger.info(yaml.safe_dump(record_detail.get('RecordErrors'))) raise Exception(f"Failed to terminate provisioned product: Status = {status}")
Example #10
Source File: node.py From Paradrop with Apache License 2.0 | 6 votes |
def generate_configuration(ctx, format): """ Generate a new node configuration based on detected hardware. The new configuration is not automatically applied. Rather, you can save it to file and use the import-configuration command to apply it. """ client = ctx.obj['client'] result = client.generate_config() format = format.lower() if format == 'json': click.echo(json.dumps(result, indent=4)) elif format == 'yaml': click.echo(yaml.safe_dump(result, default_flow_style=False)) else: click.echo("Unrecognized format: {}".format(format)) return result
Example #11
Source File: core.py From aws-service-catalog-puppet with Apache License 2.0 | 6 votes |
def expand(f): click.echo('Expanding') puppet_account_id = config.get_puppet_account_id() manifest = manifest_utils.load(f, puppet_account_id) org_iam_role_arn = config.get_org_iam_role_arn() if org_iam_role_arn is None: click.echo('No org role set - not expanding') new_manifest = manifest else: click.echo('Expanding using role: {}'.format(org_iam_role_arn)) with betterboto_client.CrossAccountClientContextManager( 'organizations', org_iam_role_arn, 'org-iam-role' ) as client: new_manifest = manifest_utils.expand_manifest(manifest, client) click.echo('Expanded') new_name = f.name.replace(".yaml", '-expanded.yaml') logger.info('Writing new manifest: {}'.format(new_name)) with open(new_name, 'w') as output: output.write( yaml.safe_dump(new_manifest, default_flow_style=False) )
Example #12
Source File: config.py From yacs with Apache License 2.0 | 6 votes |
def dump(self, **kwargs): """Dump to a string.""" def convert_to_dict(cfg_node, key_list): if not isinstance(cfg_node, CfgNode): _assert_with_logging( _valid_type(cfg_node), "Key {} with value {} is not a valid type; valid types: {}".format( ".".join(key_list), type(cfg_node), _VALID_TYPES ), ) return cfg_node else: cfg_dict = dict(cfg_node) for k, v in cfg_dict.items(): cfg_dict[k] = convert_to_dict(v, key_list + [k]) return cfg_dict self_as_dict = convert_to_dict(self, []) return yaml.safe_dump(self_as_dict, **kwargs)
Example #13
Source File: test_catalog.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
def test_catalog_update_cache_yml(tmpdir): from ideascube.serveradmin.catalog import Catalog remote_catalog_file = tmpdir.mkdir('source').join('catalog.yml') remote_catalog_file.write(yaml.safe_dump({ 'all': {'foovideos': {'name': 'Videos from Foo'}}})) c = Catalog() assert c._available == {} assert c._installed == {} c.add_remote( 'foo', 'Content from Foo', 'file://{}'.format(remote_catalog_file.strpath)) c.update_cache() assert c._available == {'foovideos': {'name': 'Videos from Foo'}} assert c._installed == {} c = Catalog() assert c._available == {'foovideos': {'name': 'Videos from Foo'}} assert c._installed == {}
Example #14
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def toYAML(self, **options): """ Serializes this Munch to YAML, using `yaml.safe_dump()` if no `Dumper` is provided. See the PyYAML documentation for more info. >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42) >>> import yaml >>> yaml.safe_dump(b, default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> b.toYAML(default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> yaml.dump(b, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' >>> b.toYAML(Dumper=yaml.Dumper, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' """ opts = dict(indent=4, default_flow_style=False) opts.update(options) if 'Dumper' not in opts: return yaml.safe_dump(self, **opts) else: return yaml.dump(self, **opts)
Example #15
Source File: Tosca.py From im with GNU General Public License v3.0 | 6 votes |
def _remove_recipe_header(script_content): """ Removes the "hosts" and "connection" elements from the recipe to make it "RADL" compatible """ try: yamlo = yaml.safe_load(script_content) if not isinstance(yamlo, list): Tosca.logger.warn("Error parsing YAML: " + script_content + "\n.Do not remove header.") return script_content except Exception: Tosca.logger.exception("Error parsing YAML: " + script_content + "\n.Do not remove header.") return script_content for elem in yamlo: if 'hosts' in elem: del elem['hosts'] if 'connection' in elem: del elem['connection'] return yaml.safe_dump(yamlo, default_flow_style=False, explicit_start=True, width=256)
Example #16
Source File: ObjectStore.py From pcocc with GNU General Public License v3.0 | 6 votes |
def put_meta(self, name, revision, kind, data_blobs, custom_meta=None): self._validate_name(name) h = self._hash_meta(name, revision) target = self.get_obj_path('meta', h) meta = {} meta['name'] = name meta['revision'] = revision meta['kind'] = kind meta['owner'] = getpass.getuser() meta['timestamp'] = time.time() meta['data_blobs'] = data_blobs meta['custom_meta'] = custom_meta # Check that all the data blobs referred in the meta data are # already in the repo for b in data_blobs: self.get_obj_path('data', b, True) with open(target, 'w') as f: yaml.safe_dump(meta, f) return meta
Example #17
Source File: ObjectStore.py From pcocc with GNU General Public License v3.0 | 6 votes |
def _init_repodir(self): if os.path.isdir(self._path): self._validate_repo_config() return if os.path.exists(self._path): raise PcoccError("Repository path {0} is not a directory".format( self._path)) parent_dir = os.path.dirname(self._path) if not os.path.isdir(parent_dir): raise PcoccError("Invalid repository parent directory {0}".format( parent_dir)) try: os.mkdir(self._path) os.mkdir(self._data_path) os.mkdir(self._meta_path) os.mkdir(self._tmp_path) with open(self._config_path, 'w') as f: yaml.safe_dump({'version': 1}, f) except OSError as e: raise PcoccError('Unable to create repository directory {0}: {1}: '.format( self._path, str(e)))
Example #18
Source File: base.py From omniduct with MIT License | 6 votes |
def set_metadata(self, key, metadata, namespace=None, replace=False): """ Set the metadata associated with a stored key, creating the key if it is missing. Args: key (str): The key for which `value` should be stored. metadata (dict, None): Additional/override metadata to be stored for `key` in the cache. Values must be serializable via `yaml.safe_dump`. namespace (str, None): The namespace to be used. replace (bool): Whether the provided metadata should entirely replace any existing metadata, or just update it. (default=False) """ namespace, key = self._namespace(namespace), self._key(key) if replace: orig_metadata = {'created': datetime.datetime.utcnow()} else: orig_metadata = self.get_metadata(key, namespace=namespace) orig_metadata.update(metadata or {}) with self._get_stream_for_key(namespace, key, 'metadata', mode='w', create=True) as fh: yaml.safe_dump(orig_metadata, fh, default_flow_style=False)
Example #19
Source File: base.py From omniduct with MIT License | 6 votes |
def set(self, key, value, namespace=None, serializer=None, metadata=None): """ Set the value of a key. Args: key (str): The key for which `value` should be stored. value (object): The value to be stored. namespace (str, None): The namespace to be used. serializer (Serializer): The `Serializer` subclass to use for the serialisation of value into the cache. (default=PickleSerializer) metadata (dict, None): Additional metadata to be stored with the value in the cache. Values must be serializable via `yaml.safe_dump`. """ namespace, key = self._namespace(namespace), self._key(key) serializer = serializer or PickleSerializer() try: with self._get_stream_for_key(namespace, key, 'data{}'.format(serializer.file_extension), mode='wb', create=True) as fh: serializer.serialize(value, fh) self.set_metadata(key, metadata, namespace=namespace, replace=True) except: self.unset(key, namespace=namespace) six.reraise(*sys.exc_info())
Example #20
Source File: issue-diff.py From openSUSE-release-tools with GNU General Public License v2.0 | 6 votes |
def prompt_interactive(changes, project, package): with tempfile.NamedTemporaryFile(mode='w', suffix='.yml') as temp: temp.write(yaml.safe_dump(changes, default_flow_style=False, default_style="'") + '\n') temp.write('# {}/{}\n'.format(project, package)) temp.write('# comment or remove lines to whitelist issues') temp.flush() editor = os.getenv('EDITOR') if not editor: editor = 'xdg-open' subprocess.call(editor.split(' ') + [temp.name]) changes_after = yaml.safe_load(open(temp.name).read()) if changes_after is None: changes_after = {} return changes_after
Example #21
Source File: markdown.py From ipymd with BSD 3-Clause "New" or "Revised" License | 6 votes |
def meta(self, source, is_notebook=False): if source is None: return '' if source.get('ipymd', {}).get('empty_meta', None): return '---\n\n' if not source: if is_notebook: return '' return '---\n\n' meta = '{}\n'.format(yaml.safe_dump(source, explicit_start=True, explicit_end=True, default_flow_style=False)) if is_notebook: # Replace the trailing `...\n\n` meta = meta[:-5] + '---\n\n' return meta
Example #22
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def toYAML(self, **options): """ Serializes this Munch to YAML, using `yaml.safe_dump()` if no `Dumper` is provided. See the PyYAML documentation for more info. >>> b = Munch(foo=['bar', Munch(lol=True)], hello=42) >>> import yaml >>> yaml.safe_dump(b, default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> b.toYAML(default_flow_style=True) '{foo: [bar, {lol: true}], hello: 42}\\n' >>> yaml.dump(b, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' >>> b.toYAML(Dumper=yaml.Dumper, default_flow_style=True) '!munch.Munch {foo: [bar, !munch.Munch {lol: true}], hello: 42}\\n' """ opts = dict(indent=4, default_flow_style=False) opts.update(options) if 'Dumper' not in opts: return yaml.safe_dump(self, **opts) else: return yaml.dump(self, **opts)
Example #23
Source File: test_catalog.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
def test_catalog_existing_remote_yml(settings): from ideascube.serveradmin.catalog import Catalog params = { 'id': 'foo', 'name': 'Content provided by Foo', 'url': 'http://foo.fr/catalog.yml'} remotes_dir = Path(settings.CATALOG_STORAGE_ROOT).mkdir('remotes') yml_file = remotes_dir.join('foo.yml') yml_file.write(yaml.safe_dump(params)) c = Catalog() remotes = c.list_remotes() assert len(remotes) == 1 remote = remotes[0] assert remote.id == params['id'] assert remote.name == params['name'] assert remote.url == params['url'] assert yml_file.check(exists=False) json_data = json.loads(remotes_dir.join('foo.json').read()) assert json_data == params
Example #24
Source File: test_catalog.py From ideascube with GNU Affero General Public License v3.0 | 6 votes |
def test_catalog_remove_remote_yml(settings): from ideascube.serveradmin.catalog import Catalog params = { 'id': 'foo', 'name': 'Content provided by Foo', 'url': 'http://foo.fr/catalog.yml'} remotes_dir = Path(settings.CATALOG_STORAGE_ROOT).mkdir('remotes') yml_file = remotes_dir.join('foo.yml') yml_file.write(yaml.safe_dump(params)) c = Catalog() c.remove_remote(params['id']) remotes = c.list_remotes() assert len(remotes) == 0 assert yml_file.check(exists=False) assert remotes_dir.join('foo.json').check(exists=False) with pytest.raises(ValueError) as exc: c.remove_remote(params['id']) assert params['id'] in exc.exconly()
Example #25
Source File: CtxtAgentBase.py From im with GNU General Public License v3.0 | 5 votes |
def add_nat_gateway_tasks(playbook): """ Add tasks to enable NAT (Tested in GCE instances) https://cloud.google.com/vpc/docs/special-configurations """ play_dir = os.path.dirname(playbook) play_filename = os.path.basename(playbook) new_playbook = os.path.join(play_dir, "nat_" + play_filename) with open(playbook) as f: yaml_data = yaml.safe_load(f) task = {"raw": ("sudo sysctl -w net.ipv4.ip_forward=1; " "sudo iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; " "sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; " "sudo iptables -t nat -D POSTROUTING -o ens4 -j MASQUERADE; " "sudo iptables -t nat -A POSTROUTING -o ens4 -j MASQUERADE")} task["name"] = "Activate NAT Gateway" task["become"] = "yes" task["ignore_errors"] = "yes" yaml_data[0]['tasks'].append(task) with open(new_playbook, 'w+') as f: yaml.safe_dump(yaml_data, f) return new_playbook
Example #26
Source File: yaml_utils.py From rekall with GNU General Public License v2.0 | 5 votes |
def encode(raw_data): return utils.SmartStr(yaml.safe_dump(raw_data, default_flow_style=False))
Example #27
Source File: version.py From rekall with GNU General Public License v2.0 | 5 votes |
def update_version_files(args): data, version_path = get_config_file(args.version_file) version_data = data["version_data"] if args.version: version_data["version"] = args.version if args.post: version_data["post"] = args.post if args.rc: version_data["rc"] = args.rc if args.codename: version_data["codename"] = args.codename # Write the updated version_data into the file. with open(version_path, "wt") as fd: fd.write(yaml.safe_dump(data, default_flow_style=False)) # Should not happen but just in case... contents = _VERSION_TEMPLATE % ( escape_string(args.version_file), escape_string(json.dumps(version_data, indent=4))) + _VERSION_CODE # Now copy the static version files to all locations. for path in data["dependent_versions"]: current_dir = os.path.abspath(os.path.dirname( os.path.abspath(__file__))) version_path = os.path.abspath(os.path.join(current_dir, path)) if not os.path.relpath(version_path, current_dir): raise TypeError("Dependent version path is outside tree.") with open(version_path, "wt") as fd: fd.write(contents) update_templates(version_data)
Example #28
Source File: generate_tool_list_from_ga_workflow_files.py From GalaxyKickStart with GNU General Public License v3.0 | 5 votes |
def print_yaml_tool_list(tool_dictionary, output_file): with open(output_file, 'w') as F: F.write("\n".join([INSTALL_TOOL_DEPENDENCIES, INSTALL_REPOSITORY_DEPENDENCIES, INSTALL_RESOLVER_DEPENDENCIES, "", ""])) F.write(yaml.safe_dump(tool_dictionary, default_flow_style=False)) return
Example #29
Source File: version.py From pyaff4 with Apache License 2.0 | 5 votes |
def update_version_files(args): data, version_path = get_config_file(args.version_file) version_data = data["version_data"] if args.version: version_data["version"] = args.version if args.post: version_data["post"] = args.post if args.rc: version_data["rc"] = args.rc if args.codename: version_data["codename"] = args.codename # Write the updated version_data into the file. with open(version_path, "wt") as fd: fd.write(yaml.safe_dump(data, default_flow_style=False)) # Should not happen but just in case... contents = _VERSION_TEMPLATE % ( escape_string(args.version_file), escape_string(json.dumps(version_data, indent=4))) + _VERSION_CODE # Now copy the static version files to all locations. for path in data["dependent_versions"]: current_dir = os.path.abspath(os.path.dirname( os.path.abspath(__file__))) version_path = os.path.abspath(os.path.join(current_dir, path)) if not os.path.relpath(version_path, current_dir): raise TypeError("Dependent version path is outside tree.") with open(version_path, "wt") as fd: fd.write(contents) update_templates(version_data)
Example #30
Source File: repository_manager.py From rekall with GNU General Public License v2.0 | 5 votes |
def Encoder(self, data, **options): if options.get("raw"): return utils.SmartStr(data) # If the user specifically wants to encode in yaml, then do so. if options.get("yaml"): return yaml.safe_dump(data, default_flow_style=False) return utils.PPrint(data)