2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the opensecurityd as RESTful server
9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
18 # https://www.x-net.at
20 # AIT Austrian Institute of Technology
21 # Donau City Strasse 1
24 # http://www.ait.ac.at
27 # Licensed under the Apache License, Version 2.0 (the "License");
28 # you may not use this file except in compliance with the License.
29 # You may obtain a copy of the License at
31 # http://www.apache.org/licenses/LICENSE-2.0
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS,
35 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 # See the License for the specific language governing permissions and
37 # limitations under the License.
38 # ------------------------------------------------------------
41 # ------------------------------------------------------------
50 from environment import Environment
52 class DictDiffer(object):
54 Calculate the difference between two dictionaries as:
57 (3) keys same in both but changed values
58 (4) keys same in both and unchanged values
60 def __init__(self, current_dict, past_dict):
61 self.current_dict, self.past_dict = current_dict, past_dict
62 self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
63 self.intersect = self.set_current.intersection(self.set_past)
65 return self.set_current - self.intersect
67 return self.set_past - self.intersect
69 return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
71 return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
73 class OpenSecurityException(Exception):
74 def __init__(self, value):
77 return repr(self.value)
79 def setupLogger(name='OpenSecurity'):
80 logger = logging.getLogger(name)
81 logger.setLevel(logging.DEBUG)
82 # create formatter and add it to the handlers
83 formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
84 # create file handler which logs even debug messages
85 fh = logging.FileHandler(os.path.join(Environment('OpenSecurity').log_path, name + '.log'))
86 fh.setLevel(logging.DEBUG)
87 fh.setFormatter(formatter)
89 # create console handler with a higher log level
90 ch = logging.StreamHandler()
91 ch.setLevel(logging.DEBUG)
92 ch.setFormatter(formatter)
96 logger = setupLogger()
97 import_logger = setupLogger('OpenSecurity_initial_import')
99 def showTrayMessage(text, timeout):
100 """show a message on the system tray
102 On windows this is shown like a ballon message.
104 @param text the text to show
105 @param timeout timeout hint in millisecs for the message
109 d = { 'text': str(text), 'timeout': int(timeout) }
110 urllib2.urlopen('http://127.0.0.1:8090/message?' + urllib.urlencode(d))
118 """Test: OpenSecurity logging"""
119 logger.info('test logging')
120 showTrayMessage('tray message test\nwith mutliple lines', 1000)
124 if __name__ == '__main__':