本文整理汇总了Python中yaml.add_constructor方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.add_constructor方法的具体用法?Python yaml.add_constructor怎么用?Python yaml.add_constructor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yaml
的用法示例。
在下文中一共展示了yaml.add_constructor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: demo
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def demo():
constructor = CustodiaSimpleConstructor(
'http+unix://%2E%2Fserver_socket/secrets'
)
constructor.client.headers['REMOTE_USER'] = 'user'
# create entries
try:
c = constructor.client.list_container('test')
except HTTPError:
constructor.client.create_container('test')
c = []
if 'key' not in c:
constructor.client.set_secret('test/key', 'secret password')
yaml.add_constructor(CustodiaSimpleConstructor.yaml_tag,
constructor)
yaml_str = 'password: !custodia/simple test/key'
print(yaml_str)
result = yaml.load(yaml_str)
print(result)
示例2: get_config
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def get_config(dir='config/config.yaml'):
# add direction join function when parse the yaml file
def join(loader, node):
seq = loader.construct_sequence(node)
return os.path.sep.join(seq)
# add string concatenation function when parse the yaml file
def concat(loader, node):
seq = loader.construct_sequence(node)
seq = [str(tmp) for tmp in seq]
return ''.join(seq)
yaml.add_constructor('!join', join)
yaml.add_constructor('!concat', concat)
with open(dir, 'r') as f:
cfg = yaml.load(f)
check_dirs(cfg)
return cfg
示例3: get_config
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def get_config(dir):
# add direction join function when parse the yaml file
def join(loader, node):
seq = loader.construct_sequence(node)
return os.path.sep.join(seq)
# add string concatenation function when parse the yaml file
def concat(loader, node):
seq = loader.construct_sequence(node)
return ''.join(seq)
yaml.add_constructor('!join', join)
yaml.add_constructor('!concat', concat)
with open(dir, 'r') as f:
cfg = yaml.load(f)
return cfg
示例4: _config_loader
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def _config_loader():
envvar_matcher = re.compile(r"\${([^}^{]+)\}")
def envvar_constructor(_loader, node): # pragma: no cover
"""Extract the matched value, expand env variable, and replace the match."""
node_value = node.value
match = envvar_matcher.match(node_value)
env_var = match.group()[2:-1]
# check for defaults
var_name, default_value = env_var.split(":")
var_name = var_name.strip()
default_value = default_value.strip()
var_value = os.getenv(var_name, default_value)
return var_value + node_value[match.end() :]
yaml.add_implicit_resolver("!envvar", envvar_matcher, None, SafeLoader)
yaml.add_constructor("!envvar", envvar_constructor, SafeLoader)
# TODO: instead of this, create custom loader and use it
# by wrapping yaml.safe_load to use it
示例5: initialize
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def initialize():
"""
Initialize the configuration system by installing YAML handlers.
Automatically done on first call to load() specified in this file.
"""
global is_initialized
# Add the custom multi-constructor
yaml.add_multi_constructor('!obj:', multi_constructor_obj)
yaml.add_multi_constructor('!pkl:', multi_constructor_pkl)
yaml.add_multi_constructor('!import:', multi_constructor_import)
yaml.add_constructor('!import', constructor_import)
yaml.add_constructor("!float", constructor_float)
pattern = re.compile(SCIENTIFIC_NOTATION_REGEXP)
yaml.add_implicit_resolver('!float', pattern)
is_initialized = True
###############################################################################
# Callbacks used by PyYAML
示例6: setup_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def setup_yaml():
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
yaml.add_representer(collections.OrderedDict, dict_representer)
yaml.add_representer(os._Environ, dict_representer)
yaml.add_representer(unicode, unicode_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)
示例7: testIfTableIsAMap
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def testIfTableIsAMap(self):
from yaml.constructor import ConstructorError
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
# Credit https://gist.github.com/pypt/94d747fe5180851196eb
def no_duplicates_constructor(loader, node, deep=False):
"""Check for duplicate keys."""
mapping = {}
for key_node, value_node in node.value:
key = loader.construct_object(key_node, deep=deep)
value = loader.construct_object(value_node, deep=deep)
if key in mapping:
raise ConstructorError(
"while constructing a mapping", node.start_mark,
"found duplicate key (%s)" % key, key_node.start_mark)
mapping[key] = value
return loader.construct_mapping(node, deep)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor)
file_path = '../translationtable/GLOBAL_TERMS.yaml'
if os.path.exists(os.path.join(os.path.dirname(__file__), file_path)):
tt_file = open(os.path.join(os.path.dirname(__file__), file_path), 'r')
try:
translation_table = yaml.safe_load(tt_file)
except yaml.constructor.ConstructorError as e:
tt_file.close()
self.assertTrue(False)
print(e)
tt_file.close()
示例8: load
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def load(s, encoding='utf-8'):
# override to set output encoding
def construct_yaml_str(loader, node):
value = loader.construct_scalar(node)
if encoding is not None:
return value.encode(encoding)
return value
yaml.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
return yaml.load(s)
示例9: LoadConfigDict
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def LoadConfigDict(config_paths, model_params):
"""Loads config dictionary from specified yaml files or command line yaml."""
# Ensure that no duplicate keys can be loaded (causing pain).
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
NoDuplicatesConstructor)
# Handle either ',' or '#' separated config lists, since borg will only
# accept '#'.
sep = ',' if ',' in config_paths else '#'
# Load flags from config file.
final_config = {}
if config_paths:
for config_path in config_paths.split(sep):
config_path = config_path.strip()
if not config_path:
continue
config_path = os.path.abspath(config_path)
tf.logging.info('Loading config from %s', config_path)
with tf.gfile.GFile(config_path.strip()) as config_file:
config_flags = yaml.load(config_file)
final_config = DeepMergeDict(final_config, config_flags)
if model_params:
model_params = MaybeLoadYaml(model_params)
final_config = DeepMergeDict(final_config, model_params)
tf.logging.info('Final Config:\n%s', yaml.dump(final_config))
return final_config
示例10: read
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def read(self, filepath):
stream = False
# Setup environment variables replacement
def env_var_single_replace(match):
return os.environ[match.group(1)] if match.group(1) in os.environ else match.group()
def env_var_multi_replacer(loader, node):
value = loader.construct_scalar(node)
return re.sub(ENV_VAR_REPLACER_PATTERN, env_var_single_replace, value)
yaml.add_implicit_resolver("!envvarreplacer", ENV_VAR_MATCHER_PATTERN)
yaml.add_constructor('!envvarreplacer', env_var_multi_replacer)
# Read file
try:
stream = open(filepath, "r")
self.config = yaml.load(stream)
# Handle an empty configuration file
if not self.config:
self.config = {}
finally:
if stream:
stream.close()
示例11: register_tag
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def register_tag(tag, classpath):
yaml.add_constructor(u'!'+tag, metaloader(classpath))
yaml.add_constructor(u'tag:nltk.org,2011:'+tag,
metaloader(classpath))
示例12: register
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def register(cls):
yaml.add_constructor(cls.tag, cls.constructor)
yaml.add_representer(cls, cls.representer)
示例13: constructor
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def constructor(tag=None, pattern=None):
"""Register custom constructor with pyyaml."""
def decorator(f):
if tag is None or f is tag:
tag_ = '!{}'.format(f.__name__)
else:
tag_ = tag
yaml.add_constructor(tag_, f)
if pattern is not None:
yaml.add_implicit_resolver(tag_, re.compile(pattern))
return f
if callable(tag): # little convenience hack to avoid empty arg list
return decorator(tag)
return decorator
示例14: setup_yaml
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def setup_yaml():
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
yaml.SafeDumper.add_representer(collections.OrderedDict, dict_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)
示例15: load_ordered_config
# 需要导入模块: import yaml [as 别名]
# 或者: from yaml import add_constructor [as 别名]
def load_ordered_config(config_path):
"""
Loads the configuration in the same order as it's defined in yaml file,
so that, while saving it in new format, order is maintained
Args:
config_path (str): Path to the configuration file
Returns:
config(dict): Returns the configurations in the defined ordered
"""
# To load data from yaml in ordered dict format
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
def dict_representer(dumper, data):
return dumper.represent_mapping(_mapping_tag, iteritems(data))
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
yaml.add_representer(collections.OrderedDict, dict_representer)
yaml.add_constructor(_mapping_tag, dict_constructor)
# format the output to print a blank scalar rather than null
def represent_none(self, _):
return self.represent_scalar('tag:yaml.org,2002:null', u'')
yaml.add_representer(type(None), represent_none)
# read input from home directory for pull requests
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
return config