Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added password option #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions redisdl.py
Expand Up @@ -3,8 +3,8 @@
import json import json
import redis import redis


def dumps(host='localhost', port=6379, db=0, pretty=False): def dumps(host='localhost', port=6379, password=None, db=0, pretty=False):
r = redis.Redis(host=host, port=port, db=db) r = redis.Redis(host=host, port=port, password=password, db=db)
kwargs = {} kwargs = {}
if not pretty: if not pretty:
kwargs['separators'] = (',', ':') kwargs['separators'] = (',', ':')
Expand All @@ -17,13 +17,13 @@ def dumps(host='localhost', port=6379, db=0, pretty=False):
table[key] = {'type': type, 'value': value} table[key] = {'type': type, 'value': value}
return encoder.encode(table) return encoder.encode(table)


def dump(fp, host='localhost', port=6379, db=0, pretty=False): def dump(fp, host='localhost', port=6379, password=None, db=0, pretty=False):
if pretty: if pretty:
# hack to avoid implementing pretty printing # hack to avoid implementing pretty printing
fp.write(dumps(host=host, port=port, db=db, pretty=pretty)) fp.write(dumps(host=host, port=port, password=password, db=db, pretty=pretty))
return return


r = redis.Redis(host=host, port=port, db=db) r = redis.Redis(host=host, port=port, password=password, db=db)
kwargs = {} kwargs = {}
if not pretty: if not pretty:
kwargs['separators'] = (',', ':') kwargs['separators'] = (',', ':')
Expand Down Expand Up @@ -64,8 +64,8 @@ def _reader(r, pretty):
raise UnknownTypeError("Unknown key type: %s" % type) raise UnknownTypeError("Unknown key type: %s" % type)
yield key, type, value yield key, type, value


def loads(s, host='localhost', port=6379, db=0, empty=False): def loads(s, host='localhost', port=6379, password=None, db=0, empty=False):
r = redis.Redis(host=host, port=port, db=db) r = redis.Redis(host=host, port=port, password=password, db=db)
if empty: if empty:
for key in r.keys(): for key in r.keys():
r.delete(key) r.delete(key)
Expand All @@ -76,9 +76,9 @@ def loads(s, host='localhost', port=6379, db=0, empty=False):
value = item['value'] value = item['value']
_writer(r, key, type, value) _writer(r, key, type, value)


def load(fp, host='localhost', port=6379, db=0, empty=False): def load(fp, host='localhost', port=6379, password=None, db=0, empty=False):
s = fp.read() s = fp.read()
loads(s, host, port, db, empty) loads(s, host, port, password, db, empty)


def _writer(r, key, type, value): def _writer(r, key, type, value):
r.delete(key) r.delete(key)
Expand Down Expand Up @@ -113,6 +113,8 @@ def options_to_kwargs(options):
args['host'] = options.host args['host'] = options.host
if options.port: if options.port:
args['port'] = int(options.port) args['port'] = int(options.port)
if options.password:
args['password'] = options.password
if options.db: if options.db:
args['db'] = int(options.db) args['db'] = int(options.db)
# dump only # dump only
Expand Down Expand Up @@ -177,6 +179,7 @@ def do_load(options, args):
parser.add_option('-H', '--host', help='connect to HOST (default localhost)') parser.add_option('-H', '--host', help='connect to HOST (default localhost)')
parser.add_option('-p', '--port', help='connect to PORT (default 6379)') parser.add_option('-p', '--port', help='connect to PORT (default 6379)')
parser.add_option('-s', '--socket', help='connect to SOCKET') parser.add_option('-s', '--socket', help='connect to SOCKET')
parser.add_option('-a', '--password', help='connect with PASSWORD')
if help == DUMP: if help == DUMP:
parser.add_option('-d', '--db', help='dump DATABASE (0-N, default 0)') parser.add_option('-d', '--db', help='dump DATABASE (0-N, default 0)')
parser.add_option('-o', '--output', help='write to OUTPUT instead of stdout') parser.add_option('-o', '--output', help='write to OUTPUT instead of stdout')
Expand Down