Package amplee :: Package storage :: Module storedejavu
[hide private]
[frames] | no frames]

Source Code for Module amplee.storage.storedejavu

  1  # -*- coding: utf-8 -*- 
  2   
  3  # 
  4  # dejavu ORM can be downloaded from: 
  5  # http://projects.amor.org/dejavu/ 
  6  # 
  7   
  8  __all__ = ['DejavuStorage', 'DJMember'] 
  9   
 10  import new 
 11  import copy 
 12  import os, os.path 
 13   
 14  import dejavu 
 15  from dejavu import logic, Unit, UnitProperty 
 16  arena = dejavu.Arena() 
 17                 
 18  from amplee.storage import Storage, StorageResourceInfo 
 19  from amplee.error import UnknownResource 
 20   
21 -class DJMember(Unit):
22 member_name = UnitProperty(unicode) 23 media_name = UnitProperty(unicode) 24 member_data = UnitProperty(str, hints={u'bytes': 0}) 25 media_data = UnitProperty(str, hints={u'bytes': 0})
26
27 -class DejavuStorage(Storage):
28 - def __init__(self, storage_type, config, base_unit=DJMember, encoding='ISO-8859-1'):
29 """ 30 Dejavu storage for amplee. 31 32 Keyword arguments 33 storage_type -- string indicating what underlying engine to use by dejavu 34 config -- a dictionnary of parameters to provide to dejavu for that storage_type 35 base_unit -- base dejavu.Unit class to use when creating collection units 36 37 The base_unit must provide at least the same properties as DJMember and more 38 if needed. 39 """ 40 #arena.logflags += dejavu.logflags.SQL 41 #arena.logflags += dejavu.logflags.FORGET 42 arena.add_store("main", "%s" % storage_type, config) 43 self.base_unit = base_unit 44 self.encoding = encoding
45
46 - def shutdown(self):
47 """Shutdown the main dejavu arena object""" 48 arena.shutdown()
49
50 - def create_container(self, collection_name):
51 """ 52 Register a new unit within the arena 53 54 This will create a table in your database 55 with the name 'collection_name' and following the 56 same schema as defined by the base_unit interface. 57 58 If it is already registered it will not create it. 59 60 Returns the newly created unit class. 61 62 Keyword argument 63 collection_name -- name of the unit to create 64 """ 65 try: 66 cls = arena.class_by_name(collection_name) 67 except KeyError: 68 cls = new.classobj(collection_name.encode(self.encoding), 69 (self.base_unit,), {}) 70 arena.register(cls) 71 if not arena.has_storage(cls): 72 arena.create_storage(cls) 73 return cls
74
75 - def info(self, collection_name, resource_name=None):
76 """ 77 Returns a StorageResourceInfo instance. 78 79 Keyword arguments 80 collection_name -- collection name 81 resource_name -- resource name to search for or None 82 """ 83 sandbox = arena.new_sandbox() 84 resource_name = resource_name or '' 85 expr = lambda m: ((m.member_name == resource_name) or \ 86 (m.media_name == resource_name)) 87 member_cls = self.create_container(collection_name) 88 mb = sandbox.unit(member_cls, expr) 89 return StorageResourceInfo(resource_name, mb, collection_name)
90
91 - def get_content(self, info):
92 """ 93 Returns the content of the resource. Raises 94 amplee.error.UnknownResource if info.key is None. 95 96 Keyword argument 97 info -- as returned by info() 98 """ 99 if info.key: 100 return info.key.media_data 101 102 raise UnknownResource(info.name)
103
104 - def get_meta_data(self, info):
105 """ 106 Returns the content of the resource. Raises 107 amplee.error.UnknownResource if info.key is None. 108 109 Keyword argument 110 info -- as returned by info() 111 """ 112 if info.key: 113 return info.key.member_data 114 115 raise UnknownResource(info.name)
116
117 - def put_content(self, info, content, member_id, media_id, **kwargs):
118 """ 119 Set the resource content of the unit instance 120 121 Automatically flushes modification to the storage. 122 123 Keyword arguments 124 info -- as returned by info() 125 If info.key is None then the unit is created is created. 126 127 content -- string object or a file object 128 to dump into mb.media_content. In the latter case read() 129 MUST return the full content as a byte string. 130 131 member_id -- as provided by member.member_id 132 media_id -- as provided by member.media_id 133 134 collection_name -- collection storing the resource 135 """ 136 if hasattr(content, 'read'): 137 content = content.read() 138 139 if not info.key: 140 sandbox = arena.new_sandbox() 141 member_cls = self.create_container(info.collection_name) 142 mb = member_cls() 143 if isinstance(member_id, str): 144 member_id = member_id.decode(self.encoding) 145 mb.member_name = member_id 146 if isinstance(media_id, str): 147 media_id = media_id.decode(self.encoding) 148 mb.media_name = media_id 149 sandbox.memorize(mb) 150 info.key = mb 151 152 info.key.update(media_data=content) 153 info.key.sandbox.flush_all()
154
155 - def put_meta_data(self, info, content, member_id, media_id, **kwargs):
156 """ 157 Set the resource content of the unit instance 158 159 Automatically flushes modification to the storage. 160 161 Keyword arguments 162 info -- as returned by info() 163 If info.key is None, it is created. 164 165 content -- string object or a file object 166 to dump into mb.media_content. In the latter case read() 167 MUST return the full content as a byte string. 168 169 member_id -- as provided by member.member_id 170 171 media_id -- as provided by member.media_id 172 """ 173 if hasattr(content, 'read'): 174 content = content.read() 175 176 if not info.key: 177 sandbox = arena.new_sandbox() 178 member_cls = self.create_container(info.collection_name) 179 mb = member_cls() 180 if isinstance(member_id, str): 181 member_id = member_id.decode(self.encoding) 182 mb.member_name = member_id 183 if isinstance(media_id, str): 184 media_id = media_id.decode(self.encoding) 185 mb.media_name = media_id 186 sandbox.memorize(mb) 187 info.key = mb 188 189 info.key.update(member_data=content) 190 info.key.sandbox.flush_all()
191
192 - def remove_content(self, info):
193 """ 194 Removes the resource from the storage 195 196 Keyword argument 197 info -- as returned by info() 198 """ 199 if info.key: 200 info.key.forget()
201
202 - def remove_meta_data(self, info):
203 """ 204 Removes the resource from the storage 205 206 Keyword argument 207 info -- as returned by info() 208 """ 209 self.remove_content(info)
210
211 - def persist(self, *args, **kwargs):
212 """ 213 Does nothing in this storage. 214 """ 215 pass
216
217 - def exists(self, info):
218 """ 219 True if 'info' is valid 220 False otherwise 221 222 Keyword argument 223 info -- as returned by info() 224 """ 225 if info.key is None: 226 return False 227 return True
228
229 - def ls(self, collection_name, ext=None):
230 """ 231 List all the resources in a collection 232 233 Keyword arguments 234 collection_name -- name of the storage unit 235 """ 236 member_cls = self.create_container(collection_name) 237 sandbox = arena.new_sandbox() 238 results = sandbox.recall(member_cls) 239 members = {} 240 for result in results: 241 name = result.member_name 242 if ext: 243 if result.member_name.endswith(ext) or result.media_name.endswith(ext): 244 members[name] = StorageResourceInfo(name, result, collection_name) 245 else: 246 members[name] = StorageResourceInfo(name, result, collection_name) 247 248 return members
249
250 - def ils(self, collection_name, ext=None):
251 """ 252 List all the resources in a collection 253 254 Keyword arguments 255 collection_name -- name of the storage unit 256 """ 257 member_cls = self.create_container(collection_name) 258 sandbox = arena.new_sandbox() 259 results = sandbox.recall(member_cls) 260 261 for result in results: 262 name = result.member_name 263 if ext: 264 if result.member_name.endswith(ext) or result.media_name.endswith(ext): 265 yield name, StorageResourceInfo(name, result, collection_name) 266 else: 267 yield name, StorageResourceInfo(name, result, collection_name) 268 269 raise StopIteration()
270