我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用environ.Path()。
def env(): import environ path = environ.Path(__file__) - 2 env = environ.Env() environ.Env.read_env(path('.env')) try: return Env( server_hostname=env('TEST_SERVER_HOSTNAME'), server_port=env.int('TEST_SERVER_PORT'), remote_directory=env('TEST_REMOTE_DIRECTORY'), remote_filename=env('TEST_REMOTE_FILENAME'), remote_user=env('TEST_REMOTE_USER'), remote_password=env('TEST_REMOTE_PASSWORD'), python_path=env('TEST_PYTHON_PATH'), python_arguments=env.list('TEST_PYTHON_ARGUMENTS'), ) except Exception as e: pytest.skip('Environment variables not set: {!r}'.format(e))
def update_body(app, pagename, templatename, context, doctree): outdir = environ.Path(app.config.html_context['output_directory']) project = app.config.project version = app.config.version if not os.path.exists(outdir.root): os.makedirs(outdir.root) directory_name = "{name}-{version}".format(name=project, version=version) json_dir = outdir.path(directory_name) if not os.path.exists(json_dir.root): os.makedirs(json_dir.root) try: out_dir = json_dir.path('/'.join(pagename.split('/')[:-1])) if not os.path.exists(out_dir()): os.makedirs(out_dir()) out_file = json_dir.path(pagename + '.json') to_write = open(out_file(), 'w+') to_context = copy.copy(context) # Use list here so we don't get an error on changing dict during iteration for key in list(context): if key not in KEYS: del to_context[key] to_write.write(json.dumps(to_context, indent=4)) except Exception: log.exception('Failure in JSON search dump')
def load_test_file(name): "Opens test files saved in the `files` directory." cwd = environ.Path(__file__) - 1 filepath = cwd('files', name) return open(filepath, 'rb')