Skip to content

Commit

Permalink
Merge pull request sonic-net#3 from qiluo-msft/qiluo/redisarg
Browse files Browse the repository at this point in the history
DBInterface supports redis connection options
  • Loading branch information
qiluo-msft authored Mar 14, 2017
2 parents e5cbe0c + 72497de commit 497bdfe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/sswsdk/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class SonicV1Connector(DBInterface):
def __init__(self, **kwargs):
super(SonicV1Connector, self).__init__(**kwargs)

pass


Expand All @@ -19,6 +22,9 @@ class SonicV1Connector(DBInterface):


class SonicV2Connector(DBInterface):
def __init__(self, **kwargs):
super(SonicV2Connector, self).__init__(**kwargs)

pass


Expand Down
14 changes: 7 additions & 7 deletions src/sswsdk/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ class DBInterface(object):

db_map = dict()

def __init__(self):
def __init__(self, **kwargs):

super(DBInterface, self).__init__()

# Store the arguments for redis client
self.redis_kwargs = kwargs
if len(self.redis_kwargs) == 0:
self.redis_kwargs['unix_socket_path'] = self.REDIS_UNIX_SOCKET_PATH

# For thread safety as recommended by python-redis
# Create a separate client for each database
self.redis_clients = DBRegistry()
Expand Down Expand Up @@ -183,12 +188,7 @@ def _onetime_connect(self, db_name):
if db_id is None:
raise ValueError("No database ID configured for '{}'".format(db_name))

kwargs = dict(
unix_socket_path=self.REDIS_UNIX_SOCKET_PATH
)
kwargs.update(self.db_map[db_name])

client = redis.StrictRedis(**kwargs)
client = redis.StrictRedis(db=self.db_map[db_name]['db'], **self.redis_kwargs)

# Enable the notification mechanism for keyspace events in Redis
client.config_set('notify-keyspace-events', self.KEYSPACE_EVENTS)
Expand Down
27 changes: 19 additions & 8 deletions src/sswsdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Syslog and daemon script utility library.
"""

from __future__ import print_function
import json
import logging
import logging.config
Expand All @@ -12,24 +13,34 @@
# TODO: move to dbsync project.
def usage(script_name):
print('Usage: python ', script_name,
'-d [logging_level] -f [update_frequency] -h [help]')
'-t [host] -p [port] -s [unix_socket_path] -d [logging_level] -f [update_frequency] -h [help]')


# TODO: move to dbsync project.
def process_options(script_name):
"""
Process command line options
"""
options, remainders = getopt(sys.argv[1:], "d:f:h", ["debug=", "frequency=", "help"])
options, remainders = getopt(sys.argv[1:], "t:p:s:d:f:h", ["host=", "port=", "unix_socket_path=", "debug=", "frequency=", "help"])

args = {}
for (opt, arg) in options:
if opt in ('-d', '--debug'):
args['log_level'] = int(arg)
elif opt in ('-f', '--frequency'):
args['update_frequency'] = int(arg)
elif opt in ('-h', '--help'):
usage(script_name)
try:
if opt in ('-d', '--debug'):
args['log_level'] = int(arg)
elif opt in ('-t', '--host'):
args['host'] = arg
elif opt in ('-p', '--port'):
args['port'] = int(arg)
elif opt in ('-s', 'unix_socket_path'):
args['unix_socket_path'] = arg
elif opt in ('-f', '--frequency'):
args['update_frequency'] = int(arg)
elif opt in ('-h', '--help'):
usage(script_name)
except ValueError as e:
print('Invalid option for {}: {}'.format(opt, e))
sys.exit(1)

return args

Expand Down

0 comments on commit 497bdfe

Please sign in to comment.