1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
23 class StorageAPI(object):
25 Base class for storage APIs.
27 def create(self, **kwargs):
33 raise NotImplementedError('Subclass must implement abstract create method')
36 class ModelAPI(StorageAPI):
38 Base class for model APIs ("MAPI").
40 def __init__(self, model_cls, name=None, **kwargs):
42 :param model_cls: representing class of the model
43 :param name: name of the model
45 super(ModelAPI, self).__init__(**kwargs)
46 self._model_cls = model_cls
47 self._name = name or model_cls.__modelname__
48 self._thread_local = threading.local()
49 self._thread_local._instrumentation = []
52 def _instrumentation(self):
53 if not hasattr(self._thread_local, '_instrumentation'):
54 self._thread_local._instrumentation = []
55 return self._thread_local._instrumentation
63 :type: :obj:`basestring`
70 Class representing the model
74 return self._model_cls
76 def get(self, entry_id, filters=None, **kwargs):
78 Gets a model from storage.
82 raise NotImplementedError('Subclass must implement abstract get method')
84 def put(self, entry, **kwargs):
86 Puts a model in storage.
90 raise NotImplementedError('Subclass must implement abstract store method')
92 def delete(self, entry_id, **kwargs):
94 Deletes a model from storage.
98 raise NotImplementedError('Subclass must implement abstract delete method')
103 def iter(self, **kwargs):
105 Iterate over all models in storage.
107 raise NotImplementedError('Subclass must implement abstract iter method')
109 def update(self, entry, **kwargs):
111 Update a model in storage.
116 raise NotImplementedError('Subclass must implement abstract update method')
119 class ResourceAPI(StorageAPI):
121 Base class for resource APIs ("RAPI").
123 def __init__(self, name, **kwargs):
125 :param name: resource type
127 super(ResourceAPI, self).__init__(**kwargs)
135 :type: :obj:`basestring`
139 def read(self, entry_id, path, **kwargs):
141 Get a bytesteam for a resource from storage.
146 raise NotImplementedError('Subclass must implement abstract read method')
148 def delete(self, entry_id, path, **kwargs):
150 Delete a resource from storage.
155 raise NotImplementedError('Subclass must implement abstract delete method')
157 def download(self, entry_id, destination, path=None, **kwargs):
159 Download a resource from storage.
165 raise NotImplementedError('Subclass must implement abstract download method')
167 def upload(self, entry_id, source, path=None, **kwargs):
169 Upload a resource to storage.
175 raise NotImplementedError('Subclass must implement abstract upload method')
178 def generate_lower_name(model_cls):
180 Generates the name of the class from the class object, e.g. ``SomeClass`` -> ``some_class``
182 :param model_cls: class to evaluate
183 :return: lowercase name
186 return getattr(model_cls, '__mapiname__', model_cls.__tablename__)