shep

Multi-state key stores using bit masks for python3
git clone git://git.defalsify.org/shep.git
Log | Files | Refs | LICENSE

redis.py (2427B)


      1 # standard imports
      2 import datetime
      3 
      4 # external imports
      5 import redis
      6 
      7 # local imports
      8 from .base import StoreFactory
      9 
     10 
     11 class RedisStore:
     12 
     13     def __init__(self, path, redis, binary=False):
     14         self.redis = redis
     15         self.__path = path
     16         self.__binary = binary
     17 
     18 
     19     def __to_path(self, k):
     20         return '.'.join([self.__path, k])
     21 
     22 
     23     def __from_path(self, s):
     24         (left, right) = s.split(b'.', maxsplit=1)
     25         return right
     26 
     27 
     28     def __to_result(self, v):
     29         if self.__binary:
     30             return v
     31         return v.decode('utf-8')
     32 
     33 
     34     def put(self, k, contents=b''):
     35         if contents == None:
     36             contents = b''
     37         k = self.__to_path(k)
     38         self.redis.set(k, contents)
     39 
     40 
     41     def remove(self, k):
     42         k = self.__to_path(k)
     43         self.redis.delete(k)
     44 
     45 
     46     def get(self, k):
     47         k = self.__to_path(k)
     48         v = self.redis.get(k)
     49         return self.__to_result(v)
     50 
     51     
     52     def list(self):
     53         (cursor, matches) = self.redis.scan(match=self.__path + '.*')
     54 
     55         r = []
     56         for s in matches:
     57             k = self.__from_path(s)
     58             v = self.redis.get(k)
     59             r.append((k, v,))
     60 
     61         return r
     62 
     63 
     64     def path(self):
     65         return None
     66 
     67 
     68     def replace(self, k, contents):
     69         if contents == None:
     70             contents = b''
     71         k = self.__to_path(k)
     72         v = self.redis.get(k)
     73         if v == None:
     74             raise FileNotFoundError(k)
     75         self.redis.set(k, contents)
     76 
     77 
     78     def modified(self, k):
     79         k = self.__to_path(k)
     80         k = '_mod' + k
     81         v = self.redis.get(k)
     82         return int(v)
     83 
     84 
     85     def register_modify(self, k):
     86         k = self.__to_path(k)
     87         k = '_mod' + k
     88         ts = datetime.datetime.utcnow().timestamp()
     89         self.redis.set(k)
     90 
     91 
     92 class RedisStoreFactory(StoreFactory):
     93 
     94     def __init__(self, host='localhost', port=6379, db=2, binary=False):
     95         self.redis = redis.Redis(host=host, port=port, db=db)
     96         self.__binary = binary
     97 
     98 
     99     def add(self, k):
    100         k = str(k)
    101         return RedisStore(k, self.redis, binary=self.__binary)
    102 
    103 
    104     def close(self):
    105         self.redis.close()
    106 
    107 
    108     def ls(self):
    109         r = []
    110         (c, ks) = self.redis.scan(match='*')
    111         for k in ks:
    112             v = k.rsplit(b'.', maxsplit=1)
    113             if v != k:
    114                 v = v[0].decode('utf-8')
    115                 if v not in r:
    116                     r.append(v)
    117         return r