mirror of
https://github.com/Doctorado-ML/Stree_datasets.git
synced 2025-08-17 08:26:02 +00:00
Add tunnel to mysql
add any kernel to script generator
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
host=<server>
|
||||
port=3306
|
||||
user=stree
|
||||
password=<password>
|
||||
database=stree_experiments
|
4
experimentation/.tunnel.dist
Normal file
4
experimentation/.tunnel.dist
Normal file
@@ -0,0 +1,4 @@
|
||||
ssh_address_or_host=(<host>, <port>)
|
||||
ssh_username=<user>
|
||||
ssh_private_key=<path_to>/id_rsa
|
||||
remote_bind_address=('127.0.0.1', 3306)
|
@@ -3,9 +3,43 @@ import sqlite3
|
||||
from datetime import datetime
|
||||
from abc import ABC
|
||||
from typing import List
|
||||
|
||||
import mysql.connector
|
||||
from ast import literal_eval as make_tuple
|
||||
from sshtunnel import SSHTunnelForwarder
|
||||
from .Models import ModelBase
|
||||
from .Utils import TextColor, MySQL
|
||||
from .Utils import TextColor
|
||||
|
||||
|
||||
class MySQL:
|
||||
def __init__(self):
|
||||
self.server = None
|
||||
|
||||
def get_connection(self):
|
||||
config_db = dict()
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(os.path.join(dir_path, ".myconfig")) as f:
|
||||
for line in f.read().splitlines():
|
||||
key, value = line.split("=")
|
||||
config_db[key] = value
|
||||
config_tunnel = dict()
|
||||
with open(os.path.join(dir_path, ".tunnel")) as f:
|
||||
for line in f.read().splitlines():
|
||||
key, value = line.split("=")
|
||||
config_tunnel[key] = value
|
||||
config_tunnel["remote_bind_address"] = make_tuple(
|
||||
config_tunnel["remote_bind_address"]
|
||||
)
|
||||
config_tunnel["ssh_address_or_host"] = make_tuple(
|
||||
config_tunnel["ssh_address_or_host"]
|
||||
)
|
||||
self.server = SSHTunnelForwarder(**config_tunnel)
|
||||
self.server.daemon_forward_servers = True
|
||||
self.server.start()
|
||||
config_db["port"] = self.server.local_bind_port
|
||||
return mysql.connector.connect(**config_db)
|
||||
|
||||
def close(self):
|
||||
self.server.close()
|
||||
|
||||
|
||||
class BD(ABC):
|
||||
@@ -108,7 +142,8 @@ class BD(ABC):
|
||||
:param record: data to insert in database
|
||||
:type record: dict
|
||||
"""
|
||||
database = MySQL.get_connection()
|
||||
dbh = MySQL()
|
||||
database = dbh.get_connection()
|
||||
command_insert = (
|
||||
"replace into results (date, time, type, accuracy, "
|
||||
"dataset, classifier, norm, stand, parameters) values (%s, %s, "
|
||||
@@ -131,6 +166,7 @@ class BD(ABC):
|
||||
cursor = database.cursor()
|
||||
cursor.execute(command_insert, values)
|
||||
database.commit()
|
||||
dbh.close()
|
||||
|
||||
def execute(self, command: str) -> None:
|
||||
c = self._con.cursor()
|
||||
|
@@ -1,7 +1,3 @@
|
||||
import os
|
||||
import mysql.connector
|
||||
|
||||
|
||||
class TextColor:
|
||||
BLUE = "\033[94m"
|
||||
CYAN = "\033[96m"
|
||||
@@ -18,15 +14,3 @@ class TextColor:
|
||||
ENDC = "\033[0m"
|
||||
BOLD = "\033[1m"
|
||||
UNDERLINE = "\033[4m"
|
||||
|
||||
|
||||
class MySQL:
|
||||
@staticmethod
|
||||
def get_connection():
|
||||
config = dict()
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
with open(os.path.join(dir_path, ".myconfig")) as f:
|
||||
for line in f.read().splitlines():
|
||||
key, value = line.split("=")
|
||||
config[key] = value
|
||||
return mysql.connector.connect(**config)
|
||||
|
Reference in New Issue
Block a user