BOSWatch/includes/filter.py

95 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: cp1252 -*-
2015-05-27 07:48:24 +02:00
"""
2015-06-14 20:21:21 +02:00
Functions for the RegEX filter
2015-05-27 07:48:24 +02:00
@author: Bastian Schroll
@requires: Configuration has to be set in the config.ini
"""
import logging # Global logger
2015-05-23 08:58:46 +02:00
import re #Regex for Filter Check
from includes import globals # Global variables
2015-06-30 08:27:50 +02:00
from includes import converter # converter functions
2015-05-26 07:19:24 +02:00
def loadFilters():
2015-05-27 07:48:24 +02:00
"""
2015-06-14 20:21:21 +02:00
load all filters from the config.ini into globals.filterList
2015-05-27 07:48:24 +02:00
@requires: Configuration has to be set in the config.ini
2015-07-02 09:02:49 +02:00
2015-05-27 07:48:24 +02:00
@return: nothing
"""
try:
2015-05-26 11:41:05 +02:00
logging.debug("loading filters")
2015-06-14 20:21:21 +02:00
# For each entry in config.ini [Filters] section
for key,val in globals.config.items("Filters"):
logging.debug(" - %s = %s", key, val)
filter = val.split(";")
2015-07-02 09:02:49 +02:00
2015-06-30 08:27:50 +02:00
# resolve the * for freqToHz()
if not filter[3] == "*":
filter[3] = converter.freqToHz(filter[3])
2015-07-02 09:02:49 +02:00
2015-06-14 20:21:21 +02:00
# insert splitet data into globals.filterList
2015-06-30 08:27:50 +02:00
globals.filterList.append({"name": key, "typ": filter[0], "dataField": filter[1], "plugin": filter[2], "freq": filter[3], "regex": filter[4]})
except:
logging.error("cannot read config file")
logging.debug("cannot read config file", exc_info=True)
return
2015-07-02 09:02:49 +02:00
2015-05-28 09:18:21 +02:00
def checkFilters(typ,data,plugin,freq):
2015-05-27 07:48:24 +02:00
"""
2015-06-14 20:21:21 +02:00
Check the Typ/Plugin combination with the RegEX filter
If no filter for the combination is found, function returns True.
2015-07-02 09:02:49 +02:00
2015-05-27 07:48:24 +02:00
@type typ: string (FMS|ZVEI|POC)
@param typ: Typ of the dataset
2015-05-28 09:18:21 +02:00
@type data: map of data (structure see interface.txt)
@param data: Contains the parameter
2015-05-27 07:48:24 +02:00
@type plugin: string
2015-06-14 20:21:21 +02:00
@param plugin: Name of the plugin to checked
2015-05-28 09:18:21 +02:00
@type freq: string
@param freq: frequency of the SDR Stick
2015-07-02 09:02:49 +02:00
2015-06-14 20:21:21 +02:00
@requires: all filters in the filterList
2015-07-02 09:02:49 +02:00
2015-05-27 07:48:24 +02:00
@return: nothing
"""
2015-05-23 08:58:46 +02:00
try:
2015-05-28 09:18:21 +02:00
logging.debug("search Filter for %s to %s at %s Hz", typ, plugin, freq)
2015-07-02 09:02:49 +02:00
2015-05-23 08:58:46 +02:00
foundFilter = False
2015-06-14 20:21:21 +02:00
# go to all filter in globals.filterList
2015-05-23 08:58:46 +02:00
for i in globals.filterList:
2015-06-14 20:21:21 +02:00
# if typ/plugin/freq combination is found
2015-05-28 09:18:21 +02:00
if i["typ"] == typ and (i["plugin"] == plugin or i['plugin'] == "*") and (i["freq"] == freq or i['freq'] == "*"):
2015-05-23 08:58:46 +02:00
foundFilter = True
logging.debug("found Filter: %s = %s", i["name"], i["regex"])
2015-06-14 20:21:21 +02:00
# Check the RegEX
2015-05-25 10:37:20 +02:00
if re.search(i["regex"], data[i["dataField"]]):
2015-05-23 09:04:01 +02:00
logging.debug("Filter passed: %s", i["name"])
2015-05-23 08:58:46 +02:00
return True
else:
2015-05-23 09:04:01 +02:00
logging.debug("Filter not passed: %s", i["name"])
2015-07-02 09:02:49 +02:00
2015-05-23 08:58:46 +02:00
if foundFilter:
logging.debug("no Filter passed")
return False
else:
logging.debug("no Filter found")
return True
2015-07-02 09:02:49 +02:00
2015-05-23 08:58:46 +02:00
except:
logging.error("Error in filter checking")
logging.debug("Error in filter checking", exc_info=True)
# something goes wrong, data will path
2015-07-02 09:02:49 +02:00
return True