root/StormSiren/StormConfig.py

Revision 190:75d280708ff5, 8.3 KB (checked in by cfreeze@…, 3 years ago)

correct merge problems that are showing up from bad branching

  • Property exe set to *
Line 
1#!/usr/bin/env python
2
3"""
4StormSiren
5Copyright (C) 2008  Chris Freeze <cfreeze/cfreeze_com\>
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions
9are met:
10
111. Redistributions of source code must retain the above copyright
12   notice, this list of conditions and the following disclaimer.
132. 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
17THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27"""
28
29"""
30This is StormSiren a program inspired by the StormSiren application
31written by Roy McManus <slorf/users_sourceforge_net>.  Much like the
32original StormSiren written by Roy McManus <slorf/users_sourceforge_net>,
33this program is a simple script capable of scanning and providing
34notifications of weather bulletins issued by the National
35Weather Service.  StormSiren supports a wide range of paging devices
36and filtering of alert types per alert device.  While inspired by
37StormSiren, StormSiren is a complete rewrite that is capable of using
38the new CAP/XML feeds provided at http://www.weather.gov/alerts/.
39
40For more information there is see the README.TXT file located in the root
41of this directory.
42"""
43
44import string
45import logging
46
47import History
48from Config import Config
49
50from EmailAlertDevice import *
51from JabberAlertDevice import *
52from MediaAlertDevice import *
53from MythtvAlertDevice import *
54from SmsAlertDevice import *
55from ExecutionAlertDevice import *
56from OsdAlertDevice import *
57
58__APPNAME__ = "StormSiren"
59
60class StormConfig(Config):
61        def __init__(self, config_file = None):
62                super(StormConfig,self).__init__(__APPNAME__,config_file)
63
64                self.__states          = []
65                self.devices           = []
66                self.__history_file  = self.config_dir + '/' + __APPNAME__ + '.history'
67                self.log               = logging.getLogger('StormConfig')
68                self.__max_history     = History.DEFAULT_MAX_HISTORICAL_EVENTS
69                self.__proxy           = ''
70
71                self.log.setLevel(logging.DEBUG)
72                self._load()
73
74        def _load(self):
75                super(StormConfig,self)._load()
76
77                jabberLoadAttempted = False
78                jabber_available    = False
79                osdLoadAttempted = False
80                osd_available    = False
81
82                smtpInfo            = EmailAlertDeviceInfo()
83                jabberInfo          = JabberAlertDeviceInfo()
84                mediaInfo           = MediaAlertDeviceInfo()
85                exeInfo             = ExecutionAlertDeviceInfo()
86                osdInfo             = OsdAlertDeviceInfo()
87
88                xstates = self.getRequired('main','states')
89                if(xstates):
90                         self.__states = string.split(xstates, ',')
91
92                self.__proxy = self.get('main','proxy', None)
93
94                num_watchers = self.getRequired('main','watchers')
95                self.__max_history = int(self.get('main','history',History.DEFAULT_MAX_HISTORICAL_EVENTS))
96
97                smtpInfo.msg_from = self.get('smtp','from',DEFAULT_SMTP_FROM)
98                smtpInfo.host = self.get('smtp','host',DEFAULT_SMTP_HOST)
99                smtpInfo.port = self.get('smtp','port',DEFAULT_SMTP_PORT)
100                smtpInfo.user = self.get('smtp','user')
101                smtpInfo.password = self.get('smtp','password')
102
103                jabberInfo.host = self.get('jabber','host',DEFAULT_JABBER_HOST)
104                jabberInfo.port = self.get('jabber','port',DEFAULT_JABBER_PORT)
105                jabberInfo.user = self.get('jabber','user')
106                jabberInfo.password = self.get('jabber','password')
107
108                mediaInfo.player = self.get('media','player')
109                mediaInfo.forecast = self.get('media','forecast')
110                mediaInfo.statement = self.get('media','statement')
111                mediaInfo.advisory = self.get('media','advisory')
112                mediaInfo.watch = self.get('media','watch')
113                mediaInfo.warning = self.get('media','warning')
114                mediaInfo.alert = self.get('media','alert')
115
116                exeInfo.forecast = self.get('execution','forecast')
117                exeInfo.statement = self.get('execution','statement')
118                exeInfo.advisory = self.get('execution','advisory')
119                exeInfo.watch = self.get('execution','watch')
120                exeInfo.warning = self.get('execution','warning')
121                exeInfo.alert = self.get('execution','alert')
122
123                for i in range(1,int(num_watchers)+1):
124                        dev = self.getRequired(str(i),"device")
125                        alerts = self.getRequired(str(i),"alert")
126                        areas = self.getRequired(str(i),"areas")
127                        self.log.debug("AlertDevice(%s) Areas(%s) Alerts(%s)" % (dev, alerts,areas))
128
129                        if(dev == "email"):
130                                to = self.getRequired(str(i),"to")
131                                textwidth = int(self.get(str(i),"textwidth",-1))
132                                self.devices.append(EmailAlertDevice(to,textwidth,smtpInfo,alerts,areas))
133                                self.log.debug("Adding email device")
134                        if(dev == "sms"):
135                                to = self.getRequired(str(i),"to")
136                                textwidth = int(self.get(str(i),"textwidth",-1))
137                                self.devices.append(SmsAlertDevice(to,textwidth,smtpInfo,alerts,areas))
138                                self.log.debug("Adding sms device")
139                        if(dev == "jabber"):
140                                if(not jabberLoadAttempted):
141                                        jabber_available = self.__loadXmpp()
142                                        jabberLoadAttempted = True
143
144                                if(jabber_available):
145                                        to = self.getRequired(str(i),"to")
146                                        wait_time = int(self.getRequired(str(i),"wait_time"))
147                                        self.devices.append(MythtvAlertDevice(MythtvAlertDeviceInfo(to,wait_time),alerts,areas))
148                                        self.log.debug("Adding jabber device")
149                                else:
150                                        self.log.error("Jabber Support Not Enabled, but Jabber Device Requested!")
151                        if(dev == "media"):
152                                self.devices.append(MediaAlertDevice(mediaInfo,alerts,areas))
153                                self.log.debug("Adding media device")
154                        if(dev == "execution"):
155                                self.devices.append(ExecutionAlertDevice(exeInfo,alerts,areas))
156                                self.log.debug("Adding execution device")
157                        if(dev == "osd"):
158                                if(not osdLoadAttempted):
159                                        osd_available = self.__loadOsd()
160                                        osdLoadAttempted = True
161
162                                if(osd_available):
163                                        position = self.get(str(i),"position", DEFAULT_OSD_POSITION)
164                                        alignment = self.get(str(i),"alignment", DEFAULT_OSD_ALIGNMENT)
165                                        offset = int(self.get(str(i),"offset", DEFAULT_OSD_OFFSET))
166                                        timeout = int(self.get(str(i),"timeout", DEFAULT_OSD_TIMEOUT))
167                                        color = self.get(str(i),"color", DEFAULT_OSD_COLOR)
168                                        font = self.get(str(i),"font", DEFAULT_OSD_FONT)
169                                        self.devices.append(OsdAlertDevice(OsdAlertDeviceInfo(position,
170                                                                                                                                                        alignment,
171                                                                                                                                                        offset,
172                                                                                                                                                        timeout,
173                                                                                                                                                        color,
174                                                                                                                                                        font),
175                                                                                                                alerts,areas))
176                                        self.log.debug("Adding jabber device")
177                                else:
178                                        self.log.error("OSD Support Not Enabled, but OSD Device Requested!")
179                        if(dev == "mythtv"):
180                                to = self.getRequired(str(i),"to")
181                                wait_time = int(self.getRequired(str(i),"wait_time"))
182                                self.devices.append(MythtvAlertDevice(MythtvAlertDeviceInfo(to,wait_time),alerts,areas))
183                                self.log.debug("Adding mythtv device")
184
185        def __str__(self):
186                str = "States:\n\t"
187                str += "\n\t".join(self.__states)
188
189                str += "\nProxy: %s" % self.proxy
190                str += "\nHistory: %i" % self.max_history
191                str += "\nDevices: %i\n" % len(self.devices)
192
193                str += "Printing Device:\n"
194                for d in self.devices:
195                        str += "\n" + '-'*60 + "\n"
196                        str += d.__str__()
197                        str += "\n" + '-'*60 + "\n"
198
199                return str
200
201        def __loadXmpp(self):
202                try:
203                        import xmpp
204                        self.log.info("Jabber support enabled")
205                        return True
206                except ImportError:
207                        self.log.error("xmpp not found, disabling Jabber support")
208                        return False
209
210        def __loadOsd(self):
211                try:
212                        import pyosd
213                        self.log.info("Osd support enabled")
214                        return True
215                except ImportError:
216                        self.log.error("pyosd not found, disabling OSD support")
217                        return False
218
219        def getStates(self):
220                return self.__states
221
222        states = property(getStates,None,None)
223
224        def getHistoryFile(self):
225                return self.__history_file
226
227        history_file = property(getHistoryFile,None,None)
228
229        def getDebug(self):
230                return self.__debug
231
232        def getMaxHistory(self):
233                return self.__max_history
234
235        def getProxy(self):
236                return self.__proxy
237
238        debug = property(getDebug,None,None)
239        max_history = property(getMaxHistory,None,None)
240        proxy = property(getProxy,None,None)
Note: See TracBrowser for help on using the browser.