Python environ 模块,Env() 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用environ.Env()

项目:django-remote-submission    作者:ornl-ndav    | 项目源码 | 文件源码
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))
项目:django-remote-submission    作者:ornl-ndav    | 项目源码 | 文件源码
def __repr__(self):
        return super(Env, self).__repr__().replace(
            'remote_password={!r}'.format(self.remote_password),
            'remote_password={!r}'.format('******'),
        )
项目:python-param-store    作者:LabD    | 项目源码 | 文件源码
def test_decrypt_env(monkeypatch):
    monkeypatch.setattr(Env, 'ENVIRON', {
        'TEST_VAR': 'ok',
        'TEST_DECRYPT': 'prefix-%{key-name}-data-%{missing}'
    })

    store = MockStore({
        'key-name': 'secret'
    })

    env = Env()
    resolve_django_environ(env, store)

    assert env.str('TEST_VAR') == 'ok'
    assert env.str('TEST_DECRYPT') == 'prefix-secret-data-%{missing}'
项目:edd    作者:JBEI    | 项目源码 | 文件源码
def set_default_site(apps, schema_editor):
    """
    Changes the default site from example.com to whatever is in VIRTUAL_HOST environment.
    """
    Site = apps.get_model('sites', 'Site')
    env = environ.Env()
    domain = env('VIRTUAL_HOST', default='localhost')
    domain = domain.split(',')[-1]  # use the last if a comma-delimited list
    Site.objects.create(domain=domain, name='EDD')
项目:django-env-overrides    作者:jcushman    | 项目源码 | 文件源码
def apply_to(settings, prefix="DJANGO", env=None):
    prefix += "__"

    if not env:
        env = environ.Env()

    for key, value in os.environ.items():
        if key.startswith(prefix):
            path_parts = key.split('__')
            if len(path_parts) < 2:
                continue
            path_parts.pop(0)

            # parse value
            if hasattr(env, path_parts[0]):
                value = getattr(env, path_parts[0])(key)
                path_parts.pop(0)
            else:
                value = env(key)

            # starting with global settings, walk down the tree to find the intended target for this value
            target = settings
            for i, path_part in enumerate(path_parts):
                # if it's an int, treat it as an array index
                if path_part.isdigit():
                    path_part = int(path_part)
                    target_type = type(target)
                    if target_type == list or target_type == tuple:
                        if len(target) <= path_part:
                            target += target_type([{}]) * (path_part - len(target) + 1)
                    else:
                        raise ValueError(
                            "Error parsing %s environment variable: "
                            "If %s%s is an integer, %s%s must be an array." % (
                                key, prefix, "__".join(path_parts[:i + 1]), prefix, "__".join(path_parts[:i])))

                # otherwise it's a dict key -- make sure target exists
                elif path_part not in target:
                    if type(target) != dict:
                        raise ValueError(
                            "Error parsing %s environment variable: %s%s is not a dict." % (
                                key, prefix, "__".join(path_parts[:i + 1])))
                    target[path_part] = {}

                last_target = target
                target = target[path_part]

            # assign value
            last_target[path_part] = value