| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | StormSiren |
|---|
| 5 | Copyright (C) 2008 Chris Freeze <cfreeze/cfreeze_com\> |
|---|
| 6 | |
|---|
| 7 | Redistribution and use in source and binary forms, with or without |
|---|
| 8 | modification, are permitted provided that the following conditions |
|---|
| 9 | are met: |
|---|
| 10 | |
|---|
| 11 | 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | notice, this list of conditions and the following disclaimer. |
|---|
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | documentation and/or other materials provided with the distribution. |
|---|
| 16 | |
|---|
| 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|---|
| 19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|---|
| 20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|---|
| 22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|---|
| 26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | """ |
|---|
| 30 | This is StormSiren a program inspired by the StormSiren application |
|---|
| 31 | written by Roy McManus <slorf/users_sourceforge_net>. Much like the |
|---|
| 32 | original StormSiren written by Roy McManus <slorf/users_sourceforge_net>, |
|---|
| 33 | this program is a simple script capable of scanning and providing |
|---|
| 34 | notifications of weather bulletins issued by the National |
|---|
| 35 | Weather Service. StormSiren supports a wide range of paging devices |
|---|
| 36 | and filtering of alert types per alert device. While inspired by |
|---|
| 37 | StormSiren, StormSiren is a complete rewrite that is capable of using |
|---|
| 38 | the new CAP/XML feeds provided at http://www.weather.gov/alerts/. |
|---|
| 39 | |
|---|
| 40 | For more information there is see the README.TXT file located in the root |
|---|
| 41 | of this directory. |
|---|
| 42 | """ |
|---|
| 43 | |
|---|
| 44 | import sys |
|---|
| 45 | import os |
|---|
| 46 | from ConfigParser import NoOptionError, NoSectionError, MissingSectionHeaderError, SafeConfigParser |
|---|
| 47 | import logging.config |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | class Config(object): |
|---|
| 51 | |
|---|
| 52 | DEFAULT_MAX_BYTES = 100000 |
|---|
| 53 | DEFAULT_MAX_LOGS = 3 |
|---|
| 54 | |
|---|
| 55 | def __init__(self, appname, config_file = None): |
|---|
| 56 | self.__appname = appname |
|---|
| 57 | self.__config_file = '' |
|---|
| 58 | self.__configParser = SafeConfigParser() |
|---|
| 59 | self.__config_dir = self.___getConfigDir() |
|---|
| 60 | self.__config_file = self.__config_dir + '/' + appname + '.conf' |
|---|
| 61 | self.log = logging.getLogger('Config') |
|---|
| 62 | |
|---|
| 63 | self.log.setLevel(logging.INFO) |
|---|
| 64 | |
|---|
| 65 | if(config_file): |
|---|
| 66 | self.__config_file = config_file |
|---|
| 67 | |
|---|
| 68 | if(os.path.exists(self.__config_file)): |
|---|
| 69 | self.log.debug("Using configuration file with %s" % self.__config_file) |
|---|
| 70 | else: |
|---|
| 71 | self.log.error("Configuration file [%s] not found" % self.__config_file) |
|---|
| 72 | self.log.error("See the example StormSiren.conf file in the examples directory") |
|---|
| 73 | sys.exit(1) |
|---|
| 74 | |
|---|
| 75 | def ___getConfigDir(self): |
|---|
| 76 | config_dir = "" |
|---|
| 77 | if(os.name == 'posix'): |
|---|
| 78 | home_dir = os.environ.get('HOME') |
|---|
| 79 | config_dir = home_dir + '/.' + self.__appname |
|---|
| 80 | elif(os.name == 'os2'): |
|---|
| 81 | home_dir = os.environ.get('HOME') |
|---|
| 82 | config_dir = home_dir + '/' + self.__appname |
|---|
| 83 | elif(os.name == 'nt'): |
|---|
| 84 | home_dir = os.environ.get('USERPROFILE') # NT/2K/XP |
|---|
| 85 | if(home_dir == None): |
|---|
| 86 | home_dir = os.environ.get('WINDIR') # 95/98/ME |
|---|
| 87 | config_dir = home_dir + '/Application Data/' + self.__appname |
|---|
| 88 | return config_dir |
|---|
| 89 | |
|---|
| 90 | def getRequired(self,section,key): |
|---|
| 91 | val = self.get(section,key) |
|---|
| 92 | if(not val): |
|---|
| 93 | self.log.error("Required key not found in configuration file") |
|---|
| 94 | self.log.error("Section [" + section + "] key [" + key + "=SOMEVALUE] Not Present") |
|---|
| 95 | sys.exit(2) |
|---|
| 96 | else: |
|---|
| 97 | return val |
|---|
| 98 | |
|---|
| 99 | def get(self,section,key,default=None): |
|---|
| 100 | try: |
|---|
| 101 | return self.__configParser.get(section,key) |
|---|
| 102 | except NoSectionError,e: |
|---|
| 103 | self.log.debug("Configuration File Error: Could not find section %s." % e) |
|---|
| 104 | return default |
|---|
| 105 | except NoOptionError: |
|---|
| 106 | self.log.debug("Configuration File Error: Could not find %s in section %s." % (key, section)) |
|---|
| 107 | return default |
|---|
| 108 | |
|---|
| 109 | def _load(self): |
|---|
| 110 | self.log.debug("Loading Configuration File") |
|---|
| 111 | self.__configParser.read(self.__config_file) |
|---|
| 112 | self.__load_logging_settings() |
|---|
| 113 | |
|---|
| 114 | def __load_logging_settings(self): |
|---|
| 115 | logger = logging.getLogger() #Get the root logger |
|---|
| 116 | shouldLogToDisk = self.get('main','logging',"False") |
|---|
| 117 | if(shouldLogToDisk == "True"): |
|---|
| 118 | self.log.debug("User requesting logging to disk") |
|---|
| 119 | logFile = self.get('log','file',None) |
|---|
| 120 | if(not logFile): |
|---|
| 121 | self.log.debug("No log file specified, using default") |
|---|
| 122 | logFile = self.__config_dir + '/' + self.__appname + '.log' |
|---|
| 123 | maxBytes = self.get('log','max_bytes',None) |
|---|
| 124 | if(not maxBytes): |
|---|
| 125 | self.log.debug("Max bytes per log file not specified, using default (%i bytes)", Config.DEFAULT_MAX_BYTES) |
|---|
| 126 | maxBytes = Config.DEFAULT_MAX_BYTES |
|---|
| 127 | maxLogs = self.get('log','max_logs',None) |
|---|
| 128 | if(not maxLogs): |
|---|
| 129 | self.log.debug("Max log files not specified, using default (%i log files)", Config.DEFAULT_MAX_LOGS) |
|---|
| 130 | maxLogs = Config.DEFAULT_MAX_LOGS |
|---|
| 131 | |
|---|
| 132 | logFormat = self.get('log','log_format',None) |
|---|
| 133 | if(not logFormat): |
|---|
| 134 | self.log.debug("Log format not specified, using default") |
|---|
| 135 | logFormat = logging.Formatter("%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s") |
|---|
| 136 | else: |
|---|
| 137 | logFormat = logging.Formatter(logFormat) |
|---|
| 138 | |
|---|
| 139 | handler = logging.handlers.RotatingFileHandler(logFile,"a",maxBytes,maxLogs) |
|---|
| 140 | handler.setFormatter(logFormat) |
|---|
| 141 | logger.addHandler(handler) |
|---|
| 142 | |
|---|
| 143 | def __str__(self): |
|---|
| 144 | return "Config File: " + self.__config_file |
|---|
| 145 | |
|---|
| 146 | def display(self): |
|---|
| 147 | print self.__str__() |
|---|
| 148 | |
|---|
| 149 | def getConfigFile(self): |
|---|
| 150 | return self.__config_file |
|---|
| 151 | |
|---|
| 152 | config_file = property(getConfigFile,None,None) |
|---|
| 153 | |
|---|
| 154 | def getConfigDir(self): |
|---|
| 155 | return self.__config_dir |
|---|
| 156 | |
|---|
| 157 | config_dir = property(getConfigDir,None,None) |
|---|