2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the opensecurityd as RESTful server
9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
11 # Copyright (C) 2013 AIT Austrian Institute of Technology
12 # AIT Austrian Institute of Technology GmbH
13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
14 # http://www.ait.ac.at
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation version 2.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 # Boston, MA 02110-1301, USA.
29 # ------------------------------------------------------------
32 # ------------------------------------------------------------
41 from environment import Environment
43 class DictDiffer(object):
45 Calculate the difference between two dictionaries as:
48 (3) keys same in both but changed values
49 (4) keys same in both and unchanged values
51 def __init__(self, current_dict, past_dict):
52 self.current_dict, self.past_dict = current_dict, past_dict
53 self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
54 self.intersect = self.set_current.intersection(self.set_past)
56 return self.set_current - self.intersect
58 return self.set_past - self.intersect
60 return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
62 return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
64 class OpenSecurityException(Exception):
65 def __init__(self, value):
68 return repr(self.value)
70 def setupLogger(name='OpenSecurity'):
71 logger = logging.getLogger(name)
72 logger.setLevel(logging.DEBUG)
73 # create formatter and add it to the handlers
74 formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
75 # create file handler which logs even debug messages
76 fh = logging.FileHandler(os.path.join(Environment('OpenSecurity').log_path, name + '.log'))
77 fh.setLevel(logging.DEBUG)
78 fh.setFormatter(formatter)
80 # create console handler with a higher log level
81 ch = logging.StreamHandler()
82 ch.setLevel(logging.DEBUG)
83 ch.setFormatter(formatter)
87 logger = setupLogger()
88 import_logger = setupLogger('OpenSecurity_initial_import')
90 def showTrayMessage(text, timeout):
91 """show a message on the system tray
93 On windows this is shown like a ballon message.
95 @param text the text to show
96 @param timeout timeout hint in millisecs for the message
100 d = { 'text': str(text), 'timeout': int(timeout) }
101 urllib2.urlopen('http://127.0.0.1:8090/message?' + urllib.urlencode(d))
109 """Test: OpenSecurity logging"""
110 logger.info('test logging')
111 showTrayMessage('tray message test\nwith mutliple lines', 1000)
115 if __name__ == '__main__':