Skip to main content

Google Appengine environment variables stored in Cloud Datastore (and/or system environment variables)

Project description

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

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

gae_env-0.1.2.tar.gz (6.3 kB view hashes)

Uploaded Source

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