BOSWatch/includes/descriptionList.py

115 lines
2.8 KiB
Python
Raw Normal View History

2015-06-25 21:19:41 +02:00
#!/usr/bin/python
# -*- coding: cp1252 -*-
"""
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
from includes import globals # Global variables
##
#
# 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)
with open(globals.script_path+'/csv/'+typ+'.csv') as csvfile:
# DictReader expected structure described in first line of csv-file
reader = csv.DictReader(csvfile)
for row in reader:
logging.debug(row)
# only import rows with an integer as id
if row[idField].isdigit() == True:
resultList[row[idField]] = row['description']
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")
if globals.config.getint("FMS", "idDescribed"):
logging.debug("- load FMS description list")
globals.fmsDescribtionList = loadCSV("fms", "fms")
2015-07-02 09:02:49 +02:00
2015-06-25 21:19:41 +02:00
if globals.config.getint("ZVEI", "idDescribed"):
logging.debug("- load ZVEI description list")
globals.zveiDescribtionList = loadCSV("zvei", "zvei")
if globals.config.getint("POC", "idDescribed"):
logging.debug("- load pocsag description list")
globals.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)
pass
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
#
2015-06-25 21:19:41 +02:00
def getDescription(typ, id):
"""
Get description for id.
Will return id if no description will be found.
@return: description as string
"""
resultStr = id;
logging.debug("look up description lists")
try:
if typ == "FMS":
resultStr = globals.fmsDescribtionList[id]
elif typ == "ZVEI":
resultStr = globals.zveiDescribtionList[id]
elif typ == "POC":
resultStr = globals.ricDescribtionList[id]
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)
pass
2015-07-02 09:02:49 +02:00
2015-06-25 21:19:41 +02:00
logging.debug(" - result for %s: %s", id, resultStr)
2015-07-02 09:02:49 +02:00
return resultStr