From 0fb024c199163b161a8b3ccb817d7a1ab116ad24 Mon Sep 17 00:00:00 2001 From: Michael Herman Date: Thu, 12 Jun 2014 07:14:45 -0500 Subject: [PATCH] added config evironment check --- 15_check_my_environment.py | 45 +++++++++++++++++++++++++++++++++++++ not_finished.py | 46 -------------------------------------- readme.md | 5 +++-- 3 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 15_check_my_environment.py delete mode 100644 not_finished.py diff --git a/15_check_my_environment.py b/15_check_my_environment.py new file mode 100644 index 0000000..82be943 --- /dev/null +++ b/15_check_my_environment.py @@ -0,0 +1,45 @@ +""" +Pass in a config file based on your environment. + +Example: + +import check_my_environment + + +class Main: + def __init__(self, configFile): + pass + + def process(self): + print "ok" + +if __name__ == "__main__": + m = Main(some_script.CONFIGFILE) + m.process() + +""" + + +import os +import sys +ENVIRONMENT = "development" +CONFIGFILE = None + + +def get_config_file(): + directory = os.path.dirname(__file__) + return { + "development": "{}/../config/development.cfg".format(directory), + "staging": "{}/../config/staging.cfg".format(directory), + "production": "{}/../config/production.cfg".format(directory) + }.get(ENVIRONMENT, None) + +CONFIGFILE = get_config_file() + +if CONFIGFILE is None: + sys.exit("Configuration error! Unknown environment set. \ + Edit config.py and set appropriate environment") +print "Config file: {}".format(CONFIGFILE) +if not os.path.exists(CONFIGFILE): + sys.exit("Configuration error! Config file does not exist") +print "Congig ok ...." diff --git a/not_finished.py b/not_finished.py deleted file mode 100644 index cad918e..0000000 --- a/not_finished.py +++ /dev/null @@ -1,46 +0,0 @@ -""" -Have multiple environments and need to pass in a config file based on the environment? - -Use this, so you don't pass the wrong config. - -Use: - -#!/usr/bin/python -import enviro_check -class Main: - def __init__(self, configFile): - pass - - def process(self): - print "ok" - -if __name__ == "__main__": - m = Main(entryscript.CONFIGFILE) - m.process() - -""" - - - -#!/usr/bin/python -import os -import sys -ENVIRONMENT = "development" -CONFIGFILE = None - -def getConfigFileForEnv(): - directory = os.path.dirname(__file__) - return { - "development" : "%s/../config/development.cfg" % directory, - "staging" : "%s/../config/staging.cfg" % directory, - "production" : "%s/../config/production.cfg" % directory - }.get(ENVIRONMENT, None) - -CONFIGFILE = getConfigFileForEnv() -if CONFIGFILE is None: - sys.exit("Configuration error! Unknown environment set. \ - Edit configurations.py and set appropriate environment") -print "CONFIGFILE : %s" % CONFIGFILE -if not os.path.exists(CONFIGFILE): - sys.exit("Configuration error! Config file does not exist") -print "configurations ok ......" diff --git a/readme.md b/readme.md index 5a2a5e5..c5d4c20 100644 --- a/readme.md +++ b/readme.md @@ -8,9 +8,10 @@ 1. **06_execution_time.py**: class used for timing execution of code 1. **07_benchmark_permissions_loading_django.py**: benchmark loading of permissions in Django 1. **08_basic_email_web_crawler.py**: web crawler for grabbing emails from a website -1. **09_basic_link_web_crawler.py**: web crawler for grabbing links from a website +1. **09_basic_link_web_crawler.py**: web crawler for grabbing links from a website 1. **10_find_files_recursively.py**: recursively grab files from a directory 1. **11_optimize_images_with_wand.py**: recursively grab images from a directory, then optimize them for the web 1. **12_csv_split.py**: Splits a CSV file into multiple files based on command line arguments. 1. **13_random_name_generator.py**: random name generator -1. ***14_html_to_markdown.sh**: Convert all html files in a single directory to markdown +1. **14_html_to_markdown.sh**: Convert all html files in a single directory to markdown +1. **15_check_my_environment.py**: Pass in a config file based on your environment.