root/siren

Revision 191:1819b4582f3e, 5.0 KB (checked in by Chris Freeze <cfreeze@…>, 15 months ago)

add profiling support

  • 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 logging
45import sys
46import profile
47from optparse import OptionParser
48
49from StormSiren.TestAlert import TestAlert
50from StormSiren.WeatherAlert import WeatherAlert
51from StormSiren.StormWeather import StormWeather
52from StormSiren.StormConfig import StormConfig
53from StormSiren.History import History, DEFAULT_MAX_HISTORICAL_EVENTS
54
55__RELEASE_VERSION = "2.0 RC4"
56__RELEASE_MONTH   = "12"
57__RELEASE_DAY     = "01"
58__RELEASE_YEAR    = "2008"
59
60logging.basicConfig(level=logging.INFO,
61                        format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s')
62
63def main():
64        parser = OptionParser()
65        log = logging.getLogger()
66
67        log.setLevel(logging.DEBUG)
68
69        parser.add_option("-c", "--conf", dest="conf",
70                                        help="Override configuration file", metavar="file_name", default=None)
71
72        parser.add_option("-H", "--history", dest="history",
73                                        help="Override file used to save history", metavar="file_name", default=None)
74
75        parser.add_option("-m", "--maxhistory", dest="maxhistory",
76                                        help="Override maximum history entries to maintain", metavar="integer", default=DEFAULT_MAX_HISTORICAL_EVENTS)
77
78        parser.add_option("-s", "--simalert",
79                                        action="store_true", dest="simalert", default=False,
80                                        help="Simulate run and do not use alert devices")
81
82        parser.add_option("-S", "--simfetch",
83                                        action="store_true", dest="simfetch", default=False,
84                                        help="Simulate the fetch using files in the examples directory")
85
86        parser.add_option("-q", "--quiet",
87                                        action="store_true", dest="quiet", default=False,
88                                        help="Print only important messages to the console")
89
90        parser.add_option("-t", "--testpage", 
91                                        action="store_true", dest="testpage", default=False,
92                                        help="Send a test alert to all configured devices")
93
94
95        (options, args) = parser.parse_args()
96
97        if(options.quiet):
98                logging.disable(logging.debug)
99                logging.disable(logging.info)
100
101        conf = StormConfig(options.conf)
102
103        log.info("StormSiren: v%s (%s/%s/%s)" % (__RELEASE_VERSION, 
104                                                        __RELEASE_MONTH, 
105                                                        __RELEASE_DAY, 
106                                                        __RELEASE_YEAR))
107        log.info("Copyright (C) %s Chris Freeze <cfreeze\/cfreeze_com\>" % __RELEASE_YEAR)
108        log.info("This is free software and comes with no warranty; see the included LICENSE file for details.")
109
110        history_file = options.history or conf.history_file
111        max_history = int(options.maxhistory) or conf.max_history
112        history = History(history_file, max_history)
113
114        #conf.display()
115        #history.display()
116
117        if(len(conf.devices) == 0):
118                log.error("No valid devices found, exiting..")
119                sys.exit(1)
120
121        if(not options.testpage):
122                for state in conf.states:
123                        log.info("State: " + state)
124                        xw = StormWeather(state,history,options.simfetch,options.simalert,conf.proxy)
125                        #xw.display()
126                        for dev in conf.devices:
127                                if(dev.isStateWanted(state)):
128                                        xw.registerDevice(dev)
129                        xw.fetch()
130
131                history.save()
132        else:
133                log.info("Sending test page to all devices")
134                ta = TestAlert("examples/test-alert.cap")
135                ta.fetch()
136                for dev in conf.devices:
137                        log.info("\tSending test alert to:\n%s" % dev.__str__())
138                        dev.send(ta.weather_alert)
139
140
141if __name__ == "__main__":
142        #profile.run("main()","siren.stats")
143        main()
Note: See TracBrowser for help on using the browser.