regenerate database migrations
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / redisco / containers.py
1 #  Copyright (c) 2010 Tim Medina
2 #
3 #  Permission is hereby granted, free of charge, to any person
4 #  obtaining a copy of this software and associated documentation
5 #  files (the "Software"), to deal in the Software without
6 #  restriction, including without limitation the rights to use,
7 #  copy, modify, merge, publish, distribute, sublicense, and/or sell
8 #  copies of the Software, and to permit persons to whom the
9 #  Software is furnished to do so, subject to the following
10 #  conditions:
11 #
12 #  The above copyright notice and this permission notice shall be
13 #  included in all copies or substantial portions of the Software.
14 #
15 #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 #  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 #  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 #  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 #  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 #  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 #  OTHER DEALINGS IN THE SOFTWARE.
23 #
24 #  The original code link is https://github.com/iamteem/redisco/tree/master/redisco/containers.py
25 """
26 This module contains the container classes to create objects
27 that persist directly in a Redis server.
28 """
29 import collections
30 from functools import partial
31
32
33 class Container(object):
34     """Create a container object saved in Redis.
35
36     Arguments:
37         key -- the Redis key this container is stored at
38         db  -- the Redis client object. Default: None
39
40     When ``db`` is not set, the gets the default connection from
41     ``redisco.connection`` module.
42     """
43
44     def __init__(self, key, db=None, pipeline=None):
45         self._db = db
46         self.key = key
47         self.pipeline = pipeline
48
49     def clear(self):
50         """Remove container from Redis database."""
51         del self.db[self.key]
52
53     def __getattribute__(self, att):
54         if att in object.__getattribute__(self, 'DELEGATEABLE_METHODS'):
55             return partial(getattr(object.__getattribute__(self, 'db'), att), self.key)
56         else:
57             return object.__getattribute__(self, att)
58
59     @property
60     def db(self):
61         if self.pipeline:
62             return self.pipeline
63         if self._db:
64             return self._db
65         if hasattr(self, 'db_cache') and self.db_cache:
66             return self.db_cache
67         else:
68             from lcm.pub.redisco import connection
69             self.db_cache = connection
70             return self.db_cache
71
72     DELEGATEABLE_METHODS = ()
73
74
75 class Hash(Container, collections.MutableMapping):
76
77     def __getitem__(self, att):
78         return self.hget(att)
79
80     def __setitem__(self, att, val):
81         self.hset(att, val)
82
83     def __delitem__(self, att):
84         self.hdel(att)
85
86     def __len__(self):
87         return self.hlen()
88
89     def __iter__(self):
90         return self.hgetall().__iter__()
91
92     def __contains__(self, att):
93         return self.hexists(att)
94
95     def __repr__(self):
96         return "<%s '%s' %s>" % (self.__class__.__name__, self.key, self.hgetall())
97
98     def keys(self):
99         return self.hkeys()
100
101     def values(self):
102         return self.hvals()
103
104     def _get_dict(self):
105         return self.hgetall()
106
107     def _set_dict(self, new_dict):
108         self.clear()
109         self.update(new_dict)
110
111     dict = property(_get_dict, _set_dict)
112
113     DELEGATEABLE_METHODS = ('hlen', 'hset', 'hdel', 'hkeys', 'hgetall', 'hvals',
114                             'hget', 'hexists', 'hincrby', 'hmget', 'hmset')