Skip to content

Commit a0418a1

Browse files
oleksandrivantsivlguohan
authored andcommitted
[configdb]: Addopt get_table method to work with python3. (#26)
Decode data from redis DB into UTF-8 in python3.
1 parent 1c7a6b4 commit a0418a1

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/swsssdk/configdb.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
config_db.listen()
1818
1919
"""
20-
2120
import sys
2221
import time
2322
from .dbconnector import SonicV2Connector
2423

24+
PY3K = sys.version_info >= (3, 0)
25+
2526
class ConfigDBConnector(SonicV2Connector):
2627

2728
INIT_INDICATOR = 'CONFIG_DB_INITIALIZED'
@@ -101,16 +102,28 @@ def __raw_to_typed(self, raw_data):
101102
if raw_data == None:
102103
return None
103104
typed_data = {}
104-
for key in raw_data:
105+
for raw_key in raw_data:
106+
key = raw_key
107+
if PY3K:
108+
key = raw_key.decode('utf-8')
109+
105110
# "NULL:NULL" is used as a placeholder for objects with no attributes
106111
if key == "NULL":
107112
pass
108113
# A column key with ending '@' is used to mark list-typed table items
109114
# TODO: Replace this with a schema-based typing mechanism.
110115
elif key.endswith("@"):
111-
typed_data[key[:-1]] = raw_data[key].split(',')
116+
value = ""
117+
if PY3K:
118+
value = raw_data[raw_key].decode("utf-8").split(',')
119+
else:
120+
value = raw_data[raw_key].split(',')
121+
typed_data[key[:-1]] = value
112122
else:
113-
typed_data[key] = raw_data[key]
123+
if PY3K:
124+
typed_data[key] = raw_data[raw_key].decode('utf-8')
125+
else:
126+
typed_data[key] = raw_data[raw_key]
114127
return typed_data
115128

116129
def __typed_to_raw(self, typed_data):
@@ -212,10 +225,15 @@ def get_table(self, table):
212225
data = {}
213226
for key in keys:
214227
try:
215-
(_, row) = key.split(self.TABLE_NAME_SEPARATOR, 1)
216228
entry = self.__raw_to_typed(client.hgetall(key))
217-
if entry != None:
218-
data[self.deserialize_key(row)] = entry
229+
if entry:
230+
if PY3K:
231+
key = key.decode('utf-8')
232+
(_, row) = key.split(self.TABLE_NAME_SEPARATOR, 1)
233+
data[self.deserialize_key(row)] = entry
234+
else:
235+
(_, row) = key.split(self.TABLE_NAME_SEPARATOR, 1)
236+
data[self.deserialize_key(row)] = entry
219237
except ValueError:
220238
pass #Ignore non table-formated redis entries
221239
return data

0 commit comments

Comments
 (0)