Created
September 24, 2010 15:45
-
-
Save jehiah/595574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This module encapsulates all knowledge about what environment your | |
code is running in (ie: development / production). | |
Note: make sure all calls to settings.get() are done at runtime, and not at import time. | |
usage: | |
import settings | |
def test(): | |
remote_endpoint = settings.get("remote_endpoint") | |
... | |
""" | |
import tornado.options | |
import random | |
tornado.options.define("environment", default="dev", help="environment") | |
def randomize(values): | |
""" this is a wrapper that returns a function which when called returns a random value""" | |
def picker(): | |
return random.choice(values) | |
return picker | |
# settings that are different for each environment | |
# (ie: pointers to remote endpoints) | |
options = { | |
"dev" : { | |
"remote_endpoint" : "http://127.0.0.1:8080", | |
}, | |
"prod" : { | |
"remote_endpoint" : randomize(["http://remote-server1.com", | |
"http://remote-server2.com"]), | |
} | |
} | |
# settings that are the same for each environment | |
# (ie: they are here just to make them easy to find/change/update) | |
default_options = { | |
"num_records_to_return" : 15, | |
} | |
def get(key): | |
env = tornado.options.options.environment | |
if env not in options: | |
raise Exception("Invalid Environment (%s)" % env) | |
v = options.get(env).get(key) or default_options.get(key) | |
if callable(v): | |
return v() | |
return v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment