本文整理汇总了Python中ruamel.yaml.RoundTripLoader方法的典型用法代码示例。如果您正苦于以下问题:Python yaml.RoundTripLoader方法的具体用法?Python yaml.RoundTripLoader怎么用?Python yaml.RoundTripLoader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ruamel.yaml
的用法示例。
在下文中一共展示了yaml.RoundTripLoader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def read_config(path='aetros.yml', logger=None, return_default=True):
path = os.path.normpath(os.path.abspath(os.path.expanduser(path)))
config = {}
if os.path.exists(path):
f = open(path, 'r')
config = yaml.load(f, Loader=yaml.RoundTripLoader)
if config is None:
config = {}
if 'storage_dir' in config:
del config['storage_dir']
if 'model' in config and config['model']:
config['root'] = os.path.dirname(path)
logger and logger.debug('Config loaded from ' + os.path.realpath(path))
if return_default:
return apply_config_defaults(config)
return config
示例2: create
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def create(self, path, value):
''' create a yaml file '''
if not self.file_exists():
# deepcopy didn't work
# Try to use ruamel.yaml and fallback to pyyaml
try:
tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict,
default_flow_style=False),
yaml.RoundTripLoader)
except AttributeError:
tmp_copy = copy.deepcopy(self.yaml_dict)
# set the format attributes if available
try:
tmp_copy.fa.set_block_style()
except AttributeError:
pass
result = Yedit.add_entry(tmp_copy, path, value, self.separator)
if result is not None:
self.yaml_dict = tmp_copy
return (True, self.yaml_dict)
return (False, self.yaml_dict)
示例3: get_resource_file
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def get_resource_file(sfile, sfile_type='yaml'):
''' return the service file '''
contents = None
with open(sfile) as sfd:
contents = sfd.read()
if sfile_type == 'yaml':
# AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripLoader'):
contents = yaml.load(contents, yaml.RoundTripLoader)
else:
contents = yaml.safe_load(contents)
elif sfile_type == 'json':
contents = json.loads(contents)
return contents
示例4: yaml_save
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def yaml_save(filename, data):
"""
***Converter Special ***
Save contents of an OrderedDict structure to a yaml file
:param filename: name of the yaml file to save to
:param data: OrderedDict to save
"""
sdata = convert_yaml(data)
print(", saving to '{}'".format(os.path.basename(filename)+'.yaml'))
if store_raw_output == True:
with open(filename+'_raw.yaml', 'w') as outfile:
outfile.write( sdata )
# Test if roundtrip gives the same result
data = yaml.load(sdata, yaml.RoundTripLoader)
_yaml_save_roundtrip(filename, data)
示例5: yaml_load_roundtrip
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def yaml_load_roundtrip(filename):
"""
Load contents of a yaml file into an dict structure for editing (using Roundtrip Loader)
:param filename: name of the yaml file to load
:return: data structure loaded from file
"""
if not EDITING_ENABLED:
return None
y = None
if not filename.lower().endswith('.yaml'):
filename += YAML_FILE
try:
with open(filename, 'r') as stream:
sdata = stream.read()
sdata = sdata.replace('\n', '\n\n')
y = yaml.load(sdata, yaml.RoundTripLoader)
except Exception as e:
logger.error("yaml_load_roundtrip: YAML-file load error: '%s'" % (e))
y = {}
return y
示例6: read_parameter
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def read_parameter(parameter_file, parameter):
fr = open(parameter_file, "r")
param = yaml.load(fr, yaml.RoundTripLoader)
return merge_two_dicts(parameter,param)
示例7: uninstall
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def uninstall(params):
"""
Uninstall third-party Mod
"""
from pip import main as pip_main
from pip.commands.uninstall import UninstallCommand
params = [param for param in params]
options, mod_list = UninstallCommand().parse_args(params)
params = ["uninstall"] + params
for mod_name in mod_list:
mod_name_index = params.index(mod_name)
if mod_name in system_mod:
print('System Mod can not be installed or uninstalled')
return
if "rqalpha_mod_" in mod_name:
lib_name = mod_name
mod_name = lib_name.replace("rqalpha_mod_", "")
else:
lib_name = "rqalpha_mod_" + mod_name
params[mod_name_index] = lib_name
# Uninstall Mod
pip_main(params)
# Remove Mod Config
config_path = get_default_config_path()
config = load_config(config_path, loader=yaml.RoundTripLoader)
for mod_name in mod_list:
if "rqalpha_mod_" in mod_name:
mod_name = mod_name.replace("rqalpha_mod_", "")
del config['mod'][mod_name]
dump_config(config_path, config)
示例8: list
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def list():
"""
List all mod configuration
"""
config_path = get_default_config_path()
config = load_config(config_path, loader=yaml.RoundTripLoader)
print(yaml.dump(config['mod'], Dumper=yaml.RoundTripDumper))
示例9: enable
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def enable(mod_name):
"""
enable mod
"""
if mod_name not in system_mod and "rqalpha_mod_" in mod_name:
mod_name = mod_name.replace("rqalpha_mod_", "")
config_path = get_default_config_path()
config = load_config(config_path, loader=yaml.RoundTripLoader)
try:
config['mod'][mod_name]['enabled'] = True
dump_config(config_path, config)
except Exception as e:
pass
示例10: disable
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def disable(mod_name):
"""
disable mod
"""
if mod_name not in system_mod and "rqalpha_mod_" in mod_name:
mod_name = mod_name.replace("rqalpha_mod_", "")
config_path = get_default_config_path()
config = load_config(config_path, loader=yaml.RoundTripLoader)
try:
config['mod'][mod_name]['enabled'] = False
dump_config(config_path, config)
except Exception as e:
pass
示例11: entry_point
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def entry_point():
from rqalpha.mod import SYSTEM_MOD_LIST
from rqalpha.utils.package_helper import import_mod
mod_config_path = get_mod_config_path()
mod_config = load_mod_config(mod_config_path, loader=yaml.RoundTripLoader)
for mod_name, config in six.iteritems(mod_config['mod']):
lib_name = "rqalpha_mod_{}".format(mod_name)
if not config['enabled']:
continue
if mod_name in SYSTEM_MOD_LIST:
# inject system mod
import_mod("rqalpha.mod." + lib_name)
else:
# inject third part mod
import_mod(lib_name)
# 原本是注入系统中所有的Mod对应的命令,现在修改为只注入用户启动的Mod对应的命令
# from . import mod
# from pkgutil import iter_modules
# from rqalpha.utils.package_helper import import_mod
# # inject system mod
# for package_name in mod.SYSTEM_MOD_LIST:
# import_mod("rqalpha.mod.rqalpha_mod_" + package_name)
# # inject user mod
# for package in iter_modules():
# if "rqalpha_mod_" in package[1]:
# import_mod(package[1])
cli(obj={})
示例12: set_tikvyml
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def set_tikvyml(knob_sessname, knob_val):
knob_sess=knob_sessname.split('.')[0:-1]
knob_name=knob_sessname.split('.')[-1]
ymldir=os.path.join(ansibledir,"conf","tikv_newcfg.yml")
tmpdir=os.path.join(ansibledir,"conf","tikv.yml")
tmpf=open(tmpdir)
tmpcontent=yaml.load(tmpf, Loader=yaml.RoundTripLoader)
if(knob_set[knob_sessname]['type']=='enum'):
idx=knob_val
knob_val=knob_set[knob_sessname]['enumval'][idx]
if(knob_set[knob_sessname]['type']=='bool'):
if(knob_val==0):
knob_val=False
else:
knob_val=True
if(knob_name=='block-size'):
knob_val=str(knob_val)+"KB"
if(knob_name=='write-buffer-size' or knob_name=='max-bytes-for-level-base' or knob_name=='target-file-size-base'):
knob_val=str(knob_val)+"MB"
if(knob_name in tmpcontent[knob_sess[0]][knob_sess[1]]): # TODO: only support 2 level of knob_sess currently
tmpcontent[knob_sess[0]][knob_sess[1]][knob_name]=knob_val
else:
return('failed')
print("set_tikvyml:: ",knob_sessname, knob_sess, knob_name, knob_val)
ymlf=open(ymldir, 'w')
yaml.dump(tmpcontent, ymlf, Dumper=yaml.RoundTripDumper)
os.popen("rm "+tmpdir+" && "+"mv "+ymldir+" "+tmpdir)
time.sleep(0.5)
return('success')
示例13: _get_complete_date_translation_data
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def _get_complete_date_translation_data(language):
cldr_data = {}
supplementary_data = {}
if language in cldr_languages:
with open(cldr_date_directory + language + '.json') as f:
cldr_data = json.load(f, object_pairs_hook=OrderedDict)
if language in supplementary_languages:
with open(supplementary_date_directory + language + '.yaml') as g:
supplementary_data = OrderedDict(RoundTripLoader(g).get_data())
complete_data = combine_dicts(cldr_data, supplementary_data)
if 'name' not in complete_data:
complete_data['name'] = language
return complete_data
示例14: _load_config
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def _load_config():
try:
with open(_CONFIG_FILE, encoding='utf-8') as file:
return yaml.load(file, Loader=yaml.RoundTripLoader)
except FileNotFoundError:
logger.error('Config file %s not found.', _CONFIG_FILE)
exit(1)
示例15: _init
# 需要导入模块: from ruamel import yaml [as 别名]
# 或者: from ruamel.yaml import RoundTripLoader [as 别名]
def _init():
try:
with open(_LANGS_FILE) as file:
langs_config = yaml.load(file, Loader=yaml.RoundTripLoader)
except FileNotFoundError:
logger.error('Language file %s not found.', _LANGS_FILE)
exit(1)
for lang_name, lang_config in langs_config.items():
if lang_config['type'] == 'compiler':
compiler = Compiler(lang_config['compiler_file'],
shlex.split(lang_config['compiler_args']),
lang_config['code_file'],
lang_config['execute_file'],
shlex.split(lang_config['execute_args']))
_langs[lang_name] = partial(
_compiler_build,
compiler,
parse_time_ns(lang_config.get('time', DEFAULT_TIME)),
parse_memory_bytes(lang_config.get('memory', DEFAULT_MEMORY)),
lang_config.get('process_limit', PROCESS_LIMIT))
elif lang_config['type'] == 'interpreter':
interpreter = Interpreter(lang_config['code_file'],
lang_config['execute_file'],
shlex.split(lang_config['execute_args']))
_langs[lang_name] = partial(_interpreter_build, interpreter)
else:
logger.error('Unknown type %s', lang_config['type'])