2020-09-27 18:52:58 -04:00
import mysql . connector
2020-09-29 08:42:07 -04:00
from mysql . connector import errorcode
2020-10-19 17:46:55 -04:00
#import mysql.connector.pooling
2020-09-27 18:52:58 -04:00
2021-05-24 17:32:54 -04:00
# Does anybody read this stuff? There's a PEP somewhere that says I should do this.
__author__ = ' Simon Adlem - G7RZU '
__copyright__ = ' Copyright (c) Simon Adlem, G7RZU 2020,2021 '
__credits__ = ' Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman, N4IRR; Jonathan Naylor, G4KLX; Hans Barthen, DL5DI; Torsten Shultze, DG1HT; Jon Lee, G4TSN; Norman Williams, M6NBP '
__license__ = ' GNU GPLv3 '
__maintainer__ = ' Simon Adlem G7RZU '
__email__ = ' simon@gb7fr.org.uk '
2020-09-27 18:52:58 -04:00
class useMYSQL :
2020-09-29 08:42:07 -04:00
#Init new object
2021-01-22 08:09:28 -05:00
def __init__ ( self , server , user , password , database , table , logger ) :
2020-09-27 18:52:58 -04:00
self . server = server
self . user = user
self . password = password
2020-09-29 08:42:07 -04:00
self . database = database
2021-01-22 08:09:28 -05:00
self . table = table
2020-09-30 15:46:35 -04:00
self . logger = logger
2020-09-29 08:42:07 -04:00
#Connect
def con ( self ) :
2020-09-30 15:46:35 -04:00
logger = self . logger
2020-09-29 08:42:07 -04:00
try :
self . db = mysql . connector . connect (
host = self . server ,
user = self . user ,
password = self . password ,
2020-09-30 15:46:35 -04:00
database = self . database ,
2020-10-19 17:46:55 -04:00
# pool_name = "hblink_master",
# pool_size = 2
2020-09-29 08:42:07 -04:00
)
except mysql . connector . Error as err :
if err . errno == errorcode . ER_ACCESS_DENIED_ERROR :
2020-09-30 15:46:35 -04:00
logger . info ( ' (MYSQL) username or password error ' )
return ( False )
2020-09-29 08:42:07 -04:00
elif err . errno == errorcode . ER_BAD_DB_ERROR :
2020-09-30 15:46:35 -04:00
logger . info ( ' (MYSQL) DB Error ' )
return ( False )
2020-09-29 08:42:07 -04:00
else :
2020-10-19 16:02:04 -04:00
logger . info ( ' (MYSQL) error: %s ' , err )
2020-09-30 15:46:35 -04:00
return ( False )
2020-09-29 08:42:07 -04:00
return ( True )
#Close DB connection
def close ( self ) :
self . db . close ( )
#Get config from DB
def getConfig ( self ) :
2020-09-27 18:52:58 -04:00
2020-09-29 08:42:07 -04:00
CONFIG = { }
CONFIG [ ' SYSTEMS ' ] = { }
2020-09-27 18:52:58 -04:00
2020-09-29 08:42:07 -04:00
_cursor = self . db . cursor ( )
try :
2021-01-22 08:09:28 -05:00
_cursor . execute ( " select * from {} where MODE= ' MASTER ' " . format ( self . table ) )
2020-09-29 08:42:07 -04:00
except mysql . connector . Error as err :
_cursor . close ( )
2020-09-30 15:46:35 -04:00
logger . info ( ' (MYSQL) error, problem with cursor execute ' )
2020-09-29 09:43:17 -04:00
raise Exception ( ' Problem with cursor execute ' )
2020-09-27 18:52:58 -04:00
2021-05-22 17:56:20 -04:00
for ( callsign , mode , enabled , _repeat , max_peers , export_ambe , ip , port , passphrase , group_hangtime , use_acl , reg_acl , sub_acl , tgid_ts1_acl , tgid_ts2_acl , default_ua_timer , single_mode , voice_ident , ts1_static , ts2_static , default_reflector , announce_lang ) in _cursor :
2020-10-02 12:13:10 -04:00
try :
CONFIG [ ' SYSTEMS ' ] . update ( { callsign : {
' MODE ' : mode ,
' ENABLED ' : bool ( enabled ) ,
' REPEAT ' : bool ( _repeat ) ,
' MAX_PEERS ' : int ( max_peers ) ,
' IP ' : ip ,
' PORT ' : int ( port ) ,
2020-10-03 17:03:14 -04:00
' PASSPHRASE ' : bytes ( passphrase , ' utf-8 ' ) ,
2020-10-02 12:13:10 -04:00
' GROUP_HANGTIME ' : int ( group_hangtime ) ,
' USE_ACL ' : bool ( use_acl ) ,
' REG_ACL ' : reg_acl ,
' SUB_ACL ' : sub_acl ,
' TG1_ACL ' : tgid_ts1_acl ,
' TG2_ACL ' : tgid_ts2_acl ,
' DEFAULT_UA_TIMER ' : int ( default_ua_timer ) ,
' SINGLE_MODE ' : bool ( single_mode ) ,
' VOICE_IDENT ' : bool ( voice_ident ) ,
' TS1_STATIC ' : ts1_static ,
' TS2_STATIC ' : ts2_static ,
2021-02-21 11:06:25 -05:00
' DEFAULT_REFLECTOR ' : int ( default_reflector ) ,
' GENERATOR ' : int ( 1 ) ,
2021-05-22 17:56:20 -04:00
' ANNOUNCEMENT_LANGUAGE ' : announce_lang
2020-10-02 12:13:10 -04:00
} } )
CONFIG [ ' SYSTEMS ' ] [ callsign ] . update ( { ' PEERS ' : { } } )
except TypeError :
logger . info ( ' (MYSQL) Problem with data from MySQL - TypeError, carrying on to next row ' )
2020-09-27 18:52:58 -04:00
2020-09-30 15:46:35 -04:00
return ( CONFIG [ ' SYSTEMS ' ] )
2020-09-29 08:42:07 -04:00
#For testing
2020-09-27 18:52:58 -04:00
if __name__ == ' __main__ ' :
2020-09-29 08:42:07 -04:00
sql = useMYSQL ( " ip " , " user " , " pass " , " db " )
sql . con ( )
print ( sql . getConfig ( ) )