Commits

Matt Bone committed d0b9952

reorganize commands.

  • Participants
  • Parent commits 77a668c

Comments (0)

Files changed (16)

+"""
+Redis backend.
+
+Mostly this maps strings to functions.
+"""
+
+from functools import partial
+
+from commands.strings import incr, get, strlen, append, sset
+from commands.hashes import hset, hget, hlen
+from commands.lists import rpush, rpop, llen
+from commands.sets import sadd, scard, sismember
+from commands.connection import ping, echo
+from commands.server import flushdb, dbsize
+
+
+class Backend(object):
+
+    def __init__(self):
+        self.db = {}  # should eventually support multiple dbs 
+
+        self.commands = {
+            # strings
+            'incr': partial(incr, self.db),
+            'incrby': partial(incr, self.db),
+            'decr': partial(incr, self.db, mult=-1),
+            'decrby': partial(incr, self.db, mult=-1),
+            'set': partial(sset, self.db),
+            'get': partial(get, self.db),
+            'strlen': partial(strlen, self.db),
+            'append': partial(append, self.db),
+
+            # hashes
+            'hset': partial(hset, self.db),
+            'hget': partial(hget, self.db),
+            'hlen': partial(hlen, self.db),
+
+            # lists
+            'rpush': partial(rpush, self.db),
+            'rpop': partial(rpop, self.db),
+            'llen': partial(llen, self.db),
+
+            #sets
+            'sadd': partial(sadd, self.db),
+            'scard': partial(scard, self.db),
+            'sismember': partial(sismember, self.db),
+
+            # connection
+            'ping': ping,
+            'echo': echo,
+
+            # server
+            'flushdb': partial(flushdb, self.db),
+            'dbsize': partial(dbsize, self.db),
+        }
+    
+    def dispatch(self, command, args):
+        return self.commands[command](*args)

commands.py

-"""
-Implementation of redis commands.
-
-Each command needs to be a callable.
-"""
-
-from functools import partial
-
-def get(db, key):
-    """
-    See: http://redis.io/commands/get
-    """
-    result = db.get(key)
-    return str(result) if result is not None else None
-
-
-def sset(db, key, value):
-    """
-    See: http://redis.io/commands/set
-    """
-    db[key] = value
-
-
-def incr(db, key, *args, mult=1):
-    """
-    See: 
-      - http://redis.io/commands/incr
-      - http://redis.io/commands/incrby
-      - http://redis.io/commands/decr
-      - http://redis.io/commands/decrby
-    """
-    if len(args) == 0:
-        increment = 1 * mult
-    elif len(args) == 1:
-        increment = args[0] * mult
-    else:
-        raise Exception("Too many arguments.")
-
-    value = db.get(key, 0)
-    value += increment
-    db[key] = value
-
-    return value
-
-
-def append(db, key, append_value):
-    """
-    See:
-      - http://redis.io/commands/append
-    """
-    value = db.get(key, "")
-    new_value = value + append_value
-    db[key] = new_value
-    return len(new_value)
-
-
-def strlen(db, key):
-    """
-    See:
-      - http://redis.io/commands/strlen
-    """
-    return len(db[key])
-
-
-def hset(db, key, field, value):
-    """
-    See:
-      - http://redis.io/commands/hset
-    """
-    hash = db.setdefault(key, {})
-
-    if field in hash:
-        hash[field] = value
-        return 0
-    else:
-        hash[field] = value
-        return 1
-
-
-def hget(db, key, field):
-    """
-    See:
-      - http://redis.io/commands/hset
-    """
-    if key not in db:
-        return None
-
-    hash = db[key]
-    if field in hash:
-        return hash[field]
-    else:
-        return None
-
-
-def hlen(db, key):
-    """
-    See:
-      - http://redis.io/commands/hlen
-    """
-    return len(db.get(key, {}))
-
-
-def rpush(db, key, *args):
-    """
-    See:
-      - http://redis.io/commands/rpush
-    """
-    mylist = db.setdefault(key, [])
-    for arg in args:
-        mylist.append(arg)
-    
-    return len(mylist)
-
-
-def llen(db, key):
-    """
-    See:
-      - http://redis.io/commands/llen
-    """
-    return len(db.get(key, []))
-
-
-def rpop(db, key):
-    """
-    See:
-      - http://redis.io/commands/rpop
-    """
-    return str(db[key].pop())
-
-
-def sadd(db, key, *args):
-    """
-    See:
-      - http://redis.io/commands/sadd
-    """
-    value = db.setdefault(key, set())
-    for arg in args:
-        value.add(arg)
-    
-    return len(value)
-
-
-def sismember(db, key, member):
-    """
-    See:
-      - http://redis.io/commands/sismember
-    """
-    value = db.get(key, set())
-    if member in value:
-        return 1
-    else:
-        return 0
-
-
-def scard(db, key):
-    """
-    See:
-      - http://redis.io/commands/card
-    """
-    return len(db.get(key, set()))
-
-
-def ping():
-    """
-    See: 
-      - http://redis.io/commands/ping
-    """
-    return "PONG"
-
-
-def echo(arg):
-    """
-    See:
-      - http://redis.io/commands/echo
-    """
-    return arg
-
-
-def dbsize(db):
-    """
-    See:
-      - http://redis.io/commands/dbsize
-    """
-    return len(db)
-
-
-def flushdb(db):
-    """
-    See:
-      - http://redis.io/commands/flushdb
-    """
-    for key in list(db.keys()):
-        del db[key]
-
-
-class Backend(object):
-
-    def __init__(self):
-        self.db = {}  # should eventually support multiple dbs 
-
-        self.commands =  {
-            # strings
-            'incr': partial(incr, self.db),
-            'incrby': partial(incr, self.db),
-            'decr': partial(incr, self.db, mult=-1),
-            'decrby': partial(incr, self.db, mult=-1),
-            'set': partial(sset, self.db),
-            'get': partial(get, self.db),
-            'strlen': partial(strlen, self.db),
-            'append': partial(append, self.db),
-
-            # hashes
-            'hset': partial(hset, self.db),
-            'hget': partial(hget, self.db),
-            'hlen': partial(hlen, self.db),
-
-            # lists
-            'rpush': partial(rpush, self.db),
-            'rpop': partial(rpop, self.db),
-            'llen': partial(llen, self.db),
-
-            #sets
-            'sadd': partial(sadd, self.db),
-            'scard': partial(scard, self.db),
-            'sismember': partial(sismember, self.db),
-
-            # connection
-            'ping': ping,
-            'echo': echo,
-
-            # server
-            'flushdb': partial(flushdb, self.db),
-            'dbsize': partial(dbsize, self.db),
-        }
-
-    
-    def dispatch(self, command, args):
-        return self.commands[command](*args)

commands/__init__.py

Empty file added.

commands/connection.py

+"""
+Implementation of redis connection commands.
+
+See: http://redis.io/commands#connection
+
+Each command needs to be a callable.
+"""
+
+
+def ping():
+    """
+    See:
+      - http://redis.io/commands/ping
+    """
+    return "PONG"
+
+
+def echo(arg):
+    """
+    See:
+      - http://redis.io/commands/echo
+    """
+    return arg

commands/hashes.py

+"""
+Implementation of redis hash commands.
+
+See: http://redis.io/commands#hash
+
+Each command needs to be a callable.
+"""
+
+
+def hset(db, key, field, value):
+    """
+    See:
+      - http://redis.io/commands/hset
+    """
+    hash = db.setdefault(key, {})
+
+    if field in hash:
+        hash[field] = value
+        return 0
+    else:
+        hash[field] = value
+        return 1
+
+
+def hget(db, key, field):
+    """
+    See:
+      - http://redis.io/commands/hset
+    """
+    if key not in db:
+        return None
+
+    hash = db[key]
+    if field in hash:
+        return hash[field]
+    else:
+        return None
+
+
+def hlen(db, key):
+    """
+    See:
+      - http://redis.io/commands/hlen
+    """
+    return len(db.get(key, {}))

commands/keys.py

Empty file added.

commands/lists.py

+"""
+Implementation of redis list commands.
+
+See: http://redis.io/commands#list
+
+Each command needs to be a callable.
+"""
+
+
+def rpush(db, key, *args):
+    """
+    See:
+      - http://redis.io/commands/rpush
+    """
+    mylist = db.setdefault(key, [])
+    for arg in args:
+        mylist.append(arg)
+
+    return len(mylist)
+
+
+def llen(db, key):
+    """
+    See:
+      - http://redis.io/commands/llen
+    """
+    return len(db.get(key, []))
+
+
+def rpop(db, key):
+    """
+    See:
+      - http://redis.io/commands/rpop
+    """
+    return str(db[key].pop())

commands/pub_sub.py

Empty file added.

commands/scripting.py

Empty file added.

commands/server.py

+"""
+Implementation of redis connection commands.
+
+See: http://redis.io/commands#connection
+
+Each command needs to be a callable.
+"""
+
+
+def dbsize(db):
+    """
+    See:
+      - http://redis.io/commands/dbsize
+    """
+    return len(db)
+
+
+def flushdb(db):
+    """
+    See:
+      - http://redis.io/commands/flushdb
+    """
+    for key in list(db.keys()):
+        del db[key]
+"""
+Implementation of redis set commands.
+
+See: http://redis.io/commands#set
+
+Each command needs to be a callable.
+"""
+
+
+def sadd(db, key, *args):
+    """
+    See:
+      - http://redis.io/commands/sadd
+    """
+    value = db.setdefault(key, set())
+    for arg in args:
+        value.add(arg)
+
+    return len(value)
+
+
+def sismember(db, key, member):
+    """
+    See:
+      - http://redis.io/commands/sismember
+    """
+    value = db.get(key, set())
+    if member in value:
+        return 1
+    else:
+        return 0
+
+
+def scard(db, key):
+    """
+    See:
+      - http://redis.io/commands/card
+    """
+    return len(db.get(key, set()))

commands/sorted_sets.py

Empty file added.

commands/strings.py

+"""
+Implementation of redis string commands.
+
+See: http://redis.io/commands#string
+
+Each command needs to be a callable.
+"""
+
+
+def get(db, key):
+    """
+    See: http://redis.io/commands/get
+    """
+    result = db.get(key)
+    return str(result) if result is not None else None
+
+
+def sset(db, key, value):
+    """
+    See: http://redis.io/commands/set
+    """
+    db[key] = value
+
+
+def incr(db, key, *args, mult=1):
+    """
+    See:
+      - http://redis.io/commands/incr
+      - http://redis.io/commands/incrby
+      - http://redis.io/commands/decr
+      - http://redis.io/commands/decrby
+    """
+    if len(args) == 0:
+        increment = 1 * mult
+    elif len(args) == 1:
+        increment = args[0] * mult
+    else:
+        raise Exception("Too many arguments.")
+
+    value = db.get(key, 0)
+    value += increment
+    db[key] = value
+
+    return value
+
+
+def append(db, key, append_value):
+    """
+    See:
+      - http://redis.io/commands/append
+    """
+    value = db.get(key, "")
+    new_value = value + append_value
+    db[key] = new_value
+    return len(new_value)
+
+
+def strlen(db, key):
+    """
+    See:
+      - http://redis.io/commands/strlen
+    """
+    return len(db[key])

commands/transactions.py

Empty file added.
 from twisted.internet import protocol, reactor
 from twisted.protocols.basic import LineReceiver
 
-from commands import Backend
+from backend import Backend
 
 
 def coerce_arg(arg):
-from commands import (get,
-                      sset,
-                      incr,
-                      strlen,
-                      )
+from commands.strings import (get,
+                              sset,
+                              incr,
+                              strlen,
+                              )
                       
 
 def test_get_command():