| 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 | import logging |
|---|
| 47 | |
|---|
| 48 | DEFAULT_MAX_HISTORICAL_EVENTS = 200 |
|---|
| 49 | |
|---|
| 50 | class History: |
|---|
| 51 | def __init__(self, file, max_events = DEFAULT_MAX_HISTORICAL_EVENTS): |
|---|
| 52 | self.file = file |
|---|
| 53 | self.loaded = False |
|---|
| 54 | self.history = {} |
|---|
| 55 | self.new_history = [] |
|---|
| 56 | self.max_events = max_events |
|---|
| 57 | self.log = logging.getLogger('History') |
|---|
| 58 | self.modified = False |
|---|
| 59 | |
|---|
| 60 | self.log.setLevel(logging.INFO) |
|---|
| 61 | |
|---|
| 62 | # See if we can open the history file. |
|---|
| 63 | self.log.info("Loading history file %s" % self.file) |
|---|
| 64 | try: |
|---|
| 65 | hist = open(self.file, 'r') |
|---|
| 66 | for line in hist: |
|---|
| 67 | cline = line.strip("\n") |
|---|
| 68 | self.history[cline] = cline |
|---|
| 69 | hist.close() |
|---|
| 70 | self.loaded = True |
|---|
| 71 | |
|---|
| 72 | # Don't worry if we can't open it. We'll open it later during the writing stage |
|---|
| 73 | except IOError: |
|---|
| 74 | self.log.debug("Unable to load history file [%s] assuming empty file" % self.file) |
|---|
| 75 | pass |
|---|
| 76 | |
|---|
| 77 | def exists(self,id): |
|---|
| 78 | if(id in self.history): |
|---|
| 79 | return True |
|---|
| 80 | else: |
|---|
| 81 | return False |
|---|
| 82 | |
|---|
| 83 | def add(self,id): |
|---|
| 84 | self.modified = True |
|---|
| 85 | self.history[id] = id |
|---|
| 86 | self.new_history.append(id) |
|---|
| 87 | |
|---|
| 88 | def save(self): |
|---|
| 89 | if(self.modified): |
|---|
| 90 | if((len(self.history) + len(self.new_history)) > self.max_events): |
|---|
| 91 | self.log.info("Saving history file, but trimming first") |
|---|
| 92 | self.saveWithTrim() |
|---|
| 93 | else: |
|---|
| 94 | self.log.info("Saving history file") |
|---|
| 95 | self.saveWithoutTrim() |
|---|
| 96 | else: |
|---|
| 97 | self.log.debug("No modifications detected") |
|---|
| 98 | |
|---|
| 99 | def __write_list(self,list): |
|---|
| 100 | try: |
|---|
| 101 | hist = open(self.file + ".tmp", 'w') |
|---|
| 102 | for a in list: |
|---|
| 103 | hist.write(a + '\n') |
|---|
| 104 | hist.close() |
|---|
| 105 | except IOError: |
|---|
| 106 | self.log.error("Unable to write to temporary file: %s" % (self.file + ".tmp")) |
|---|
| 107 | sys.exit(2) |
|---|
| 108 | |
|---|
| 109 | try: |
|---|
| 110 | os.rename(self.file + ".tmp", self.file) |
|---|
| 111 | except OSError,e: |
|---|
| 112 | self.log.error("Unable to overwrite history file: %s" % self.file) |
|---|
| 113 | sys.exit(2) |
|---|
| 114 | |
|---|
| 115 | def saveWithoutTrim(self): |
|---|
| 116 | newlist = [] |
|---|
| 117 | for a in self.history.keys(): |
|---|
| 118 | newlist.append(a) |
|---|
| 119 | self.__write_list(newlist) |
|---|
| 120 | |
|---|
| 121 | def saveWithTrim(self): |
|---|
| 122 | newlist = [] |
|---|
| 123 | |
|---|
| 124 | #Read the original list back into an array first so that we can chop off the top |
|---|
| 125 | try: |
|---|
| 126 | hist = open(self.file, 'r') |
|---|
| 127 | for line in hist: |
|---|
| 128 | cline = line.strip("\n") |
|---|
| 129 | newlist.append(cline) |
|---|
| 130 | hist.close() |
|---|
| 131 | except IOError: |
|---|
| 132 | self.log.error("Unable to read history file: %s" % (self.file)) |
|---|
| 133 | sys.exit(2) |
|---|
| 134 | |
|---|
| 135 | newlist.extend(self.new_history) |
|---|
| 136 | newlist = newlist[-self.max_events:] #Slice off the end |
|---|
| 137 | |
|---|
| 138 | self.__write_list(newlist) |
|---|
| 139 | |
|---|
| 140 | def __str__(self): |
|---|
| 141 | str = "History\n" |
|---|
| 142 | str += "Items: %i\n" % len(self.history) |
|---|
| 143 | str += "Added Items: %i\n" % len(self.new_history) |
|---|
| 144 | str += "Max Events: %i\n" % self.max_events |
|---|
| 145 | if((len(self.history) + len(self.new_history)) > self.max_events): |
|---|
| 146 | str += "Trim Required: yes\n" |
|---|
| 147 | else: |
|---|
| 148 | str += "Trim Required: no\n" |
|---|
| 149 | str += "Items\n\t" |
|---|
| 150 | str += "\n\t".join(self.history) |
|---|
| 151 | return str |
|---|
| 152 | |
|---|
| 153 | def display(self): |
|---|
| 154 | print self.__str__() |
|---|