2015-06-25 21:19:41 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-31 19:50:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-06-25 21:19:41 +02:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Function to expand the dataset with a description.
|
|
|
|
|
|
|
|
|
|
@author: Jens Herrmann
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging # Global logger
|
|
|
|
|
import csv # for loading the description files
|
2017-04-28 07:44:12 +02:00
|
|
|
import re # for matching IDs with a regular expression
|
2015-06-25 21:19:41 +02:00
|
|
|
|
2016-10-03 11:56:27 +02:00
|
|
|
from includes import globalVars # Global variables
|
2015-07-30 22:18:31 +02:00
|
|
|
from includes.helper import stringConverter
|
2015-06-25 21:19:41 +02:00
|
|
|
|
2015-07-13 19:43:47 +02:00
|
|
|
|
2015-07-31 19:50:27 +02:00
|
|
|
# local variables
|
|
|
|
|
fmsDescribtionList = {}
|
|
|
|
|
zveiDescribtionList = {}
|
|
|
|
|
ricDescribtionList = {}
|
|
|
|
|
|
|
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Local function will load the csv-file
|
|
|
|
|
#
|
|
|
|
|
def loadCSV(typ, idField):
|
|
|
|
|
"""
|
|
|
|
|
Local function for loading csv-file into python list
|
|
|
|
|
Structure: [id] = description
|
|
|
|
|
|
|
|
|
|
@return: Python list of descriptions
|
|
|
|
|
"""
|
|
|
|
|
resultList = {}
|
|
|
|
|
try:
|
|
|
|
|
logging.debug("-- loading %s.csv", typ)
|
2016-10-03 12:02:18 +02:00
|
|
|
with open(globalVars.script_path+'/csv/'+typ+'.csv') as csvfile:
|
2015-06-25 21:19:41 +02:00
|
|
|
# DictReader expected structure described in first line of csv-file
|
|
|
|
|
reader = csv.DictReader(csvfile)
|
|
|
|
|
for row in reader:
|
|
|
|
|
logging.debug(row)
|
2017-04-28 07:44:12 +02:00
|
|
|
# only import rows with an integer as id, allow subrics though
|
2019-10-17 12:08:47 +02:00
|
|
|
if re.match("^[0-9A-F]+$", row[idField], re.IGNORECASE):
|
2015-07-13 19:43:47 +02:00
|
|
|
try:
|
2017-05-01 09:14:45 +02:00
|
|
|
resultList[row[idField].lower()] = stringConverter.convertToUTF8(row['description'])
|
2015-07-14 16:49:14 +02:00
|
|
|
except:
|
|
|
|
|
# skip entry in case of an exception
|
2015-07-13 19:43:47 +02:00
|
|
|
pass
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("-- loading csv finished")
|
|
|
|
|
except:
|
|
|
|
|
logging.error("loading csvList for typ: %s failed", typ)
|
|
|
|
|
logging.debug("loading csvList for typ: %s failed", typ, exc_info=True)
|
|
|
|
|
raise
|
|
|
|
|
return resultList;
|
|
|
|
|
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# call this for loading the description lists
|
|
|
|
|
#
|
|
|
|
|
def loadDescriptionLists():
|
|
|
|
|
"""
|
|
|
|
|
Load data from the csv-files in global description list for FMS, ZVEI and POCSAG
|
|
|
|
|
|
|
|
|
|
@return: nothing
|
|
|
|
|
@exception: Exception if loading failed
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
logging.debug("loading description lists")
|
|
|
|
|
|
2016-10-03 12:02:18 +02:00
|
|
|
if globalVars.config.getint("FMS", "idDescribed"):
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("- load FMS description list")
|
2015-07-31 19:50:27 +02:00
|
|
|
global fmsDescribtionList
|
|
|
|
|
fmsDescribtionList = loadCSV("fms", "fms")
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2016-10-03 12:02:18 +02:00
|
|
|
if globalVars.config.getint("ZVEI", "idDescribed"):
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("- load ZVEI description list")
|
2015-07-31 19:50:27 +02:00
|
|
|
global zveiDescribtionList
|
|
|
|
|
zveiDescribtionList = loadCSV("zvei", "zvei")
|
2015-06-25 21:19:41 +02:00
|
|
|
|
2016-10-03 12:02:18 +02:00
|
|
|
if globalVars.config.getint("POC", "idDescribed"):
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("- load pocsag description list")
|
2015-07-31 19:50:27 +02:00
|
|
|
global ricDescribtionList
|
|
|
|
|
ricDescribtionList = loadCSV("poc", "ric")
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
except:
|
|
|
|
|
logging.error("cannot load description lists")
|
|
|
|
|
logging.debug("cannot load description lists", exc_info=True)
|
|
|
|
|
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# public function for getting a description
|
2015-07-02 09:02:49 +02:00
|
|
|
#
|
2016-11-20 13:05:24 +01:00
|
|
|
def getDescription(typ, data):
|
2015-06-25 21:19:41 +02:00
|
|
|
"""
|
|
|
|
|
Get description for id.
|
|
|
|
|
Will return id if no description will be found.
|
|
|
|
|
|
|
|
|
|
@return: description as string
|
|
|
|
|
"""
|
2019-10-17 11:01:53 +02:00
|
|
|
resultStr = data
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("look up description lists")
|
|
|
|
|
try:
|
|
|
|
|
if typ == "FMS":
|
2016-11-20 13:05:24 +01:00
|
|
|
resultStr = fmsDescribtionList[data]
|
2015-06-25 21:19:41 +02:00
|
|
|
elif typ == "ZVEI":
|
2016-11-20 13:05:24 +01:00
|
|
|
resultStr = zveiDescribtionList[data]
|
2015-06-25 21:19:41 +02:00
|
|
|
elif typ == "POC":
|
2020-04-22 20:24:54 +02:00
|
|
|
if globalVars.config.getint("POC", "onlysubric"):
|
|
|
|
|
resultStr = ricDescribtionList[data] # only SubRIC
|
2020-04-22 21:44:03 +02:00
|
|
|
else:
|
|
|
|
|
resultStr = ricDescribtionList[data[:-1]] # MainRIC
|
|
|
|
|
resultStr += " " + ricDescribtionList[data] # SubRIC
|
2015-06-25 21:19:41 +02:00
|
|
|
else:
|
2015-07-02 09:02:49 +02:00
|
|
|
logging.warning("Invalid Typ: %s", typ)
|
|
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
except KeyError:
|
|
|
|
|
# will be thrown when there is no description for the id
|
|
|
|
|
# -> nothing to do...
|
|
|
|
|
pass
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-25 21:19:41 +02:00
|
|
|
except:
|
2015-06-29 23:32:55 +02:00
|
|
|
logging.error("Error during look up description lists")
|
2015-06-25 21:19:41 +02:00
|
|
|
logging.debug("Error during look up description lists", exc_info=True)
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2016-11-20 13:05:24 +01:00
|
|
|
logging.debug(" - result for %s: %s", data, resultStr)
|
2015-07-02 09:02:49 +02:00
|
|
|
return resultStr
|