本文整理汇总了Python中toml.load函数的典型用法代码示例。如果您正苦于以下问题:Python load函数的具体用法?Python load怎么用?Python load使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_cargo_toml
def read_cargo_toml(key, manifest=None):
"""Read a value from Cargo.toml manifest.
:param key: Key to read from [package], or entire path to a key.
It may contain dots.
:param manifest: Optional path to the manifest,
or a file-like object with it opened
:return: The value
"""
if not isinstance(key, (list, tuple)):
key = (key,) if key.startswith('package.') else ('package', key)
key = list(chain.from_iterable(k.split('.') for k in key))
if not key:
raise ValueError("key must not be empty")
# Read the content of Cargo.toml.
manifest = manifest or Path.cwd() / 'Cargo.toml'
if hasattr(manifest, 'read'):
content = toml.load(manifest)
else:
manifest = Path(manifest)
with manifest.open() as f:
content = toml.load(f)
# Get the value.
value = content
for k in key:
value = value[k]
return value
示例2: _load_sim_file
def _load_sim_file(self, sim_file):
'''Load, parse and expand the simulation file.'''
sim_fname = os.path.realpath(sim_file)
# Load the master file
path, _ = os.path.split(sim_fname)
with open(sim_fname) as file_:
sim = toml.load(file_)
# Expand the building
building_fname = '{}.toml'.format(sim['building']['model'])
with open(os.path.join(path, 'buildings', building_fname)) as file_:
sim['building'] = toml.load(file_)['floor']
# Expand the lifts
processed_lifts = []
for lift in sim['lifts']:
fname = '{}.toml'.format(lift['model'])
with open(os.path.join(path, 'lifts', fname)) as file_:
exp = toml.load(file_)
exp['bottom_floor_number'], exp['top_floor_number'] = lift['range']
exp['location'] = lift['location']
exp['open_doors'] = lift['open_doors']
processed_lifts.append(exp)
sim['lifts'] = processed_lifts
# If provided, initialise the random seed
try:
seed(sim['people']['seed'])
except KeyError:
print('no seed')
pass
self.description = sim
示例3: test_test_mode
def test_test_mode() -> None:
'''
Tests server and client by starting the server in a thread, then
requesting meta and sensor data. Asserts that this data is in the
correct format and returns None on success.
'''
print('Finding default configs...')
if os.path.isdir('docs'):
server_config_file = 'docs/sensed-config.sample.toml'
client_config_file = 'docs/senselog-config.sample.toml'
elif os.path.isdir('../docs'):
server_config_file = '../docs/sensed-config.sample.toml'
client_config_file = '../docs/senselog-config.sample.toml'
print('Running sensed test mode test...')
with open(server_config_file) as f:
server_config = DotMap(toml.load(f))
dt_sensor = datetime.Sensor(server_config)
ni_sensor = netifaces.Sensor(server_config)
cam_sensor = camera.Sensor(server_config)
hat_sensor = hat.Sensor(server_config)
server_config.sensors = DotMap({'datetime': dt_sensor,
'netifaces': ni_sensor,
'camera': cam_sensor,
'hat': hat_sensor})
server = socketserver.UDPServer((server_config.sensed.host,
server_config.sensed.port),
SensedServer)
server.sensors = server_config.sensors
server.config = server_config
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
with open(client_config_file) as f:
client_config = DotMap(toml.load(f))
client = SensedClient(client_config)
meta = client.get_all_meta()
sensors = client.get_all_sensors()
assert isinstance(meta, list)
assert isinstance(meta[0], dict)
assert isinstance(sensors, list)
assert isinstance(sensors[0], dict)
print('Packet test passed. Shutting down...')
server.shutdown()
print('Test complete.')
示例4: read_user_state
async def read_user_state(path):
global prev
global manual
book_files = utility.find_files(path, ('brf', 'pef'))
main_toml = os.path.join(path, 'sd-card', USER_STATE_FILE)
current_book = manual_filename
if os.path.exists(main_toml):
main_state = toml.load(main_toml)
if 'current_book' in main_state:
current_book = main_state['current_book']
if not current_book == manual_filename:
current_book = os.path.join(path, current_book)
if 'current_language' in main_state:
current_language = main_state['current_language']
else:
current_language = 'en_GB:en'
else:
current_language = 'en_GB:en'
manual_toml = os.path.join(path, to_state_file(manual_filename))
if os.path.exists(manual_toml):
t = toml.load(manual_toml)
if 'current_page' in t:
manual = manual._replace(page_number=t['current_page'] - 1)
if 'bookmarks' in t:
manual = manual._replace(bookmarks=tuple(sorted(manual.bookmarks + tuple(
bm - 1 for bm in t['bookmarks']))))
else:
manual = Manual.create(current_language)
books = OrderedDict({manual_filename: manual})
for book_file in book_files:
toml_file = to_state_file(book_file)
book = BookFile(filename=book_file, width=40, height=9)
if os.path.exists(toml_file):
t = toml.load(toml_file)
if 'current_page' in t:
book = book._replace(page_number=t['current_page'] - 1)
if 'bookmarks' in t:
book = book._replace(bookmarks=tuple(sorted(book.bookmarks + tuple(
bm - 1 for bm in t['bookmarks']))))
books[book_file] = book
books[cleaning_filename] = CleaningAndTesting.create()
if current_book not in books:
current_book = manual_filename
user_state = frozendict(books=FrozenOrderedDict(
books), current_book=current_book, current_language=current_language)
prev = user_state
return user_state.copy(books=user_state['books'])
示例5: loadconfig
def loadconfig(filename):
try:
config=toml.load(filename)
except IOError:
with open(filename,"w") as f: pass
config={}
return config
示例6: read_config
def read_config(self):
try:
config_data = toml.load(self.config_file)
except Exception as e:
raise ConfigError(self.config_file, e)
self._validate_config(config_data)
self._config_data = config_data
示例7: load
def load(cls, fp: TextIO, args: Optional[Namespace] = None):
"""
:param fp: .toml file's file pointer
:param args: command line arguments
:return: Config instance
"""
config_dic = toml.load(fp)
code_style_config_dic = config_dic.get('codestyle', {})
postprocess_config_dic = config_dic.get('postprocess', {})
etc_config_dic = config_dic.get('etc', {})
if args:
code_style_config_dic = _update_config_dict(code_style_config_dic,
dict(
template_file=args.template,
workspace_dir=args.workspace,
lang=args.lang))
etc_config_dic = _update_config_dict(etc_config_dic,
dict(
download_without_login=args.without_login,
parallel_download=args.parallel,
save_no_session_cache=args.save_no_session_cache))
return Config(
code_style_config=CodeStyleConfig(**code_style_config_dic),
postprocess_config=PostprocessConfig(**postprocess_config_dic),
etc_config=EtcConfig(**etc_config_dic)
)
示例8: __init__
def __init__(self, parent=None):
"""Initiate the abstract widget that is displayed in the preferences dialog."""
super(Playback, self).__init__(parent)
self.user_config_file = os.path.join(AppDirs('mosaic', 'Mandeep').user_config_dir,
'settings.toml')
with open(self.user_config_file) as conffile:
config = toml.load(conffile)
playback_config = QGroupBox('Playback Configuration')
playback_config_layout = QVBoxLayout()
playback_config_layout.setAlignment(Qt.AlignTop)
self.cover_art_playback = QCheckBox('Cover Art Playback')
self.playlist_save_checkbox = QCheckBox('Save Playlist on Close')
playback_config_layout.addWidget(self.cover_art_playback)
playback_config_layout.addWidget(self.playlist_save_checkbox)
playback_config.setLayout(playback_config_layout)
main_layout = QVBoxLayout()
main_layout.addWidget(playback_config)
self.setLayout(main_layout)
self.check_playback_setting(config)
self.check_playlist_save(config)
self.cover_art_playback.clicked.connect(lambda: self.cover_art_playback_setting(config))
self.playlist_save_checkbox.clicked.connect(lambda: self.playlist_save_setting(config))
示例9: layout
def layout(self, client, userdata):
fixtures = os.path.join(ROOT, 'test/fixtures')
profile = webdriver.FirefoxProfile(os.path.join(fixtures, 'profile', client))
for xpi in glob.glob(os.path.join(ROOT, 'xpi/*.xpi')):
profile.add_extension(xpi)
profile.set_preference('extensions.zotero.translators.better-bibtex.testing', True)
with open(os.path.join(os.path.dirname(__file__), 'preferences.toml')) as f:
preferences = toml.load(f)
for p, v in nested_dict_iter(preferences['general']):
profile.set_preference(p, v)
if userdata.get('locale', '') == 'fr':
for p, v in nested_dict_iter(preferences['fr']):
profile.firefox.set_preference(p, v)
if userdata.get('first-run', 'false') == 'false':
profile.set_preference('extensions.zotero.translators.better-bibtex.citekeyFormat', '[auth][shorttitle][year]')
if client == 'jurism':
print('\n\n** WORKAROUNDS FOR JURIS-M IN PLACE -- SEE https://github.com/Juris-M/zotero/issues/34 **\n\n')
#profile.set_preference('extensions.zotero.dataDir', os.path.join(self.path, 'jurism'))
#profile.set_preference('extensions.zotero.useDataDir', True)
#profile.set_preference('extensions.zotero.translators.better-bibtex.removeStock', False)
profile.update_preferences()
shutil.rmtree(self.path, ignore_errors=True)
shutil.move(profile.path, self.path)
示例10: set_database_path
def set_database_path(dbfolder):
"""Use to write the database path into the config.
Parameters
----------
dbfolder : str or pathlib.Path
Path to where planetpy will store data it downloads..
"""
# First check if there's a config file, so that we don't overwrite
# anything:
try:
config = toml.load(str(configpath))
except IOError: # config file doesn't exist
config = {} # create new config dictionary
# check if there's an `data_archive` sub-dic
try:
archive_config = config["data_archive"]
except KeyError:
config["data_archive"] = {"path": dbfolder}
else:
archive_config["path"] = dbfolder
with open(configpath, "w") as f:
ret = toml.dump(config, f)
print(f"Saved database path {ret} into {configpath}.")
示例11: main
def main() -> None:
data = toml.load(sys.stdin)
assert list(data.keys()) == ["source"]
# this value is non deterministic
data["source"]["vendored-sources"]["directory"] = "@[email protected]"
lines = []
inner = data["source"]
for source, attrs in sorted(inner.items()):
lines.append("[source.{}]".format(quote(source)))
if source == "vendored-sources":
lines.append('"directory" = "@[email protected]"\n')
else:
for key, value in sorted(attrs.items()):
attr = "{} = {}".format(quote(key), quote(value))
lines.append(attr)
lines.append("")
result = "\n".join(lines)
real = toml.loads(result)
assert real == data, "output = {} while input = {}".format(real, data)
print(result)
示例12: __init__
def __init__(self, overrides=None, valgrind=False):
self.pid = None
with open(os.path.join(constants.ASSETS_DIR, "test.toml")) as fp:
self.cf = toml.load(fp)
if overrides:
update_nested_dict(self.cf, overrides)
self.cf["core"].pop("admin_host", None)
# extract some field from cf
self.data_store = self.cf["zone_source"]["type"].lower()
self.pidfile = self.cf["core"]["pidfile"]
self.admin_host = self.cf["core"].get("admin_host", None)
self.admin_port = self.cf["core"].get("admin_port", None)
self.dns_port = self.cf["core"]["port"]
self.dns_host = self.cf["core"]["bind"]
# override mongo host and mongo port
mongo_conf = self.cf["zone_source"]["mongo"]
if self.data_store == "mongo":
self.zm = ZoneMongo(constants.MONGO_HOST,
constants.MONGO_PORT,
mongo_conf["dbname"])
self.valgrind = valgrind
self.cf_str = toml.dumps(self.cf)
self.fp = tempfile.NamedTemporaryFile()
self.fp.write(self.cf_str.encode("utf8"))
self.fp.flush()
fname = self.fp.name
if self.valgrind:
# TODO find the bug of possible lost and still reachable memory
self.cmd = "valgrind --leak-check=full --show-reachable=no --show-possibly-lost=no %s -c %s" % (DNS_BIN, fname)
else:
self.cmd = "%s -c %s" % (DNS_BIN, fname)
self.vagrant = vagrant.Vagrant(root=os.path.join(constants.REPO_ROOT, "vagrant"))
示例13: main
def main():
host = sys.argv[1]
print('Running certbot')
with open('hosts.toml') as f:
host_conf = toml.load(f)
# get certbot domains
print('Run certbot for %s' % host_conf)
certbot_conf = host_conf.get('certbot', {})
if not certbot_conf:
raise ValueError('[certbot] configuration section of the hosts.toml file is empty!')
webroot_path = certbot_conf.get("webroot_path", "/var/www/html/")
domains = certbot_conf.get('domains', [])
email = certbot_conf.get('email')
if not domains or not type(domains) == list:
raise ValueError('certbot conf has no list of domains entry!')
if not os.path.isfile('/usr/bin/certbot'):
install('software-properties-common')
local['add-apt-repository']['-y', 'ppa:certbot/certbot'] & FG
apt_get['update'] & FG
install('python-certbot-nginx')
cron_path = '/etc/cron.d/certbot-global-auto-renew'
if not os.path.isfile(cron_path):
with open(cron_path, 'w') as f:
f.write(cron_template)
os.chmod(cron_path, 0o600)
domains = sorted(domains, key=lambda d:len(d))
name = domains[0]
conf_path = '/etc/letsencrypt/renewal/' + name + '.conf'
matches = True
if not os.path.isfile(conf_path):
matches = False
else:
with open(conf_path, 'r') as f:
text = f.read()
for d in domains:
if d + ' = ' + webroot_path.rstrip('/') not in text:
matches = False
if matches:
print('letsencrypt renewal conf exists for domains %s' % domains)
return
yn = input("Is DNS for the SSL domains pointing to this server? If no, you'll have to confirm domain ownership via editing DNS entries. (y/n) ")
if yn.lower() == 'y':
args = ['certonly', '--webroot', '-w', webroot_path, '--cert-name', name]
for domain in domains:
args.extend(['-d', domain])
local['certbot'][args] & FG
else:
args = ['certonly', '--manual', '--preferred-challenges=dns', '--cert-name', name]
for domain in domains:
args.extend(['-d', domain])
local['certbot'][args] & FG
print("Once DNS is pointing to this server, run certbot again to configure automatic renewal.")
示例14: run
def run():
homu_cfg = toml.load('/home/servo/homu/cfg.toml')
homu_builders = homu_cfg['repo']['servo']['buildbot']
# We need to invoke a new process to read the Buildbot master config
# because Buildbot is written in python2.
scriptpath = os.path.join(os.path.dirname(__file__), 'get_buildbot_cfg.py')
ret = subprocess.run(
['/usr/bin/python2', scriptpath],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if ret.returncode != 0:
return Failure(
'Unable to retrieve buildbot builders:', ret.stderr
)
buildbot_builders = json.loads(ret.stdout.decode('utf-8'))['builders']
failure_msg = ''
for builder_set in ['builders', 'try_builders']:
diff = set(homu_builders[builder_set]) - set(buildbot_builders)
if diff:
failure_msg += 'Invalid builders for "{}": {}\n' \
.format(builder_set, diff)
if failure_msg:
return Failure(
"Homu config isn't synced with Buildbot config:",
failure_msg
)
return Success('Buildbot and homu configs are synced')
示例15: read_config
def read_config():
with open('reversion.toml') as f:
conf = toml.load(f)
if 'currentversion' not in conf:
raise ConfigError("No field named currentversion")
if not isinstance(conf['currentversion'], str):
raise ConfigError("currentversion should be a string, not {}".format(
type(conf['currentversion'])))
places = conf.get('place', [])
if not places:
raise ConfigError("Need at least one replacement site ([[place]] section)")
if not isinstance(places, list):
raise ConfigError("place must be an array")
if not isinstance(places[0], dict):
raise ConfigError("place must be an array of tables")
for place in places:
if 'file' not in place:
raise ConfigError("Missing file= field for place")
if not isinstance(place['file'], str):
raise ConfigError("file must be string")
if ('line-regex' in place) and not isinstance(place['line-regex'], str):
raise ConfigError("linematch must be string")
return conf