Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 1b21a45

Browse files
committed
Add capibility props, cap inforcement optional
1 parent 9d32118 commit 1b21a45

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

orbitdbapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.0.rc.1.dev4'
1+
__version__ = '0.1.0.rc.1.dev5'

orbitdbapi/db.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, client, params, **kwargs):
1515
self.__id_safe = urlquote(self.__id, safe='')
1616
self.__type = params['type']
1717
self.__use_cache = kwargs.get('use_db_cache')
18+
self.__enforce_caps = kwargs.get('__enforce_caps', True)
1819
self.logger = logging.getLogger(__name__)
1920

2021
if hasattr( self.params, 'indexBy'):
@@ -63,6 +64,22 @@ def putable(self):
6364
def removeable(self):
6465
return 'remove' in self.__params.get('capabilities', {})
6566

67+
@property
68+
def iterable(self):
69+
return 'iterator' in self.__params.get('capabilities', {})
70+
71+
@property
72+
def addable(self):
73+
return 'add' in self.__params.get('capabilities', {})
74+
75+
@property
76+
def valuable(self):
77+
return 'value' in self.__params.get('capabilities', {})
78+
79+
@property
80+
def incrementable(self):
81+
return 'inc' in self.__params.get('capabilities', {})
82+
6683
@property
6784
def indexed(self):
6885
return hasattr(self, '__index_by')
@@ -91,6 +108,8 @@ def get_raw(self, item):
91108
return (self.__client._call('get', endpoint))
92109

93110
def put(self, item, cache=None):
111+
if self.__enforce_caps and not self.putable:
112+
raise CapabilityError('Db {} does not have put capability'.format(self.__dbname))
94113
if cache is None: cache = self.__use_cache
95114
if cache:
96115
if hasattr(self, '__index_by'):
@@ -108,17 +127,23 @@ def put(self, item, cache=None):
108127
return entry_hash
109128

110129
def add(self, item, cache=None):
130+
if self.__enforce_caps and not self.addable:
131+
raise CapabilityError('Db {} does not have add capability'.format(self.__dbname))
111132
if cache is None: cache = self.__use_cache
112133
endpoint = '/'.join(['db', self.__id_safe, 'add'])
113134
entry_hash = self.__client._call('post', endpoint, item)
114135
if cache: self.__cache[entry_hash] = item
115136
return entry_hash
116137

117138
def iterator_raw(self, params):
139+
if self.__enforce_caps and not self.iterable:
140+
raise CapabilityError('Db {} does not have remove capability'.format(self.__dbname))
118141
endpoint = '/'.join(['db', self.__id_safe, 'rawiterator'])
119142
return self.__client._call('get', endpoint, params)
120143

121144
def iterator(self, params):
145+
if self.__enforce_caps and not self.iterable:
146+
raise CapabilityError('Db {} does not have remove capability'.format(self.__dbname))
122147
endpoint = '/'.join(['db', self.__id_safe, 'iterator'])
123148
return self.__client._call('get', endpoint, params)
124149

@@ -129,8 +154,8 @@ def index(self):
129154
return result
130155

131156
def remove(self, item):
132-
if not self.removeable:
133-
raise CapabilityError('Db does not have remove capability')
157+
if self.__enforce_caps and not self.removeable:
158+
raise CapabilityError('Db {} does not have remove capability'.format(self.__dbname))
134159
item = str(item)
135160
endpoint = '/'.join(['db', self.__id_safe, item])
136161
return self.__client._call('delete', endpoint)

0 commit comments

Comments
 (0)