forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_adapter.py
More file actions
61 lines (50 loc) · 2.06 KB
/
attribute_adapter.py
File metadata and controls
61 lines (50 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import re
from .errors import DataJointError, _support_adapted_types
class AttributeAdapter:
"""
Base class for adapter objects for user-defined attribute types.
"""
@property
def attribute_type(self):
"""
:return: a supported DataJoint attribute type to use; e.g. "longblob", "blob@store"
"""
raise NotImplementedError("Undefined attribute adapter")
def get(self, value):
"""
convert value retrieved from the the attribute in a table into the adapted type
:param value: value from the database
:return: object of the adapted type
"""
raise NotImplementedError("Undefined attribute adapter")
def put(self, obj):
"""
convert an object of the adapted type into a value that DataJoint can store in a table attribute
:param obj: an object of the adapted type
:return: value to store in the database
"""
raise NotImplementedError("Undefined attribute adapter")
def get_adapter(context, adapter_name):
"""
Extract the AttributeAdapter object by its name from the context and validate.
"""
if not _support_adapted_types():
raise DataJointError("Support for Adapted Attribute types is disabled.")
adapter_name = adapter_name.lstrip("<").rstrip(">")
try:
adapter = context[adapter_name]
except KeyError:
raise DataJointError("Attribute adapter '{adapter_name}' is not defined.".format(adapter_name=adapter_name))
if not isinstance(adapter, AttributeAdapter):
raise DataJointError(
"Attribute adapter '{adapter_name}' must be an instance of datajoint.AttributeAdapter".format(
adapter_name=adapter_name
)
)
if not isinstance(adapter.attribute_type, str) or not re.match(r"^\w", adapter.attribute_type):
raise DataJointError(
"Invalid attribute type {type} in attribute adapter '{adapter_name}'".format(
type=adapter.attribute_type, adapter_name=adapter_name
)
)
return adapter