-
Notifications
You must be signed in to change notification settings - Fork 20
/
client_config.py
155 lines (128 loc) · 5.14 KB
/
client_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""ClientConfig deals with the args passed to the MariaDB client"""
# Copyright (c) MariaDB Foundation.
# Distributed under the terms of the Modified BSD License.
import os
import json
class ClientConfig:
def __init__(self, log, name="mariadb_config.json"):
self.log = log
self.config_name = name
datadir = "/tmp/mariadb_kernel/datadir"
pidfile = "/tmp/mariadb_kernel/mysqld.pid"
socketfile = "/tmp/mariadb_kernel/mysqld.sock"
if "NB_USER" in os.environ:
datadir = os.path.join("/home/", os.environ["NB_USER"], "work", ".db")
self.default_config = {
"user": "root",
"host": "localhost",
"socket": socketfile,
"port": "3306",
"password": "",
"server_datadir": datadir, # Server specific option
"server_pid": pidfile, # Server specific option
"start_server": "True",
"client_bin": "mysql",
"server_bin": "mysqld",
"server_name": "MariaDB",
"db_init_bin": "mysql_install_db",
"database": None,
"extra_server_config": [
"--no-defaults",
"--skip_log_error",
],
"extra_db_init_config": [
"--auth-root-authentication-method=normal",
],
"debug": "False",
"code_completion": "True",
}
self._load_config()
def _load_config(self):
path = self._config_path()
self.log.info(f"Loading config file at {path}...")
cfg = {}
using_default = False
try:
with open(path, "r", encoding="utf-8") as file:
cfg = json.load(file)
except (OSError, json.JSONDecodeError) as exception:
if isinstance(exception, OSError):
self.log.info(
f"Config file {self.config_name} at {path} " "does not exist"
)
if isinstance(exception, json.JSONDecodeError):
self.log.info(
f"Config file {self.config_name} at {path} "
f"is not valid JSON: {exception}"
)
using_default = True
# We should abort loading the custom config if the user passes
# an unsupported option
customk = cfg.keys()
defaultk = self.default_config.keys()
if len(customk - defaultk) > 0:
self.log.info(
f"Config file {self.config_name} at {path} "
f"contains unsupported options: {customk - defaultk}"
)
using_default = True
if using_default:
self.log.info(
f"Using default config: {json.dumps(self.default_config, indent=4)}"
)
return
self.default_config.update(cfg)
def _config_path(self):
default_dir = os.path.join(os.path.expanduser("~"), ".jupyter")
custom_dir = os.environ.get("JUPYTER_CONFIG_DIR")
if custom_dir:
default_dir = custom_dir
return os.path.join(default_dir, self.config_name)
def get_args(self):
params = ""
keys = ["user", "host", "port", "password", "socket", "database"]
for key in keys:
value = self.default_config[key]
if value is not None:
params += f"--{key}={value} "
# Disable progress reports in statements like LOAD DATA
params += "--disable-progress-reports"
return params
def get_server_args(self):
params = []
params.extend(self.default_config["extra_server_config"])
# Use same connection config for both server and client
params.append(f"--socket={self.default_config['socket']}")
params.append(f"--port={self.default_config['port']}")
params.append(f"--bind-address={self.default_config['host']}")
# Server specific config
params.append(f"--datadir={self.default_config['server_datadir']}")
params.append(f"--pid-file={self.default_config['server_pid']}")
return params
def get_init_args(self):
params = []
params.extend(self.get_server_args())
params.extend(self.default_config["extra_db_init_config"])
return params
def get_server_paths(self):
return [
os.path.dirname(self.default_config["socket"]),
os.path.dirname(self.default_config["server_datadir"]),
os.path.dirname(self.default_config["server_pid"]),
]
def get_server_pidfile(self):
return self.default_config["server_pid"]
def start_server(self):
return self.default_config["start_server"] == "True"
def client_bin(self):
return self.default_config["client_bin"]
def server_bin(self):
return self.default_config["server_bin"]
def db_init_bin(self):
return self.default_config["db_init_bin"]
def debug_logging(self):
return self.default_config["debug"] == "True"
def autocompletion_enabled(self):
return self.default_config["code_completion"] == "True"
def server_name(self):
return self.default_config["server_name"]