Mysql read library
This commit is contained in:
parent
b63bf51ae8
commit
b79870a3cd
138
mysql_config.py
138
mysql_config.py
@ -1,81 +1,81 @@
|
|||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
from mysql.connector import errorcode
|
||||||
|
|
||||||
class useMYSQL:
|
class useMYSQL:
|
||||||
|
#Init new object
|
||||||
def __init__(self, server,user,password,db):
|
def __init__(self, server,user,password,database):
|
||||||
self.server = server
|
self.server = server
|
||||||
|
|
||||||
self.user = user
|
self.user = user
|
||||||
self.password = password
|
self.password = password
|
||||||
self.db = db
|
self.database = database
|
||||||
|
|
||||||
|
#Connect
|
||||||
|
def con(self):
|
||||||
|
try:
|
||||||
|
self.db = mysql.connector.connect(
|
||||||
|
host=self.server,
|
||||||
|
user=self.user,
|
||||||
|
password=self.password,
|
||||||
|
database=self.database
|
||||||
|
)
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
||||||
|
return [False,'Username or password error']
|
||||||
|
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
||||||
|
return [False,'DB Does Not Exist']
|
||||||
|
else:
|
||||||
|
return [False,err]
|
||||||
|
|
||||||
|
return(True)
|
||||||
|
|
||||||
|
#Close DB connection
|
||||||
|
def close(self):
|
||||||
|
self.db.close()
|
||||||
|
|
||||||
|
#Get config from DB
|
||||||
|
def getConfig(self):
|
||||||
|
|
||||||
|
CONFIG = {}
|
||||||
|
CONFIG['SYSTEMS'] = {}
|
||||||
|
|
||||||
|
_cursor = self.db.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.db = mysql.connector.connect(
|
_cursor.execute("select * from repeaters where ENABLED=1 and MODE='MASTER'")
|
||||||
host=server,
|
except mysql.connector.Error as err:
|
||||||
user=user,
|
_cursor.close()
|
||||||
password=password,
|
return(False)
|
||||||
database=db
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def getConfig(self):
|
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) in _cursor:
|
||||||
|
|
||||||
_cursor = self.db.cursor()
|
CONFIG['SYSTEMS'].update({callsign: {
|
||||||
|
'MODE': mode,
|
||||||
_cursor.execute = ("SELECT * from repeaters where ENABLED=1")
|
'ENABLED': bool(enabled),
|
||||||
|
'REPEAT': bool(_repeat),
|
||||||
_res = cursor.fetchall()
|
'MAX_PEERS': int(max_peers),
|
||||||
|
'IP': ip,
|
||||||
print(_res)
|
'PORT': int(port),
|
||||||
|
'PASSPHRASE': passphrase,
|
||||||
#elif config.getboolean(section, 'ENABLED'):
|
'GROUP_HANGTIME': int(group_hangtime),
|
||||||
#if config.get(section, 'MODE') == 'PEER':
|
'USE_ACL': bool(use_acl),
|
||||||
#CONFIG['SYSTEMS'].update({section: {
|
'REG_ACL': reg_acl,
|
||||||
#'MODE': config.get(section, 'MODE'),
|
'SUB_ACL': sub_acl,
|
||||||
#'ENABLED': config.getboolean(section, 'ENABLED'),
|
'TG1_ACL': tgid_ts1_acl,
|
||||||
#'LOOSE': config.getboolean(section, 'LOOSE'),
|
'TG2_ACL': tgid_ts2_acl,
|
||||||
#'SOCK_ADDR': (gethostbyname(config.get(section, 'IP')), config.getint(section, 'PORT')),
|
'DEFAULT_UA_TIMER': int(default_ua_timer),
|
||||||
#'IP': gethostbyname(config.get(section, 'IP')),
|
'SINGLE_MODE': bool(single_mode),
|
||||||
#'PORT': config.getint(section, 'PORT'),
|
'VOICE_IDENT': bool(voice_ident)
|
||||||
#'MASTER_SOCKADDR': (gethostbyname(config.get(section, 'MASTER_IP')), config.getint(section, 'MASTER_PORT')),
|
}})
|
||||||
#'MASTER_IP': gethostbyname(config.get(section, 'MASTER_IP')),
|
CONFIG['SYSTEMS'][callsign].update({'PEERS': {}})
|
||||||
#'MASTER_PORT': config.getint(section, 'MASTER_PORT'),
|
|
||||||
#'PASSPHRASE': bytes(config.get(section, 'PASSPHRASE'), 'utf-8'),
|
|
||||||
#'CALLSIGN': bytes(config.get(section, 'CALLSIGN').ljust(8)[:8], 'utf-8'),
|
|
||||||
#'RADIO_ID': config.getint(section, 'RADIO_ID').to_bytes(4, 'big'),
|
|
||||||
#'RX_FREQ': bytes(config.get(section, 'RX_FREQ').ljust(9)[:9], 'utf-8'),
|
|
||||||
#'TX_FREQ': bytes(config.get(section, 'TX_FREQ').ljust(9)[:9], 'utf-8'),
|
|
||||||
#'TX_POWER': bytes(config.get(section, 'TX_POWER').rjust(2,'0'), 'utf-8'),
|
|
||||||
#'COLORCODE': bytes(config.get(section, 'COLORCODE').rjust(2,'0'), 'utf-8'),
|
|
||||||
#'LATITUDE': bytes(config.get(section, 'LATITUDE').ljust(8)[:8], 'utf-8'),
|
|
||||||
#'LONGITUDE': bytes(config.get(section, 'LONGITUDE').ljust(9)[:9], 'utf-8'),
|
|
||||||
#'HEIGHT': bytes(config.get(section, 'HEIGHT').rjust(3,'0'), 'utf-8'),
|
|
||||||
#'LOCATION': bytes(config.get(section, 'LOCATION').ljust(20)[:20], 'utf-8'),
|
|
||||||
#'DESCRIPTION': bytes(config.get(section, 'DESCRIPTION').ljust(19)[:19], 'utf-8'),
|
|
||||||
#'SLOTS': bytes(config.get(section, 'SLOTS'), 'utf-8'),
|
|
||||||
#'URL': bytes(config.get(section, 'URL').ljust(124)[:124], 'utf-8'),
|
|
||||||
#'SOFTWARE_ID': bytes(config.get(section, 'SOFTWARE_ID').ljust(40)[:40], 'utf-8'),
|
|
||||||
#'PACKAGE_ID': bytes(config.get(section, 'PACKAGE_ID').ljust(40)[:40], 'utf-8'),
|
|
||||||
#'GROUP_HANGTIME': config.getint(section, 'GROUP_HANGTIME'),
|
|
||||||
#'OPTIONS': bytes(config.get(section, 'OPTIONS'), 'utf-8'),
|
|
||||||
#'USE_ACL': config.getboolean(section, 'USE_ACL'),
|
|
||||||
#'SUB_ACL': config.get(section, 'SUB_ACL'),
|
|
||||||
#'TG1_ACL': config.get(section, 'TGID_TS1_ACL'),
|
|
||||||
#'TG2_ACL': config.get(section, 'TGID_TS2_ACL')
|
|
||||||
#}})
|
|
||||||
#CONFIG['SYSTEMS'][section].update({'STATS': {
|
|
||||||
#'CONNECTION': 'NO', # NO, RTPL_SENT, AUTHENTICATED, CONFIG-SENT, YES
|
|
||||||
#'CONNECTED': None,
|
|
||||||
#'PINGS_SENT': 0,
|
|
||||||
#'PINGS_ACKD': 0,
|
|
||||||
#'NUM_OUTSTANDING': 0,
|
|
||||||
#'PING_OUTSTANDING': False,
|
|
||||||
#'LAST_PING_TX_TIME': 0,
|
|
||||||
#'LAST_PING_ACK_TIME': 0,
|
|
||||||
#}})
|
|
||||||
|
|
||||||
|
return(CONFIG)
|
||||||
|
|
||||||
|
|
||||||
|
#For testing
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sql = useMYSQL("87.117.229.39","hblink","project999","hblink")
|
|
||||||
|
|
||||||
sql.getConfig()
|
sql = useMYSQL("ip","user","pass","db")
|
||||||
|
|
||||||
|
sql.con()
|
||||||
|
|
||||||
|
print( sql.getConfig())
|
||||||
|
Loading…
Reference in New Issue
Block a user