本文整理汇总了Python中ruamel.yaml.SafeLoader方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.SafeLoader方法的具体用法?Python yaml.SafeLoader怎么用?Python yaml.SafeLoader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ruamel.yaml
的用法示例。
在下文中一共展示了yaml.SafeLoader方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _ordered_load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def _ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
"""
Ordered yaml loader
Use this instead ot yaml.loader/yaml.saveloader to get an Ordereddict
:param stream: stream to read from
:param Loader: yaml-loader to use
:object_pairs_hook: ...
:return: OrderedDict structure
"""
# usage example: ordered_load(stream, yaml.SafeLoader)
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
return yaml.load(stream, OrderedLoader)
示例2: _from_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def _from_yaml(yaml_string=None, filename=None, encoding="utf-8", errors="strict", **kwargs):
if 'Loader' not in kwargs:
kwargs['Loader'] = yaml.SafeLoader
if filename:
_exists(filename)
with open(filename, 'r', encoding=encoding, errors=errors) as f:
data = yaml.load(f, **kwargs)
elif yaml_string:
data = yaml.load(yaml_string, **kwargs)
else:
raise BoxError('from_yaml requires a string or filename')
return data
示例3: test_to_yaml_basic
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def test_to_yaml_basic(self):
a = Box(test_dict)
assert yaml.load(a.to_yaml(), Loader=yaml.SafeLoader) == test_dict
示例4: test_to_yaml_file
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def test_to_yaml_file(self):
a = Box(test_dict)
a.to_yaml(tmp_yaml_file)
with open(tmp_yaml_file) as f:
data = yaml.load(f, Loader=yaml.SafeLoader)
assert data == test_dict
示例5: test_to_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def test_to_yaml(self):
m_file = os.path.join(tmp_dir, "movie_data")
movie_string = _to_yaml(movie_data)
assert "Rick Moranis" in movie_string
_to_yaml(movie_data, filename=m_file)
assert "Rick Moranis" in open(m_file).read()
assert yaml.load(open(m_file), Loader=yaml.SafeLoader) == yaml.load(movie_string, Loader=yaml.SafeLoader)
示例6: test_box_list_to_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def test_box_list_to_yaml(self):
bl = BoxList([{'item': 1, 'CamelBad': 2}])
assert yaml.load(bl.to_yaml(), Loader=yaml.SafeLoader)[0]['item'] == 1
示例7: yaml_load_fromstring
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def yaml_load_fromstring(string, ordered=False):
"""
Load contents of a string into an dict/OrderedDict structure. The string has to be valid yaml
:param string: name of the yaml file to load
:type string: str
:param ordered: load to an OrderedDict? Default=False
:type ordered: bool
:return: configuration data loaded from the file (or None if an error occured) and error string
:rtype: Dict|OrderedDict|None, str
"""
dict_type = 'dict'
if ordered:
dict_type = 'OrderedDict'
logger.info("Loading '{}' to '{}'".format(string, dict_type))
y = None
estr = ''
try:
sdata = string
# sdata = sdata.replace('\n', '\n\n')
if ordered:
y = _ordered_load(sdata, yaml.SafeLoader)
else:
y = yaml.load(sdata, yaml.SafeLoader)
except Exception as e:
estr = str(e)
if "found character '\\t'" in estr:
estr = estr[estr.find('line'):]
estr = 'TABs are not allowed in YAML files, use spaces for indentation instead!\nError in ' + estr
if ("while scanning a simple key" in estr) and ("could not found expected ':'" in estr):
estr = estr[estr.find('column'):estr.find('could not')]
estr = 'The colon (:) following a key has to be followed by a space. The space is missing!\nError in ' + estr
return y, estr
示例8: from_yaml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def from_yaml(cls, yaml_string=None, filename=None,
encoding="utf-8", errors="strict",
loader=yaml.SafeLoader, **kwargs):
"""
Transform a yaml object string into a Box object.
:param yaml_string: string to pass to `yaml.load`
:param filename: filename to open and pass to `yaml.load`
:param encoding: File encoding
:param errors: How to handle encoding errors
:param loader: YAML Loader, defaults to SafeLoader
:param kwargs: parameters to pass to `Box()` or `yaml.load`
:return: Box object from yaml data
"""
bx_args = {}
for arg in kwargs.copy():
if arg in BOX_PARAMETERS:
bx_args[arg] = kwargs.pop(arg)
data = _from_yaml(yaml_string=yaml_string, filename=filename,
encoding=encoding, errors=errors,
Loader=loader, **kwargs)
if not isinstance(data, dict):
raise BoxError('yaml data not returned as a dictionary'
'but rather a {0}'.format(type(data).__name__))
return cls(data, **bx_args)
示例9: load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def load(stream, Loader=yaml.SafeLoader, object_pairs_hook=OrderedDict):
class OrderedLoader(Loader):
pass
def construct_mapping(loader, node):
loader.flatten_mapping(node)
return object_pairs_hook(loader.construct_pairs(node))
OrderedLoader.add_constructor(
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
construct_mapping)
return yaml.load(stream, OrderedLoader)
示例10: save
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def save(data, stream=None, Dumper=yaml.SafeDumper,
default_flow_style=False,
encoding='utf-8',
**kwds):
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)
OrderedDumper.add_representer(np.float64,
lambda dumper, data: dumper.represent_float(float(data)))
OrderedDumper.add_representer(complex,
lambda dumper, data: dumper.represent_str(str(data)))
OrderedDumper.add_representer(np.complex128,
lambda dumper, data: dumper.represent_str(str(data)))
OrderedDumper.add_representer(np.ndarray,
lambda dumper, data: dumper.represent_list(list(data)))
# I added the following two lines to make pyrpl compatible with pyinstruments. In principle they can be erased
if isinstance(data, dict) and not isinstance(data, OrderedDict):
data = OrderedDict(data)
return yaml.dump(data,
stream=stream,
Dumper=OrderedDumper,
default_flow_style=default_flow_style,
encoding=encoding,
**kwds)
# usage example:
# load(stream, yaml.SafeLoader)
# save(data, stream=f, Dumper=yaml.SafeDumper)
示例11: yaml_load
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def yaml_load(filename, ordered=False, ignore_notfound=False):
"""
Load contents of a configuration file into an dict/OrderedDict structure. The configuration file has to be a valid yaml file
:param filename: name of the yaml file to load
:type filename: str
:param ordered: load to an OrderedDict? Default=False
:type ordered: bool
:return: configuration data loaded from the file (or None if an error occured)
:rtype: Dict | OrderedDict | None
"""
dict_type = 'dict'
if ordered:
dict_type = 'OrderedDict'
logger.info("Loading '{}' to '{}'".format(filename, dict_type))
y = None
try:
with open(filename, 'r') as stream:
sdata = stream.read()
sdata = sdata.replace('\n', '\n\n')
if ordered:
y = _ordered_load(sdata, yaml.SafeLoader)
else:
y = yaml.load(sdata, yaml.SafeLoader)
except Exception as e:
estr = str(e)
if "found character '\\t'" in estr:
estr = estr[estr.find('line'):]
estr = 'TABs are not allowed in YAML files, use spaces for indentation instead!\nError in ' + estr
if ("while scanning a simple key" in estr) and ("could not found expected ':'" in estr):
estr = estr[estr.find('column'):estr.find('could not')]
estr = 'The colon (:) following a key has to be followed by a space. The space is missing!\nError in ' + estr
if '(line: ' in estr:
line = convert_linenumber(estr)
line = convert_linenumber(line, 2)
# estr += '\nNOTE: To find correct line numbers: add 1 to line and divide by 2 -> '+line
estr = line
estr += '\nNOTE: Look for the error at the expected <block end>, near the second specified line number'
if "[Errno 2]" in estr:
if not ignore_notfound:
logger.warning("YAML-file not found: {}".format(filename))
else:
logger.error("YAML-file load error in {}: \n{}".format(filename, estr))
return y
示例12: run
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import SafeLoader [as 别名]
def run(configfile, unsafe: bool, loop: Optional[str], service: Optional[str]):
# Read the configuration from the supplied YAML files
config = {} # type: Dict[str, Any]
for path in configfile:
config_data = yaml.load(path, Loader=Loader if unsafe else SafeLoader)
assert isinstance(config_data, dict), 'the document root element must be a dictionary'
config = merge_config(config, config_data)
# Override the event loop policy if specified
if loop:
config['event_loop_policy'] = loop
services = config.pop('services', {})
if not isinstance(services, dict):
raise click.ClickException('The "services" key must be a dict, not {}'.format(
qualified_name(services)))
# If "component" was defined, use that as the default service if one has not been defined yet
if 'component' in config:
component = config.pop('component')
services.setdefault('default', dict(component=component))
# Try to figure out which service to launch
service = service or os.getenv('ASPHALT_SERVICE')
if len(services) == 0:
raise click.ClickException('No services have been defined')
elif service:
try:
service_config = services[service]
except KeyError:
raise click.ClickException(
'Service {!r} has not been defined'.format(service)) from None
elif len(services) == 1:
service_config = next(iter(services.values()))
elif 'default' in services:
service_config = services['default']
else:
raise click.ClickException(
'Multiple services present in configuration file but no default service has been '
'defined and no service was explicitly selected with -s / --service')
# Merge the service-level configuration with the top level one
config = merge_config(config, service_config)
# Start the application
run_application(**config)