本文整理汇总了Python中yaml.SafeDumper方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.SafeDumper方法的具体用法?Python yaml.SafeDumper怎么用?Python yaml.SafeDumper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.SafeDumper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
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
示例2: _ordered_dump
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
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)
示例3: write_policy_file
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def write_policy_file(self, policy, format="yaml"):
""" Write a policy file to disk in the specified format.
Input a dictionary and a format. Valid formats are `yaml` and `json`
Returns the file path.
"""
fh = tempfile.NamedTemporaryFile(mode="w+b", suffix="." + format, delete=False)
if format == "json":
fh.write(json.dumps(policy).encode("utf8"))
else:
fh.write(yaml.dump(policy, encoding="utf8", Dumper=yaml.SafeDumper))
fh.flush()
self.addCleanup(os.unlink, fh.name)
self.addCleanup(fh.close)
return fh.name
示例4: yaml_dump
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def yaml_dump(data, stream: TextIO) -> None:
"""
Dump data to a yaml file in an ordered way.
:param data: the data to be dumped
:param stream: the file pointer
"""
def ordered_dump(data, stream=None, **kwds):
class OrderedDumper(yaml.SafeDumper):
"""A wrapper for safe yaml loader."""
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) # nosec
ordered_dump(data, stream)
示例5: roles
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def roles():
"""
Remove the roles from the cluster/*.sls files
"""
# Keep yaml human readable/editable
friendly_dumper = yaml.SafeDumper
friendly_dumper.ignore_aliases = lambda self, data: True
cluster_dir = '/srv/pillar/ceph/cluster'
for filename in os.listdir(cluster_dir):
pathname = "{}/{}".format(cluster_dir, filename)
content = None
with open(pathname, "r") as sls_file:
content = yaml.safe_load(sls_file)
log.info("content {}".format(content))
if 'roles' in content:
content.pop('roles')
with open(pathname, "w") as sls_file:
sls_file.write(yaml.dump(content, Dumper=friendly_dumper,
default_flow_style=False))
示例6: default
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def default():
"""
Remove the .../stack/defaults directory. Preserve available_roles
"""
# Keep yaml human readable/editable
friendly_dumper = yaml.SafeDumper
friendly_dumper.ignore_aliases = lambda self, data: True
preserve = {}
content = None
pathname = "/srv/pillar/ceph/stack/default/{}/cluster.yml".format('ceph')
with open(pathname, "r") as sls_file:
content = yaml.safe_load(sls_file)
preserve['available_roles'] = content['available_roles']
stack_default = "/srv/pillar/ceph/stack/default"
shutil.rmtree(stack_default)
os.makedirs("{}/{}".format(stack_default, 'ceph'))
with open(pathname, "w") as sls_file:
sls_file.write(yaml.dump(preserve, Dumper=friendly_dumper,
default_flow_style=False))
uid = pwd.getpwnam("salt").pw_uid
gid = grp.getgrnam("salt").gr_gid
for path in [stack_default, "{}/{}".format(stack_default, 'ceph'), pathname]:
os.chown(path, uid, gid)
示例7: yamlDump
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def yamlDump(s):
class YamlOrderedDumper(yaml.SafeDumper):
pass
def represent_ordereddict(dumper, data):
rep = []
for k,v in data.items():
k = dumper.represent_data(k)
v = dumper.represent_data(v)
rep.append((k, v))
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', rep)
YamlOrderedDumper.add_representer(OrderedDict, represent_ordereddict)
return yaml.dump(s, Dumper=YamlOrderedDumper)
示例8: yaml_fwrite
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def yaml_fwrite(filepath, content, preamble=""):
with open(filepath, "w") as f:
f.write(preamble)
f.write(yaml.dump(content, Dumper=YamlDumper))
示例9: __init__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def __init__(self, *args, **kwargs):
yaml.SafeDumper.__init__(self, *args, **kwargs)
self.add_representer(OrderedDict, type(self).represent_ordereddict)
示例10: __write_tpm_data
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def __write_tpm_data(self):
os.umask(0o077)
if os.geteuid() != 0 and common.REQUIRE_ROOT:
logger.warning("Creating tpm metadata file without root. Sensitive trust roots may be at risk!")
with open('tpmdata.yml', 'w') as f:
yaml.dump(self.global_tpmdata, f, Dumper=SafeDumper)
示例11: write_private
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def write_private(inp):
priv = inp[0]
salt = inp[1]
global global_password
priv_encoded = yaml.dump(priv, Dumper=SafeDumper)
key = crypto.kdf(global_password,salt)
ciphertext = crypto.encrypt(priv_encoded,key)
towrite = {'salt':salt,'priv':ciphertext}
with os.fdopen(os.open('private.yml',os.O_WRONLY | os.O_CREAT,0o600), 'w') as f:
yaml.dump(towrite,f, Dumper=SafeDumper)
示例12: write_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def write_yaml(data, yml_file):
Dumper = yaml.SafeDumper
Dumper.ignore_aliases = lambda self, data: True
yaml.add_representer(OrderedDict, represent_ordereddict, Dumper=Dumper)
return yaml.dump(data, yml_file, default_flow_style=False, Dumper=Dumper)
示例13: main
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def main():
NoDatesSafeLoader.remove_implicit_resolver('tag:yaml.org,2002:timestamp')
yaml.SafeDumper.orig_represent_str = yaml.SafeDumper.represent_str
yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper)
fname = sys.argv[1]
edit(fname)
示例14: __init__
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def __init__(self, *args, **kwargs):
yaml.SafeDumper.__init__(self, *args, **kwargs)
yaml.representer.SafeRepresenter.add_representer(OrderedDict, type(self)._odyrepr)
示例15: dump_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import SafeDumper [as 别名]
def dump_yaml(obj: Any, **kwargs) -> str:
global yaml_logged_dumper
if not yaml_logged_dumper:
yaml_logged_dumper = True
# logger.info("YAML: using %s dumper" % ("Python" if (yaml_dumper == yaml.SafeDumper) else "C"))
return yaml.dump(obj, Dumper=yaml_dumper, **kwargs)