def __init__(self, path="/home/spennington/.mylogin.cnf", group="remote", password=None, host="localhost", user="******", db="jobsearch"): self.connection = None self.host = host self.user = user self.database = db self.port = 3306 if password is None and path and isinstance(path, str) and\ path.find('mylogin.cnf') < 0: self.connection = mysqldb.connect(option_files=path, option_groups=group, use_unicode=True, charset="utf8", collation="utf8_general_ci", use_pure=True, db=self.database) else: if password is None and path and isinstance(path, str) and\ path.find('mylogin.cnf') >= 0: conf = myloginpath.parse(group) if conf and isinstance(conf, dict): # print(conf) self.user = conf['user'] self.host = conf['host'] password = conf['password'].replace('"', '') if 'port' in conf.keys(): self.port = conf['port'] else: raise ValueError("There was a problem with .mylogin.cnf") # conf['password'] = conf['password'].replace("\"", '') # conf['password'] = conf['password'].encode(encoding='utf-8') try: self.connection = mysqldb.connect(user=self.user, host=self.host, password=password, port=self.port, use_pure=False, ssl_disabled=True, db=self.database) except mysqldb.Error as err: if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print('(1): ') elif err.errno == errorcode.ER_BAD_DB_ERROR: print('(2): ') else: print('U: ' + str(err.errno)) print(err) raise ValueError("Failed to connect to MySQL DB") self.cursor = self.connection.cursor() if self.database is not None: self.cursor.execute("USE " + self.database + ";")
def test_normal(): expected = { "host": "testhost", "password": "******", "port": 1234, "socket": "testsocket", "user": "******", } actual = myloginpath.parse("test", "test/test.mylogin.cnf") assert expected == actual
def load_from_login_path(): p = argparse.ArgumentParser() p.add_argument('--login-path') args, rest = p.parse_known_args() if args.login_path: try: return myloginpath.parse(args.login_path), rest except NoSectionError as err: print(err) except FileNotFoundError as err: print(err) return {}, rest
def getMySqlConnection(usr='******'): global chamelenonDb ipaddress = "127.0.0.1" charset = "utf8mb4" curtype = pymysql.cursors.DictCursor conf = myloginpath.parse(usr) sqlCon = pymysql.connect(**conf, host=ipaddress, charset=charset, cursorclass=curtype) sqlCursor = sqlCon.cursor() return sqlCon, sqlCursor
def get_mysql_login(self, login_path): try: login_dict = myloginpath.parse(login_path) except (configparser.Error, KeyError, TypeError) as e: raise LoginNotFound( 'Unable to get MySQL connection string for "{}". Error: {}'. format(login_path, repr(e))) login_dict = { **{CONFIG_TO_CONNECT.get(k, k): v for k, v in login_dict.items()}, 'db': login_path, } self.log_kv(f'Found MySQL login for "{login_path}"', { **login_dict, "pw": "***" }) return login_dict
#!C:\Users\moconnor\.virtualenvs\test-YVpzeIj4\Scripts\python.exe import pymysql import sys import configparser import myloginpath from configparser import RawConfigParser from os import getenv conf = myloginpath.parse('client') print(**conf) #_____________________________________________________________________________________ db = pymysql.connect(**conf, host='localhost', db='testdb') #-------------- #-------------- cursor = db.cursor() # execute SQL query using execute() method. cursor.execute("SELECT database()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print("Database: %s " % data) # disconnect from server db.close()
def test_special(): expected = {"host": 'host with " quote'} actual = myloginpath.parse("test", "test/test_special.mylogin.cnf") assert expected == actual