2018-01-07 22:19:56 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
|
|
|
|
@file: descriptor.py
|
|
|
|
|
@date: 07.01.2018
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
@description:
|
|
|
|
|
"""
|
|
|
|
|
import logging
|
|
|
|
|
import csv
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
from boswatch.utils import paths
|
|
|
|
|
|
|
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Descriptor:
|
|
|
|
|
"""!CSV Descriptor class to load specific
|
|
|
|
|
descriptions from CSV files, manage and serve them"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""!Initialise a private list for the DescriptionList objects"""
|
|
|
|
|
self._lists = {}
|
|
|
|
|
|
|
|
|
|
def loadDescription(self, csvType):
|
2018-01-07 23:41:52 +01:00
|
|
|
"""!Build a new description list from DescriptionList class
|
2018-01-07 22:19:56 +01:00
|
|
|
|
2018-01-07 23:32:09 +01:00
|
|
|
@param csvType: Name of the CSV file without `.csv`
|
|
|
|
|
@return True or False"""
|
|
|
|
|
bwDescriptionList = DescriptionList()
|
|
|
|
|
if bwDescriptionList.loadCSV(csvType):
|
|
|
|
|
self._lists[csvType] = bwDescriptionList
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2018-01-07 22:19:56 +01:00
|
|
|
|
|
|
|
|
def addDescriptions(self, bwPacket):
|
|
|
|
|
"""!Add the short and long description to a bwPacket
|
|
|
|
|
|
2018-01-07 23:32:09 +01:00
|
|
|
@param bwPacket: bwPacket instance to add descriptions
|
|
|
|
|
@return True or False"""
|
2018-01-07 22:19:56 +01:00
|
|
|
logging.debug("add descriptions to bwPacket")
|
2018-01-07 23:32:09 +01:00
|
|
|
try:
|
|
|
|
|
bwPacket.setField("shortDescription",
|
|
|
|
|
self._lists[bwPacket.getField("mode")].getShortDescription(bwPacket.getField(bwPacket.getField("mode"))))
|
2018-01-07 22:19:56 +01:00
|
|
|
|
2018-01-07 23:32:09 +01:00
|
|
|
bwPacket.setField("longDescription",
|
|
|
|
|
self._lists[bwPacket.getField("mode")].getLongDescription(bwPacket.getField(bwPacket.getField("mode"))))
|
|
|
|
|
return True
|
2018-01-07 23:41:52 +01:00
|
|
|
except: # pragma: no cover
|
2018-01-07 23:32:09 +01:00
|
|
|
logging.exception("error while adding descriptions")
|
|
|
|
|
return False
|
2018-01-07 22:19:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DescriptionList:
|
2018-01-07 23:32:09 +01:00
|
|
|
def __init__(self): # , csvType):
|
2018-01-07 22:19:56 +01:00
|
|
|
"""!Loads the given CSV file into internal list
|
|
|
|
|
|
|
|
|
|
@param csvType: Name of the CSV file without `.csv`"""
|
|
|
|
|
logging.debug("create new descriptionList")
|
|
|
|
|
self._descriptionList = {}
|
2018-01-07 23:32:09 +01:00
|
|
|
# self.loadCSV(csvType)
|
2018-01-07 22:19:56 +01:00
|
|
|
|
|
|
|
|
def getShortDescription(self, id):
|
|
|
|
|
"""!Returns the short description of given id
|
|
|
|
|
|
|
|
|
|
@return short description or empty string"""
|
|
|
|
|
try:
|
|
|
|
|
return self._descriptionList[str(id)]["shortDescription"]
|
|
|
|
|
except:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
def getLongDescription(self, id):
|
|
|
|
|
"""!Returns the long description of given id
|
|
|
|
|
|
|
|
|
|
@return long description or empty string"""
|
|
|
|
|
try:
|
|
|
|
|
return self._descriptionList[str(id)]["longDescription"]
|
|
|
|
|
except:
|
|
|
|
|
return ""
|
|
|
|
|
|
2018-01-07 23:32:09 +01:00
|
|
|
def loadCSV(self, csvType):
|
2018-01-07 22:19:56 +01:00
|
|
|
"""!Load descriptions from an csv file
|
|
|
|
|
|
|
|
|
|
@param csvType: Name of the CSV file without `.csv`
|
|
|
|
|
@return True or False"""
|
|
|
|
|
count = 0
|
|
|
|
|
logging.debug("loading csv file: %s", csvType)
|
|
|
|
|
csvPath = paths.CSV_PATH + csvType + ".csv"
|
|
|
|
|
try:
|
|
|
|
|
csvFile = open(csvPath, 'r', -1, 'utf-8')
|
|
|
|
|
reader = csv.DictReader(csvFile)
|
|
|
|
|
|
|
|
|
|
for line in reader:
|
|
|
|
|
if re.match("^[0-9]+[A-D]?$", line["id"], re.IGNORECASE):
|
|
|
|
|
self._descriptionList[line["id"]] = {"shortDescription": line["shortDescription"], "longDescription": line["longDescription"]}
|
|
|
|
|
logging.debug("- %s", line)
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
logging.debug("%s entry's loaded", count)
|
|
|
|
|
return True
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logging.error("csv file not found: %s", csvPath)
|
|
|
|
|
return False
|
|
|
|
|
except: # pragma: no cover
|
|
|
|
|
logging.exception("error while loading descriptions")
|
|
|
|
|
return False
|