OpenSecurity/bin/opensecurity_util.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 221 853af9cfab6a
permissions -rwxr-xr-x
lizenz fixed in all files
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurityd
     6 #   
     7 # the opensecurityd as RESTful server
     8 #
     9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
    10 #
    11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
    12 # 
    13 # 
    14 #     X-Net Services GmbH
    15 #     Elisabethstrasse 1
    16 #     4020 Linz
    17 #     AUSTRIA
    18 #     https://www.x-net.at
    19 # 
    20 #     AIT Austrian Institute of Technology
    21 #     Donau City Strasse 1
    22 #     1220 Wien
    23 #     AUSTRIA
    24 #     http://www.ait.ac.at
    25 # 
    26 # 
    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
    30 # 
    31 #    http://www.apache.org/licenses/LICENSE-2.0
    32 # 
    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 # ------------------------------------------------------------
    39 
    40 
    41 # ------------------------------------------------------------
    42 # imports
    43 
    44 import logging
    45 import os
    46 import urllib
    47 import urllib2
    48 
    49 # local
    50 from environment import Environment
    51 
    52 class DictDiffer(object):
    53     """
    54     Calculate the difference between two dictionaries as:
    55     (1) items added
    56     (2) items removed
    57     (3) keys same in both but changed values
    58     (4) keys same in both and unchanged values
    59     """
    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)
    64     def added(self):
    65         return self.set_current - self.intersect 
    66     def removed(self):
    67         return self.set_past - self.intersect 
    68     def changed(self):
    69         return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
    70     def unchanged(self):
    71         return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
    72     
    73 class OpenSecurityException(Exception):
    74     def __init__(self, value):
    75         self.value = value
    76     def __str__(self):
    77         return repr(self.value)
    78     
    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)
    88     logger.addHandler(fh)
    89     # create console handler with a higher log level
    90     ch = logging.StreamHandler()
    91     ch.setLevel(logging.DEBUG)
    92     ch.setFormatter(formatter)
    93     logger.addHandler(ch)
    94     return logger
    95 
    96 logger = setupLogger()
    97 import_logger = setupLogger('OpenSecurity_initial_import')
    98 
    99 def showTrayMessage(text, timeout):
   100     """show a message on the system tray
   101     
   102     On windows this is shown like a ballon message.
   103     
   104     @param  text        the text to show
   105     @param  timeout     timeout hint in millisecs for the message 
   106 
   107     """
   108     try:
   109         d = { 'text': str(text), 'timeout': int(timeout) }
   110         urllib2.urlopen('http://127.0.0.1:8090/message?' + urllib.urlencode(d))
   111     except:
   112         pass
   113 
   114 
   115 # test method
   116 def test():
   117 
   118     """Test: OpenSecurity logging"""
   119     logger.info('test logging')
   120     showTrayMessage('tray message test\nwith mutliple lines', 1000)
   121 
   122 
   123 # test the module           
   124 if __name__ == '__main__':
   125     test()