def yaml(self):
"""Serialize the object to yaml"""
return yaml.dump(self.data)
python类dump()的实例源码
def save(self):
"""Save this config to disk.
If the charm is using the :mod:`Services Framework <services.base>`
or :meth:'@hook <Hooks.hook>' decorator, this
is called automatically at the end of successful hook execution.
Otherwise, it should be called directly by user code.
To disable automatic saves, set ``implicit_save=False`` on this
instance.
"""
with open(self.path, 'w') as f:
json.dump(self, f)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)
postprocess_toc_yml.py 文件源码
项目:azure-docs-sdk-python
作者: MicrosoftDocs
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def rewrite_yml(data):
with io.open('toc.yml', 'w', encoding='utf8') as outfile:
yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True)
def save_yaml_config(filepath, config):
with open(filepath, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
def save(self, cfg_file=None):
"""
Save's configuration back to disk.
"""
if cfg_file is None:
cfg_file = self.cfg_file
else:
# acquire new configuration path
cfg_file = abspath(expanduser(cfg_file))
try:
with open(cfg_file, 'w') as fp:
yaml.dump(self.cfg_data, fp, default_flow_style=False)
except IOError, e:
logger.debug('%s' % (str(e)))
logger.error('Failed to write configuration file %s' % (
cfg_file,
))
return False
# Update central configuration
self.cfg_file = cfg_file
if hasattr(self, '__mask_re'):
# Presumably something has changed if the user called save so we
# destroy our cached mask to be safe
del self.__mask_re
return True
def insert(data, filename):
with open(filename, 'w') as yaml_file:
yaml.dump(data, yaml_file)
def enter_data(cont, valinput, filename): #Cont for container#profile.yml
stream = open(filename, 'r')
prof = yaml.load(stream) # Player information is stored here.
prof[cont] = valinput
with open(filename, 'w') as yaml_file:
yaml_file.write(yaml.dump(prof, default_flow_style = False))
def get_data(cont, filename):
stream = open(filename, 'r')
profile = yaml.load(stream) # Player information is stored here.
data = yaml.dump(profile, default_flow_style = False, encoding=('utf-8'))
return profile[cont]
def save_yaml_config(filepath, config):
with open(filepath, 'w') as f:
yaml.dump(config, f, default_flow_style=False)
def saveUserPreferences(self) :
'''
write user preferences into the related configuration file
'''
with open(self._userPrfesPathName, 'w') as outfile:
yaml.dump(self._userPrefs, outfile, default_flow_style=False)
def write(data):
with open(path, 'w') as file:
yaml.dump(data, file, default_flow_style=True)
def load_source(in_f, out_f):
reader = csv.reader(in_f)
entries = [make_entry(row) for row in reader]
yaml.dump(entries, out_f, allow_unicode=True, default_flow_style=False)
def __save_config(self):
"""Saves the configuration.
Saves the config attribute to the configuration
file.
"""
with open(self.USERS_CONF_PATH, "w+") as config_file:
config_file.write(yaml.dump(self.__config))
def hugepage_support(user, group='hugetlb', nr_hugepages=256,
max_map_count=65536, mnt_point='/run/hugepages/kvm',
pagesize='2MB', mount=True, set_shmmax=False):
"""Enable hugepages on system.
Args:
user (str) -- Username to allow access to hugepages to
group (str) -- Group name to own hugepages
nr_hugepages (int) -- Number of pages to reserve
max_map_count (int) -- Number of Virtual Memory Areas a process can own
mnt_point (str) -- Directory to mount hugepages on
pagesize (str) -- Size of hugepages
mount (bool) -- Whether to Mount hugepages
"""
group_info = add_group(group)
gid = group_info.gr_gid
add_user_to_group(user, group)
if max_map_count < 2 * nr_hugepages:
max_map_count = 2 * nr_hugepages
sysctl_settings = {
'vm.nr_hugepages': nr_hugepages,
'vm.max_map_count': max_map_count,
'vm.hugetlb_shm_group': gid,
}
if set_shmmax:
shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax']))
shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages
if shmmax_minsize > shmmax_current:
sysctl_settings['kernel.shmmax'] = shmmax_minsize
sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
lfstab = fstab.Fstab()
fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
if fstab_entry:
lfstab.remove_entry(fstab_entry)
entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
lfstab.add_entry(entry)
if mount:
fstab_mount(mnt_point)
def yaml(self):
"""Serialize the object to yaml"""
return yaml.dump(self.data)
def save(self):
"""Save this config to disk.
If the charm is using the :mod:`Services Framework <services.base>`
or :meth:'@hook <Hooks.hook>' decorator, this
is called automatically at the end of successful hook execution.
Otherwise, it should be called directly by user code.
To disable automatic saves, set ``implicit_save=False`` on this
instance.
"""
with open(self.path, 'w') as f:
json.dump(self, f)
def store_context(self, file_name, config_data):
if not os.path.isabs(file_name):
file_name = os.path.join(hookenv.charm_dir(), file_name)
with open(file_name, 'w') as file_stream:
os.fchmod(file_stream.fileno(), 0o600)
yaml.dump(config_data, file_stream)