BW3-Core/boswatch/wildcard.py

106 lines
3.5 KiB
Python
Raw Normal View History

2018-05-16 11:18:23 +02:00
#!/usr/bin/python
2018-01-15 14:18:15 +01:00
# -*- coding: utf-8 -*-
r"""!
2018-01-15 14:18:15 +01:00
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: wildcard.py
@date: 15.01.2018
@author: Bastian Schroll
@description: Functions to replace wildcards in stings
2018-01-15 14:18:15 +01:00
"""
import logging
2018-02-22 13:36:50 +01:00
import time
2018-01-15 14:18:15 +01:00
logging.debug("- %s loaded", __name__)
2019-10-26 18:35:31 +02:00
# todo check function - write an test
_additionalWildcards = {}
def registerWildcard(wildcard, bwPacketField):
r"""!Register a new additional wildcard
2019-10-26 18:35:31 +02:00
@param wildcard: New wildcard string with format: '{WILDCARD}'
@param bwPacketField: Field of the bwPacket which is used for wildcard replacement"""
if wildcard in _additionalWildcards:
logging.error("wildcard always registered: %s", wildcard)
return
logging.debug("register new wildcard %s for field: %s", wildcard, bwPacketField)
_additionalWildcards[wildcard] = bwPacketField
2018-01-15 14:18:15 +01:00
2018-02-22 13:36:50 +01:00
def replaceWildcards(message, bwPacket):
r"""!Replace the wildcards in a given message
2019-10-26 18:35:31 +02:00
@param message: Message in which wildcards should be replaced
@param bwPacket: bwPacket instance with the replacement information
@return Input message with the replaced wildcards"""
2018-02-01 14:41:09 +01:00
_wildcards = {
# formatting wildcards
2019-10-26 13:41:17 +02:00
# todo check if br and par are needed - if not also change config
2018-02-22 13:36:50 +01:00
"{BR}": "\r\n",
"{LPAR}": "(",
"{RPAR}": ")",
2019-10-26 10:18:55 +02:00
"{TIME}": time.strftime("%d.%m.%Y %H:%M:%S"),
2018-02-22 13:36:50 +01:00
# info wildcards
2019-03-02 09:15:40 +01:00
# server
2020-02-22 23:47:13 +01:00
"{SNAME}": bwPacket.get("serverName"),
"{SVERS}": bwPacket.get("serverVersion"),
"{SDATE}": bwPacket.get("serverBuildDate"),
"{SBRCH}": bwPacket.get("serverBranch"),
2019-03-02 09:15:40 +01:00
# client
2020-02-22 23:47:13 +01:00
"{CNAME}": bwPacket.get("clientName"),
"{CIP}": bwPacket.get("clientIP"),
"{CVERS}": bwPacket.get("clientVersion"),
"{CDATE}": bwPacket.get("clientBuildDate"),
"{CBRCH}": bwPacket.get("clientBranch"),
2018-01-15 14:18:15 +01:00
2018-02-01 14:41:09 +01:00
# boswatch wildcards
2020-02-24 21:51:19 +01:00
"{INSRC}": bwPacket.get("inputSource"),
"{TIMES}": bwPacket.get("timestamp"),
2020-02-22 23:47:13 +01:00
"{FREQ}": bwPacket.get("frequency"),
"{MODE}": bwPacket.get("mode"),
2018-01-15 14:18:15 +01:00
2018-02-01 14:41:09 +01:00
# fms wildcards
2020-02-22 23:47:13 +01:00
"{FMS}": bwPacket.get("fms"),
"{SERV}": bwPacket.get("service"),
"{COUNT}": bwPacket.get("country"),
"{LOC}": bwPacket.get("location"),
"{VEHC}": bwPacket.get("vehicle"),
"{STAT}": bwPacket.get("status"),
"{DIR}": bwPacket.get("direction"),
2020-02-24 21:51:19 +01:00
"{DIRT}": bwPacket.get("directionText"),
2020-02-22 23:47:13 +01:00
"{TACI}": bwPacket.get("tacticalInfo"),
2018-02-22 13:36:50 +01:00
2018-02-01 14:41:09 +01:00
# pocsag wildcards
2020-02-22 23:47:13 +01:00
"{BIT}": bwPacket.get("bitrate"),
"{RIC}": bwPacket.get("ric"),
"{SRIC}": bwPacket.get("subric"),
"{SRICT}": bwPacket.get("subricText"),
"{MSG}": bwPacket.get("message"),
2018-02-22 13:36:50 +01:00
2018-02-01 14:41:09 +01:00
# zvei wildcards
2020-02-22 23:47:13 +01:00
"{TONE}": bwPacket.get("tone"),
2018-02-01 14:41:09 +01:00
2018-02-22 13:36:50 +01:00
# message for MSG packet is done in poc
}
for wildcard, field in _wildcards.items():
2020-02-22 23:47:13 +01:00
if field is not None:
message = message.replace(wildcard, field)
for wildcard, fieldName in _additionalWildcards.items():
field = bwPacket.get(fieldName)
2020-02-22 23:47:13 +01:00
if field is not None:
message = message.replace(wildcard, field)
2018-02-01 14:41:09 +01:00
return message