Skip to main content

gae_env

Console-visible environment variables for Google Appengine projects

Overview

By default, GAE environment variables may be configured in the app.yaml file or uploaded via a secrets.json.

But, for sensitive data, you should not store it in source code as it will be checked into source control. Even if you dont, the wrong people (inside your organization) may find it there.

Also, your development environment probably uses different config values from your production environment. If these values are stored in code, you will have to run different code in development and production, which is not just messy but also bad practice.

gae_env reads environment variables from Cloud Datastore (and/or system environment variables). This way, it becomes more convenient to edit variables in the Developer's Console

Usage

# coding: utf-8
import gae_env
from gae_env import ValueNotSetError, NOT_SET_VALUE

key_foo = 'foo'

# get the value stored at key 'key_foo'
value_bar = gae_env.get(key_foo)

# By default, values are of type str

# get value as int
value_bar = gae_env.get(key_foo, converter_class=int)

# get value as float
value_bar = gae_env.get(key_foo, converter_class=float)

# ******************************************************************************
# If there is a value for a key in the system environment variables or datastore,
# it will be returned.
# Else, a place holder record will be created and ValueNotSetError exception will be raised.
# The exception will remind you to go to the Developers Console
# and update the placeholder record.
# ******************************************************************************

# The default error raising behaviour can be turned off with a param
# get value without raising any ValueNotSetError
value_bar = gae_env.get(key_foo, raise_value_not_set_error=False)

# If no value has been set for a key and raise_value_not_set_error=False,
# it will return NOT_SET_VALUE.
# To return None instead, pass return_none_for_not_set_value=True
value_bar = gae_env.get(
    key_foo, raise_value_not_set_error=False, return_none_for_not_set_value=True
)

# There's a convenience method for setting the value of a key at runtime
# NB: This sets the value in Cloud Datastore (not system environment variables)
value_bar = 'bar'
gae_env.set_value(name=key_foo, value=value_bar)

How to set Datastore values in the App Engine console:

  1. Go to the console.

  2. Select your project at the top of the page if it's not already selected.

  3. In the Kind dropdown box, select GaeEnvSettings.

  4. Your keys will show up. For those where an exception was raised, they will all have the value __NOT_SET__. Click each one and set its value.

What if I am in development?

  • Using the local admin server. This is usually started on the url http://localhost:8000

    1. In the admin server page, go to the Datastore Viewer
    2. In the EntityKind dropdown box, select GaeEnvSettings.
    3. Your keys will show up. For those where an exception was raised, they will all have the value __NOT_SET__. Click each one and set its value.
  • Alternatively, you can pass environment variables as arguments to dev_appserver.py.

    • The argument --env_var=... can be used to specify the environment variables to use when running the local development server. Each env_var argument is in the format of key=value, and you can define multiple envrionment variables. For example: --env_var=KEY_1=val1 --env_var=KEY_2=val2

Dependencies

gae_env uses the ndb library which uses MemCache and Cloud Datastore under the hood, so it's fast.

But this also means it requires the context of a Google Appengine runtime in order to work, and can be used in Google Appengine projects only

Testing

This project uses nose, nosegae and coverage for testing.

You must have these installed:

pip install -r test_requirements.txt

In addition, you must have gcloud sdk installed, with google appengine enabled.

Run:

export GAE_LIB_ROOT=/path/to/local/google-cloud-sdk/platform/google_appengine/
python setup.py nosetests --gae-lib-root=$GAE_LIB_ROOT

Contributing

  1. Fork it (https://github.com/Odame/gae-env/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Release files for gae-env 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gae-env 0.1.2
File Size Uploaded
gae_env-0.1.2.tar.gz 6.3 kB Details

Release files / gae_env-0.1.2.tar.gz

Download URL gae_env-0.1.2.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b1f08950eda45961a3507265a681b9dc27d0dcc4f7f76c2aa1fab7a0787b546d
BLAKE2b-256 checksum
How to use checksums
15f86ae5f1cc15a980241bd02442761824c4266eb92dac44196c8a94be989059
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.14

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page