1
1
"""
2
2
Database connection module for SwSS
3
3
"""
4
- from . import logger
5
- from .interface import DBInterface
6
4
import os
7
5
import json
6
+ from . import logger
7
+ from .interface import DBInterface
8
8
9
9
# FIXME: Convert to metaclasses when Py2 support is removed. Metaclasses have unique interfaces to Python2/Python3.
10
10
@@ -28,10 +28,10 @@ def load_sonic_global_db_config(global_db_file_path=SONIC_DB_GLOBAL_CONFIG_FILE,
28
28
"""
29
29
Parse and load the global database config json file
30
30
"""
31
- if SonicDBConfig ._sonic_db_global_config_init == True :
31
+ if SonicDBConfig ._sonic_db_global_config_init :
32
32
return
33
33
34
- if os .path .isfile (global_db_file_path ) == True :
34
+ if os .path .isfile (global_db_file_path ):
35
35
global_db_config_dir = os .path .dirname (global_db_file_path )
36
36
with open (global_db_file_path , "r" ) as read_file :
37
37
all_ns_dbs = json .load (read_file )
@@ -40,15 +40,15 @@ def load_sonic_global_db_config(global_db_file_path=SONIC_DB_GLOBAL_CONFIG_FILE,
40
40
# If the user already invoked load_sonic_db_config() explicitly to load the
41
41
# database_config.json file for current namesapce, skip loading the file
42
42
# referenced here in the global config file.
43
- if SonicDBConfig ._sonic_db_config_init == True :
43
+ if SonicDBConfig ._sonic_db_config_init :
44
44
continue
45
45
ns = ''
46
46
else :
47
47
ns = entry ['namespace' ]
48
48
49
49
# If API is called with a namespace parameter, load the json file only for that namespace.
50
- if namespace is not None and ns != namespace :
51
- continue ;
50
+ if namespace is not None and ns != namespace :
51
+ continue
52
52
53
53
# Check if _sonic_db_config already have this namespace present
54
54
if ns in SonicDBConfig ._sonic_db_config :
@@ -59,23 +59,23 @@ def load_sonic_global_db_config(global_db_file_path=SONIC_DB_GLOBAL_CONFIG_FILE,
59
59
db_include_file = os .path .join (global_db_config_dir , entry ['include' ])
60
60
61
61
# Not finding the database_config.json file for the namespace
62
- if os .path .isfile (db_include_file ) == False :
62
+ if not os .path .isfile (db_include_file ):
63
63
msg = "'{}' file is not found !!" .format (db_include_file )
64
64
logger .warning (msg )
65
65
continue
66
66
67
67
# As we load the database_config.json file for current namesapce,
68
68
# set the _sonic_db_config_init flag to True to prevent loading again
69
69
# by the API load_sonic_db_config()
70
- if ns is '' :
70
+ if not ns :
71
71
SonicDBConfig ._sonic_db_config_init = True
72
72
73
73
with open (db_include_file , "r" ) as inc_file :
74
74
SonicDBConfig ._sonic_db_config [ns ] = json .load (inc_file )
75
75
76
76
# If API is called with a namespace parameter,we break here as we loaded the json file.
77
- if namespace is not None and ns == namespace :
78
- break ;
77
+ if namespace is not None and ns == namespace :
78
+ break
79
79
80
80
SonicDBConfig ._sonic_db_global_config_init = True
81
81
@@ -84,11 +84,11 @@ def load_sonic_db_config(sonic_db_file_path=SONIC_DB_CONFIG_FILE):
84
84
"""
85
85
Get multiple database config from the database_config.json
86
86
"""
87
- if SonicDBConfig ._sonic_db_config_init == True :
87
+ if SonicDBConfig ._sonic_db_config_init :
88
88
return
89
89
90
90
try :
91
- if os .path .isfile (sonic_db_file_path ) == False :
91
+ if not os .path .isfile (sonic_db_file_path ):
92
92
msg = "'{}' is not found, it is not expected in production devices!!" .format (sonic_db_file_path )
93
93
logger .warning (msg )
94
94
sonic_db_file_path = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'config' , 'database_config.json' )
@@ -110,12 +110,12 @@ def namespace_validation(namespace):
110
110
raise RuntimeError (msg )
111
111
112
112
# Check if the global config is loaded entirely or for the namespace
113
- if namespace != '' and SonicDBConfig ._sonic_db_global_config_init == False :
113
+ if namespace != '' and not SonicDBConfig ._sonic_db_global_config_init :
114
114
msg = "Load the global DB config first using API load_sonic_global_db_config"
115
115
logger .warning (msg )
116
116
raise RuntimeError (msg )
117
117
118
- if SonicDBConfig ._sonic_db_config_init == False :
118
+ if not SonicDBConfig ._sonic_db_config_init :
119
119
SonicDBConfig .load_sonic_db_config ()
120
120
121
121
if namespace not in SonicDBConfig ._sonic_db_config :
@@ -133,10 +133,10 @@ def EMPTY_NAMESPACE(ns):
133
133
@staticmethod
134
134
def db_name_validation (db_name , namespace = None ):
135
135
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
136
- if SonicDBConfig ._sonic_db_config_init == False :
136
+ if not SonicDBConfig ._sonic_db_config_init :
137
137
SonicDBConfig .load_sonic_db_config ()
138
138
SonicDBConfig .namespace_validation (namespace )
139
- db = SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ]
139
+ db = SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ]
140
140
if db_name not in db :
141
141
msg = "{} is not a valid database name in configuration file" .format (db_name )
142
142
logger .warning (msg )
@@ -145,7 +145,7 @@ def db_name_validation(db_name, namespace=None):
145
145
@staticmethod
146
146
def inst_name_validation (inst_name , namespace = None ):
147
147
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
148
- if SonicDBConfig ._sonic_db_config_init == False :
148
+ if not SonicDBConfig ._sonic_db_config_init :
149
149
SonicDBConfig .load_sonic_db_config ()
150
150
SonicDBConfig .namespace_validation (namespace )
151
151
instances = SonicDBConfig ._sonic_db_config [namespace ]["INSTANCES" ]
@@ -157,21 +157,21 @@ def inst_name_validation(inst_name, namespace=None):
157
157
@staticmethod
158
158
def get_dblist (namespace = None ):
159
159
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
160
- if SonicDBConfig ._sonic_db_config_init == False :
160
+ if not SonicDBConfig ._sonic_db_config_init :
161
161
SonicDBConfig .load_sonic_db_config ()
162
162
SonicDBConfig .namespace_validation (namespace )
163
163
return SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ].keys ()
164
164
165
165
@staticmethod
166
166
def get_ns_list ():
167
- if SonicDBConfig ._sonic_db_config_init == False :
167
+ if not SonicDBConfig ._sonic_db_config_init :
168
168
SonicDBConfig .load_sonic_db_config ()
169
169
return SonicDBConfig ._sonic_db_config .keys ()
170
170
171
171
@staticmethod
172
172
def get_instance (db_name , namespace = None ):
173
173
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
174
- if SonicDBConfig ._sonic_db_config_init == False :
174
+ if not SonicDBConfig ._sonic_db_config_init :
175
175
SonicDBConfig .load_sonic_db_config ()
176
176
SonicDBConfig .db_name_validation (db_name , namespace )
177
177
inst_name = SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ][db_name ]["instance" ]
@@ -181,44 +181,44 @@ def get_instance(db_name, namespace=None):
181
181
@staticmethod
182
182
def get_instancelist (namespace = None ):
183
183
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
184
- if SonicDBConfig ._sonic_db_config_init == False :
184
+ if not SonicDBConfig ._sonic_db_config_init :
185
185
SonicDBConfig .load_sonic_db_config ()
186
186
SonicDBConfig .namespace_validation (namespace )
187
187
return SonicDBConfig ._sonic_db_config [namespace ]["INSTANCES" ]
188
188
189
189
@staticmethod
190
190
def get_socket (db_name , namespace = None ):
191
191
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
192
- if SonicDBConfig ._sonic_db_config_init == False :
192
+ if not SonicDBConfig ._sonic_db_config_init :
193
193
SonicDBConfig .load_sonic_db_config ()
194
194
return SonicDBConfig .get_instance (db_name , namespace )["unix_socket_path" ]
195
195
196
196
@staticmethod
197
197
def get_hostname (db_name , namespace = None ):
198
198
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
199
- if SonicDBConfig ._sonic_db_config_init == False :
199
+ if not SonicDBConfig ._sonic_db_config_init :
200
200
SonicDBConfig .load_sonic_db_config ()
201
201
return SonicDBConfig .get_instance (db_name , namespace )["hostname" ]
202
202
203
203
@staticmethod
204
204
def get_port (db_name , namespace = None ):
205
205
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
206
- if SonicDBConfig ._sonic_db_config_init == False :
206
+ if not SonicDBConfig ._sonic_db_config_init :
207
207
SonicDBConfig .load_sonic_db_config ()
208
208
return SonicDBConfig .get_instance (db_name , namespace )["port" ]
209
209
210
210
@staticmethod
211
211
def get_dbid (db_name , namespace = None ):
212
212
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
213
- if SonicDBConfig ._sonic_db_config_init == False :
213
+ if not SonicDBConfig ._sonic_db_config_init :
214
214
SonicDBConfig .load_sonic_db_config ()
215
215
SonicDBConfig .db_name_validation (db_name , namespace )
216
216
return SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ][db_name ]["id" ]
217
217
218
218
@staticmethod
219
219
def get_separator (db_name , namespace = None ):
220
220
namespace = SonicDBConfig .EMPTY_NAMESPACE (namespace )
221
- if SonicDBConfig ._sonic_db_config_init == False :
221
+ if not SonicDBConfig ._sonic_db_config_init :
222
222
SonicDBConfig .load_sonic_db_config ()
223
223
SonicDBConfig .db_name_validation (db_name , namespace )
224
224
return SonicDBConfig ._sonic_db_config [namespace ]["DATABASES" ][db_name ]["separator" ]
@@ -228,7 +228,7 @@ def __init__(self, use_unix_socket_path=False, namespace=None, **kwargs):
228
228
self .dbintf = DBInterface (** kwargs )
229
229
self .use_unix_socket_path = use_unix_socket_path
230
230
231
- """If the user don't give the namespace as input, it refers to the local namespace
231
+ """If the user don't give the namespace as input, it refers to the local namespace
232
232
where this application is run. (It could be a network namespace or linux host namesapce)
233
233
"""
234
234
self .namespace = namespace
0 commit comments