本文整理汇总了Python中yaml.CSafeDumper方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.CSafeDumper方法的具体用法?Python yaml.CSafeDumper怎么用?Python yaml.CSafeDumper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.CSafeDumper方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: yaml_fwrite
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def yaml_fwrite(filepath, content, preamble=""):
with open(filepath, "w") as f:
f.write(preamble)
f.write(yaml.dump(content, Dumper=YamlDumper))
示例2: __write_tpm_data
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [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)
示例3: write_private
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [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)
示例4: export
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def export(self, metadata, **kwargs):
"""
Export metadata as YAML. This uses yaml.SafeDumper by default.
"""
kwargs.setdefault("Dumper", SafeDumper)
kwargs.setdefault("default_flow_style", False)
kwargs.setdefault("allow_unicode", True)
metadata = yaml.dump(metadata, **kwargs).strip()
return u(metadata) # ensure unicode
示例5: write_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def write_yaml(root, file_name, data, overwrite=False):
"""
Write dictionary data in yaml format.
:param root: Directory name.
:param file_name: Desired file name. Will automatically add .yaml extension if not given
:param data: data to be dumped as yaml format
:param overwrite: If True, will overwrite existing files
"""
if not exists(root):
raise MissingConfigException("Parent directory '%s' does not exist." % root)
file_path = os.path.join(root, file_name)
yaml_file_name = file_path if file_path.endswith(".yaml") else file_path + ".yaml"
if exists(yaml_file_name) and not overwrite:
raise Exception("Yaml file '%s' exists as '%s" % (file_path, yaml_file_name))
try:
with codecs.open(yaml_file_name, mode='w', encoding=ENCODING) as yaml_file:
yaml.dump(data, yaml_file,
default_flow_style=False,
allow_unicode=True,
Dumper=YamlSafeDumper)
except Exception as e:
raise e
示例6: dump
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def dump(*args, **kwargs):
"""Delegate to yaml dumps.
"""
if kwargs is None:
kwargs = {}
kwargs['Dumper'] = Dumper
return yaml.dump(*args, **kwargs)
示例7: test_is_c_dumper
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def test_is_c_dumper(self):
dumper = storage._SimpleDumper(io.StringIO(''))
if getattr(yaml, 'CSafeDumper', None) is not None:
self.assertIsInstance(dumper, yaml.CSafeDumper)
else:
self.assertIsInstance(dumper, yaml.SafeDumper)
示例8: dump
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def dump(data, stream=None, **kwds):
"""
Serialize a Python object into a YAML stream.
If stream is None, return the produced string instead.
Dict keys are produced in the order in which they appear in OrderedDicts.
Safe version.
If objects are not "conventional" objects, they will be dumped converted to string with the str() function.
They will then not be recovered when loading with the load() function.
"""
# Display OrderedDicts correctly
class OrderedDumper(SafeDumper):
pass
def _dict_representer(dumper, data):
return dumper.represent_mapping(
original_yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
list(data.items()))
# Display long strings correctly
def _long_str_representer(dumper, data):
if data.find("\n") != -1:
# Drop some unneeded data
# \t are forbidden in YAML
data = data.replace("\t", " ")
# empty spaces at end of line are always useless in INGInious, and forbidden in YAML
data = "\n".join([p.rstrip() for p in data.split("\n")])
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
else:
return dumper.represent_scalar('tag:yaml.org,2002:str', data)
# Default representation for some odd objects
def _default_representer(dumper, data):
return _long_str_representer(dumper, str(data))
OrderedDumper.add_representer(str, _long_str_representer)
OrderedDumper.add_representer(str, _long_str_representer)
OrderedDumper.add_representer(OrderedDict, _dict_representer)
OrderedDumper.add_representer(None, _default_representer)
s = original_yaml.dump(data, stream, OrderedDumper, encoding='utf-8', allow_unicode=True, default_flow_style=False, indent=4, **kwds)
if s is not None:
return s.decode('utf-8')
else:
return
示例9: _save_month_to_disk
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def _save_month_to_disk(month, journal_dir):
"""
When overwriting 2014-12.txt:
write new content to 2014-12.new.txt
cp 2014-12.txt 2014-12.old.txt
mv 2014-12.new.txt 2014-12.txt
rm 2014-12.old.txt
"""
content = {}
for day_number, day in month.days.items():
if not day.empty:
content[day_number] = day.content
def get_filename(infix):
year_and_month = format_year_and_month(month.year_number, month.month_number)
return os.path.join(journal_dir, "{}{}.txt".format(year_and_month, infix))
old = get_filename(".old")
new = get_filename(".new")
filename = get_filename("")
# Do not save empty month files.
if not content and not os.path.exists(filename):
return False
with codecs.open(new, "wb", encoding="utf-8") as f:
# Write readable unicode and no Python directives.
yaml.dump(content, f, Dumper=Dumper, allow_unicode=True)
if os.path.exists(filename):
mtime = os.path.getmtime(filename)
if mtime != month.mtime:
conflict = get_filename(".CONFLICT_BACKUP" + str(mtime))
logging.debug(
"Last edit time of %s conflicts with edit time at file load\n"
"--> Backing up to %s" % (filename, conflict)
)
shutil.copy2(filename, conflict)
shutil.copy2(filename, old)
shutil.move(new, filename)
if os.path.exists(old):
os.remove(old)
try:
# Make file readable and writable only by the owner.
os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR)
except OSError:
pass
month.edited = False
month.mtime = os.path.getmtime(filename)
logging.info("Wrote file %s" % filename)
return True
示例10: safe_yaml_dump_all
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import CSafeDumper [as 别名]
def safe_yaml_dump_all(
documents,
stream=None,
Dumper=yDumper,
default_style=None,
default_flow_style=None,
canonical=None,
indent=None,
width=None,
allow_unicode=None,
line_break=None,
encoding='utf-8',
explicit_start=None,
explicit_end=None,
version=None,
tags=None,
):
if Dumper != yDumper:
stream_name = get_stream_name(stream)
log.debug("Unsafe dumping of YAML has been disabled - using safe dumper instead in %s", stream_name)
if pyyaml_dump_all:
return pyyaml_dump_all(
documents,
stream,
yDumper,
default_style,
default_flow_style,
canonical,
indent,
width,
allow_unicode,
line_break,
encoding,
explicit_start,
explicit_end,
version,
tags,
)
return yaml.dump_all(
documents,
stream,
yDumper,
default_style,
default_flow_style,
canonical,
indent,
width,
allow_unicode,
line_break,
encoding,
explicit_start,
explicit_end,
version,
tags,
)