BW3-Core/plugin/mysql.py
Luflosi d4dcc75711
Avoid "DeprecationWarning: invalid escape sequence"
Without this change, many warnings like this will be generated while running pytest:
```
test/test_template.py:3
  /build/source/test/test_template.py:3: DeprecationWarning: invalid escape sequence '\/'
    """!
```
This can also be seen when manually running python with warnings enabled.

This happens because the comment uses a multiline string and Python interprets the backslash in the logo as an escape character and complains that \/ is not a valid escape sequence. To fix this, prepend the string with the letter r to indicate that the backslash should be treated as a literal character, see https://docs.python.org/3/reference/lexical_analysis.html#index-20.
I also applied this change to all the comment strings since that shouldn't break anything and to establish it as a pattern for the future so this problem hopefully never happens again.

This is what I did specifically:
- Change the comment at the top of bw_client.py and bw_server.py to start with `"""!` since that seems to be the pattern here
- Search-and-Replace all occurances of `"""!` with `r"""!`
- Manually change the strings in `logoToLog()` in boswatch/utils/header.py
2023-09-19 17:49:09 +02:00

189 lines
7.4 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: mysql.py
@date: 15.02.2021
@author: Jan Speller
@description: Mysql Plugin
"""
import logging
from plugin.pluginBase import PluginBase
# ###################### #
# Custom plugin includes #
import mysql.connector
from datetime import datetime
# ###################### #
logging.debug("- %s loaded", __name__)
class BoswatchPlugin(PluginBase):
r"""!Description of the Plugin"""
def __init__(self, config):
r"""!Do not change anything here!"""
super().__init__(__name__, config) # you can access the config class on 'self.config'
def onLoad(self):
r"""!Called by import of the plugin
Remove if not implemented"""
self.sqlInserts = {
"pocsag": "INSERT INTO boswatch (packetTimestamp, packetMode, pocsag_ric, pocsag_subric, pocsag_subricText, pocsag_message, pocsag_bitrate, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"zvei": "INSERT INTO boswatch (packetTimestamp, packetMode, zvei_tone, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"fms": "INSERT INTO boswatch (packetTimestamp, packetMode, fms_fms, fms_service, fms_country, fms_location, fms_vehicle, fms_status, fms_direction, fms_directionText, fms_tacticalInfo, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"msg": "INSERT INTO boswatch (packetTimestamp, packetMode, serverName, serverVersion, serverBuildDate, serverBranch, clientName, clientIP, clientVersion, clientBuildDate, clientBranch, inputSource, frequency) VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
}
self.connection = mysql.connector.connect(
host=self.config.get("host"),
user=self.config.get("user"),
password=self.config.get("password"),
database=self.config.get("database"),
)
self.cursor = self.connection.cursor()
self.cursor.execute("SHOW TABLES LIKE 'boswatch'")
if self.cursor.fetchone() is None:
with open('init_db.sql') as f:
for stmnt in f.read().split(';'):
self.cursor.execute(stmnt)
self.connection.commit()
self.cursor.close()
def setup(self):
r"""!Called before alarm
Remove if not implemented"""
try:
self.connection.ping(reconnect=True, attempts=3, delay=2)
except mysql.connector.Error:
logging.warning("Connection was down, trying to reconnect...")
self.onLoad()
self.cursor = self.connection.cursor()
def fms(self, bwPacket):
r"""!Called on FMS alarm
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("fms"),
bwPacket.get("service"),
bwPacket.get("country"),
bwPacket.get("location"),
bwPacket.get("vehicle"),
bwPacket.get("status"),
bwPacket.get("direction"),
bwPacket.get("directionText"),
bwPacket.get("tacticalInfo"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("fms"), val)
def pocsag(self, bwPacket):
r"""!Called on POCSAG alarm
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("ric"),
bwPacket.get("subric"),
bwPacket.get("subricText"),
bwPacket.get("message"),
bwPacket.get("bitrate"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("pocsag"), val)
def zvei(self, bwPacket):
r"""!Called on ZVEI alarm
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("tone"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("pocsag"), val)
def msg(self, bwPacket):
r"""!Called on MSG packet
@param bwPacket: bwPacket instance
Remove if not implemented"""
val = (
datetime.fromtimestamp(float(bwPacket.get("timestamp"))),
bwPacket.get("mode"),
bwPacket.get("serverName"),
bwPacket.get("serverVersion"),
bwPacket.get("serverBuildDate"),
bwPacket.get("serverBranch"),
bwPacket.get("clientName"),
bwPacket.get("clientIP"),
bwPacket.get("clientVersion"),
bwPacket.get("clientBuildDate"),
bwPacket.get("clientBranch"),
bwPacket.get("inputSource"),
bwPacket.get("frequency")
)
self.cursor.execute(self.sqlInserts.get("msg"), val)
def teardown(self):
r"""!Called after alarm
Remove if not implemented"""
self.connection.commit()
self.cursor.close()
def onUnload(self):
r"""!Called by destruction of the plugin
Remove if not implemented"""
self.connection.close()