Skip to main content

Switch environment variable based profiles in Python with ease

Project description

pip install wr-profiles

Example

# warehouse.py

from wr_profiles import Profile, Property

class _WarehouseProfile(Profile):
    profile_root = 'warehouse'

    host = Property('host', default='localhost')
    user = Property('user')
    password = Property('password', default='')

    @property
    def url(self):
        return '{user}:{password}@{host}'.format(user=self.user, password=self.password, host=self.host)

warehouse_profile = _WarehouseProfile()

print(warehouse_profile.url)
> export WAREHOUSE_SANDBOX_USER="sandbox-user"
> export WAREHOUSE_SANDBOX_PASSWORD="sandbox-password"
> export WAREHOUSE_PROD_USER="root"
> export WAREHOUSE_PROD_PASSWORD="bigSecret"
> export WAREHOUSE_PROFILE="sandbox"

> python warehouse.py
sandbox-user:sandbox-password@localhost

> WAREHOUSE_PROFILE="prod" python warehouse.py
root:bigSecret@localhost

Profile

A Profile represents a set of configuration values backed by environment variables. The active profile can be switched by changing just one environment variable – the one identifying the name of the profile to be used. Your code may be using more than one independent profile at a time, or a combination of interrelated profiles when changing one enclosing profile changes selection of sub-profiles.

For example, your code may be connecting to a data warehouse. Most of the time you connect to one instance, but in certain scenarios, say, when doing integration testing, you may wish to connect to a different instance. The configuration for this connection consists of multiple environment variables. Changing them all just to connect to a different warehouse should be as easy as setting a single environment variable.

A profile can be either live or frozen. A live profile always checks the backing environment variables. A frozen profile instance uses the values that were loaded before freezing and does not consult the environment variables.

In a single codebase, one usually has a single object whose properties are consulted to get the prop values of the active profile. The variables being consulted depend on the currently selected profile of the particular type. Profiles of one type have a common profile root. Profile root is the name that all environment variables backing this profile start with.

For example, a warehouse connection could be governed by a WarehouseProfile. In your code you would then refer to warehouse_profile, a singleton of this class. Profile root for this profile could be warehouse. The active warehouse profile would be pointed to by environment variable WAREHOUSE_PROFILE which holds the name of the active profile. If this variable was set to "local_replica", then the properties of warehouse profile would be loaded from environment variables that start with WAREHOUSE_LOCAL_REPLICA_.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wr-profiles-1.0.1.tar.gz (8.6 kB view details)

Uploaded Source

File details

Details for the file wr-profiles-1.0.1.tar.gz.

File metadata

  • Download URL: wr-profiles-1.0.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for wr-profiles-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d5f8f11724c09103c5f9c5e5dd5c4fe2f5612eecd9901349bf6e33c585ad9062
MD5 d8bbdde4980d6acb7b9db70d15177d1e
BLAKE2b-256 e18935af250f57a07277f1a32319e408b028410a0615b8670422dc503d9b8803

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page