Browse files

Except statement compatibility with python 2.5

  • Loading branch information...
1 parent edf753f commit 2d510150a0f9972a96cd90263bb0e22761b93009 Mark Costello committed Mar 22, 2013
Showing with 24 additions and 23 deletions.
  1. +10 −9 rdbtools/cli/rdb.py
  2. +14 −14 rdbtools/cli/redis_memory_for_key.py
View
19 rdbtools/cli/rdb.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+from __future__ import with_statement
import os
import sys
from optparse import OptionParser
@@ -13,23 +14,23 @@ def main():
parser = OptionParser(usage=usage)
parser.add_option("-c", "--command", dest="command",
help="Command to execute. Valid commands are json or diff", metavar="FILE")
-
+
parser.add_option("-f", "--file", dest="output",
help="Output file", metavar="FILE")
parser.add_option("-n", "--db", dest="dbs", action="append",
help="Database Number. Multiple databases can be provided. If not specified, all databases will be included.")
parser.add_option("-k", "--key", dest="keys", default=None,
help="Keys to export. This can be a regular expression")
parser.add_option("-t", "--type", dest="types", action="append",
- help="""Data types to include. Possible values are string, hash, set, sortedset, list. Multiple typees can be provided.
+ help="""Data types to include. Possible values are string, hash, set, sortedset, list. Multiple typees can be provided.
If not specified, all data types will be returned""")
-
+
(options, args) = parser.parse_args()
-
+
if len(args) == 0:
parser.error("Redis RDB file not specified")
dump_file = args[0]
-
+
filters = {}
if options.dbs:
filters['dbs'] = []
@@ -38,18 +39,18 @@ def main():
filters['dbs'].append(int(x))
except ValueError:
raise Exception('Invalid database number %s' %x)
-
+
if options.keys:
filters['keys'] = options.keys
-
+
if options.types:
filters['types'] = []
for x in options.types:
if not x in VALID_TYPES:
raise Exception('Invalid type provided - %s. Expected one of %s' % (x, (", ".join(VALID_TYPES))))
else:
filters['types'].append(x)
-
+
# TODO : Fix this ugly if-else code
if options.output:
with open(options.output, "wb") as f:
@@ -77,7 +78,7 @@ def main():
parser = RdbParser(callback, filters=filters)
parser.parse(dump_file)
-
+
if __name__ == '__main__':
main()
View
28 rdbtools/cli/redis_memory_for_key.py
@@ -7,7 +7,7 @@
from StringIO import StringIO
except ImportError:
from io import StringIO
-
+
from optparse import OptionParser
from rdbtools import RdbParser, JSONCallback, MemoryCallback
from rdbtools.callbacks import encode_key
@@ -23,21 +23,21 @@ def main():
"""
parser = OptionParser(usage=usage)
- parser.add_option("-s", "--server", dest="host", default="127.0.0.1",
+ parser.add_option("-s", "--server", dest="host", default="127.0.0.1",
help="Redis Server hostname. Defaults to 127.0.0.1")
- parser.add_option("-p", "--port", dest="port", default=6379, type="int",
+ parser.add_option("-p", "--port", dest="port", default=6379, type="int",
help="Redis Server port. Defaults to 6379")
- parser.add_option("-a", "--password", dest="password",
+ parser.add_option("-a", "--password", dest="password",
help="Password to use when connecting to the server")
parser.add_option("-d", "--db", dest="db", default=0,
help="Database number, defaults to 0")
-
+
(options, args) = parser.parse_args()
-
+
if len(args) == 0:
parser.error("Key not specified")
redis_key = args[0]
- print_memory_for_key(redis_key, host=options.host, port=options.port,
+ print_memory_for_key(redis_key, host=options.host, port=options.port,
db=options.db, password=options.password)
def print_memory_for_key(key, host='localhost', port=6379, db=0, password=None):
@@ -51,7 +51,7 @@ def print_memory_for_key(key, host='localhost', port=6379, db=0, password=None):
if not raw_dump:
sys.stderr.write('Key %s does not exist\n' % key)
sys.exit(-1)
-
+
stream = StringIO(raw_dump)
data_type = read_unsigned_char(stream)
parser.read_object(stream, data_type)
@@ -62,19 +62,19 @@ def connect_to_redis(host, port, db, password):
if not check_redis_version(redis):
sys.stderr.write('This script only works with Redis Server version 2.6.x or higher\n')
sys.exit(-1)
- except ConnectionError as e:
+ except ConnectionError, e:
sys.stderr.write('Could not connect to Redis Server : %s\n' % e)
sys.exit(-1)
- except ResponseError as e:
+ except ResponseError, e:
sys.stderr.write('Could not connect to Redis Server : %s\n' % e)
sys.exit(-1)
return redis
-
+
def check_redis_version(redis):
server_info = redis.info()
version_str = server_info['redis_version']
version = tuple(map(int, version_str.split('.')))
-
+
if version[0] > 2 or (version[0] == 2 and version[1] >= 6) :
return True
else:
@@ -92,8 +92,8 @@ def next_record(self, record) :
print("%s\t\t\t%s" % ("Encoding", record.encoding))
print("%s\t\t%s" % ("Number of Elements", record.size))
print("%s\t%s" % ("Length of Largest Element", record.len_largest_element))
-
- #print("%d,%s,%s,%d,%s,%d,%d\n" % (record.database, record.type, encode_key(record.key),
+
+ #print("%d,%s,%s,%d,%s,%d,%d\n" % (record.database, record.type, encode_key(record.key),
# record.bytes, record.encoding, record.size, record.len_largest_element))
if __name__ == '__main__':

0 comments on commit 2d51015

Please sign in to comment.