1
|
|
|
""" |
2
|
|
|
PyStratum |
3
|
|
|
|
4
|
|
|
Copyright 2015-2016 Set Based IT Consultancy |
5
|
|
|
|
6
|
|
|
Licence MIT |
7
|
|
|
""" |
8
|
|
|
from configparser import ConfigParser |
9
|
|
|
|
10
|
|
|
from enarksh.C import C |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Credentials: |
14
|
|
|
""" |
15
|
|
|
Singleton for reading MySQL's credentials. |
16
|
|
|
""" |
17
|
|
|
instance = None |
18
|
|
|
""" |
19
|
|
|
The singleton of this class. |
20
|
|
|
|
21
|
|
|
:type: None|enarksh.Credentials.Credentials |
22
|
|
|
""" |
23
|
|
|
|
24
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
25
|
|
|
@staticmethod |
26
|
|
|
def get(): |
27
|
|
|
""" |
28
|
|
|
Returns the singleton of this class. |
29
|
|
|
|
30
|
|
|
:rtype: enarksh.Credentials.Credentials |
31
|
|
|
""" |
32
|
|
|
if not Credentials.instance: |
33
|
|
|
Credentials.instance = Credentials() |
34
|
|
|
|
35
|
|
|
return Credentials.instance |
36
|
|
|
|
37
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
38
|
|
|
def __init__(self): |
39
|
|
|
""" |
40
|
|
|
Object constructor. |
41
|
|
|
""" |
42
|
|
|
self.__config = ConfigParser() |
43
|
|
|
self.__config.read(C.CREDENTIALS_CFG) |
44
|
|
|
|
45
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
46
|
|
|
def get_host(self): |
47
|
|
|
""" |
48
|
|
|
Returns the hostname of the MySQL instance. |
49
|
|
|
|
50
|
|
|
:rtype: str |
51
|
|
|
""" |
52
|
|
|
return self.__config.get('database', 'host', fallback='localhost') |
53
|
|
|
|
54
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
55
|
|
|
def get_user(self): |
56
|
|
|
""" |
57
|
|
|
Returns the user for connecting to the MySQL instance. |
58
|
|
|
|
59
|
|
|
:rtype: str |
60
|
|
|
""" |
61
|
|
|
return self.__config.get('database', 'user') |
62
|
|
|
|
63
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
64
|
|
|
def get_password(self): |
65
|
|
|
""" |
66
|
|
|
Returns the password for connecting to the MySQL instance. |
67
|
|
|
|
68
|
|
|
:rtype: str |
69
|
|
|
""" |
70
|
|
|
return self.__config.get('database', 'password') |
71
|
|
|
|
72
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
73
|
|
|
def get_database(self): |
74
|
|
|
""" |
75
|
|
|
Returns the database or schema name. |
76
|
|
|
|
77
|
|
|
:rtype: str |
78
|
|
|
""" |
79
|
|
|
return self.__config.get('database', 'database') |
80
|
|
|
|
81
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
82
|
|
|
def get_port(self): |
83
|
|
|
""" |
84
|
|
|
Returns the port to the MySQL instance. |
85
|
|
|
|
86
|
|
|
:rtype: int |
87
|
|
|
""" |
88
|
|
|
return self.__config.getint('database', 'port', fallback=3306) |
89
|
|
|
|
90
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
91
|
|
|
|