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
oliver@71
     1
#!/bin/env python
oliver@71
     2
# -*- coding: utf-8 -*-
oliver@71
     3
BarthaM@212
     4
# ------------------------------------------------------------
BarthaM@212
     5
# opensecurityd
BarthaM@212
     6
#   
BarthaM@212
     7
# the opensecurityd as RESTful server
BarthaM@212
     8
#
BarthaM@212
     9
# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
BarthaM@212
    10
#
oliver@240
    11
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
BarthaM@212
    12
# 
BarthaM@212
    13
# 
oliver@240
    14
#     X-Net Services GmbH
oliver@240
    15
#     Elisabethstrasse 1
oliver@240
    16
#     4020 Linz
oliver@240
    17
#     AUSTRIA
oliver@240
    18
#     https://www.x-net.at
oliver@240
    19
# 
oliver@240
    20
#     AIT Austrian Institute of Technology
oliver@240
    21
#     Donau City Strasse 1
oliver@240
    22
#     1220 Wien
oliver@240
    23
#     AUSTRIA
oliver@240
    24
#     http://www.ait.ac.at
oliver@240
    25
# 
oliver@240
    26
# 
oliver@240
    27
# Licensed under the Apache License, Version 2.0 (the "License");
oliver@240
    28
# you may not use this file except in compliance with the License.
oliver@240
    29
# You may obtain a copy of the License at
oliver@240
    30
# 
oliver@240
    31
#    http://www.apache.org/licenses/LICENSE-2.0
oliver@240
    32
# 
oliver@240
    33
# Unless required by applicable law or agreed to in writing, software
oliver@240
    34
# distributed under the License is distributed on an "AS IS" BASIS,
oliver@240
    35
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
oliver@240
    36
# See the License for the specific language governing permissions and
oliver@240
    37
# limitations under the License.
BarthaM@212
    38
# ------------------------------------------------------------
BarthaM@212
    39
BarthaM@212
    40
BarthaM@212
    41
# ------------------------------------------------------------
BarthaM@212
    42
# imports
BarthaM@212
    43
oliver@71
    44
import logging
oliver@71
    45
import os
oliver@193
    46
import urllib
oliver@193
    47
import urllib2
oliver@71
    48
oliver@71
    49
# local
oliver@71
    50
from environment import Environment
oliver@71
    51
mb@78
    52
class DictDiffer(object):
mb@78
    53
    """
mb@78
    54
    Calculate the difference between two dictionaries as:
mb@78
    55
    (1) items added
mb@78
    56
    (2) items removed
mb@78
    57
    (3) keys same in both but changed values
mb@78
    58
    (4) keys same in both and unchanged values
mb@78
    59
    """
mb@78
    60
    def __init__(self, current_dict, past_dict):
mb@78
    61
        self.current_dict, self.past_dict = current_dict, past_dict
mb@78
    62
        self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
mb@78
    63
        self.intersect = self.set_current.intersection(self.set_past)
mb@78
    64
    def added(self):
mb@78
    65
        return self.set_current - self.intersect 
mb@78
    66
    def removed(self):
mb@78
    67
        return self.set_past - self.intersect 
mb@78
    68
    def changed(self):
mb@78
    69
        return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
mb@78
    70
    def unchanged(self):
mb@78
    71
        return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
mb@78
    72
    
oliver@71
    73
class OpenSecurityException(Exception):
oliver@71
    74
    def __init__(self, value):
oliver@71
    75
        self.value = value
oliver@71
    76
    def __str__(self):
oliver@71
    77
        return repr(self.value)
oliver@71
    78
    
oliver@71
    79
def setupLogger(name='OpenSecurity'):
oliver@71
    80
    logger = logging.getLogger(name)
oliver@71
    81
    logger.setLevel(logging.DEBUG)
oliver@71
    82
    # create formatter and add it to the handlers
oliver@71
    83
    formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
oliver@71
    84
    # create file handler which logs even debug messages
oliver@71
    85
    fh = logging.FileHandler(os.path.join(Environment('OpenSecurity').log_path, name + '.log'))
oliver@71
    86
    fh.setLevel(logging.DEBUG)
oliver@71
    87
    fh.setFormatter(formatter)
oliver@71
    88
    logger.addHandler(fh)
oliver@71
    89
    # create console handler with a higher log level
oliver@71
    90
    ch = logging.StreamHandler()
oliver@71
    91
    ch.setLevel(logging.DEBUG)
oliver@71
    92
    ch.setFormatter(formatter)
oliver@71
    93
    logger.addHandler(ch)
oliver@71
    94
    return logger
oliver@71
    95
oliver@71
    96
logger = setupLogger()
BarthaM@221
    97
import_logger = setupLogger('OpenSecurity_initial_import')
oliver@193
    98
oliver@193
    99
def showTrayMessage(text, timeout):
oliver@193
   100
    """show a message on the system tray
oliver@193
   101
    
oliver@193
   102
    On windows this is shown like a ballon message.
oliver@193
   103
    
oliver@193
   104
    @param  text        the text to show
oliver@193
   105
    @param  timeout     timeout hint in millisecs for the message 
oliver@193
   106
oliver@193
   107
    """
oliver@193
   108
    try:
oliver@193
   109
        d = { 'text': str(text), 'timeout': int(timeout) }
oliver@193
   110
        urllib2.urlopen('http://127.0.0.1:8090/message?' + urllib.urlencode(d))
oliver@193
   111
    except:
oliver@193
   112
        pass
oliver@193
   113
oliver@193
   114
oliver@71
   115
# test method
oliver@71
   116
def test():
oliver@71
   117
oliver@71
   118
    """Test: OpenSecurity logging"""
oliver@71
   119
    logger.info('test logging')
oliver@193
   120
    showTrayMessage('tray message test\nwith mutliple lines', 1000)
oliver@71
   121
oliver@71
   122
oliver@71
   123
# test the module           
oliver@71
   124
if __name__ == '__main__':
oliver@71
   125
    test()