|
| 1 | +""" |
| 2 | +Have multiple environments and need to pass in a config file based on the environment? |
| 3 | +
|
| 4 | +Use this, so you don't pass the wrong config. |
| 5 | +
|
| 6 | +Use: |
| 7 | +
|
| 8 | +#!/usr/bin/python |
| 9 | +import enviro_check |
| 10 | +class Main: |
| 11 | + def __init__(self, configFile): |
| 12 | + pass |
| 13 | +
|
| 14 | + def process(self): |
| 15 | + print "ok" |
| 16 | +
|
| 17 | +if __name__ == "__main__": |
| 18 | + m = Main(entryscript.CONFIGFILE) |
| 19 | + m.process() |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +#!/usr/bin/python |
| 26 | +importos |
| 27 | +importsys |
| 28 | +ENVIRONMENT="development" |
| 29 | +CONFIGFILE=None |
| 30 | + |
| 31 | +defgetConfigFileForEnv(): |
| 32 | +directory=os.path.dirname(__file__) |
| 33 | +return { |
| 34 | +"development" :"%s/../config/development.cfg"%directory, |
| 35 | +"staging" :"%s/../config/staging.cfg"%directory, |
| 36 | +"production" :"%s/../config/production.cfg"%directory |
| 37 | + }.get(ENVIRONMENT,None) |
| 38 | + |
| 39 | +CONFIGFILE=getConfigFileForEnv() |
| 40 | +ifCONFIGFILEisNone: |
| 41 | +sys.exit("Configuration error! Unknown environment set.\ |
| 42 | + Edit configurations.py and set appropriate environment") |
| 43 | +print"CONFIGFILE : %s"%CONFIGFILE |
| 44 | +ifnotos.path.exists(CONFIGFILE): |
| 45 | +sys.exit("Configuration error! Config file does not exist") |
| 46 | +print"configurations ok ......" |