Skip to content

Commit 20cf0a6

Browse files
check env variable DJ_ADAPTED_TYPES to switch supprt for AttributeAdapter
1 parent ec676f4 commit 20cf0a6

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

datajoint/attribute_adapter.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
import re
2+
import os
23
from .errors import DataJointError
34

5+
6+
ADAPTED_TYPE_SWITCH = "DJ_SUPPORT_ADAPTED_TYPES"
7+
8+
9+
def _switch_adapated_types(on):
10+
"""
11+
Enable (on=True) or disable (on=False) support for AttributeAdapter
12+
"""
13+
if on:
14+
os.environ[ADAPTED_TYPE_SWITCH] = "TRUE"
15+
else:
16+
del os.environ[ADAPTED_TYPE_SWITCH]
17+
18+
19+
def _support_adapted_types():
20+
"""
21+
check if support for AttributeAdapter is enabled
22+
"""
23+
return os.getenv(ADAPTED_TYPE_SWITCH, "FALSE").upper() == "TRUE"
24+
25+
426
class AttributeAdapter:
527
"""
628
Base class for adapter objects for user-defined attribute types.
@@ -33,6 +55,8 @@ def get_adapter(context, adapter_name):
3355
"""
3456
Extract the AttributeAdapter object by its name from the context and validate.
3557
"""
58+
if not _support_adapted_types():
59+
raise DataJointError('Support for Adapted Attribute types is disabled.')
3660
adapter_name = adapter_name.lstrip('<').rstrip('>')
3761
try:
3862
adapter = context[adapter_name]

datajoint/version.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__version__ = "0.12.dev6c"
1+
__version__ = "0.12.dev6"
2+
3+
assert len(__version__) <= 10 # The log table limits version to the 10 characters

tests/schema_adapted.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import datajoint as dj
22
import networkx as nx
3+
from datajoint import attribute_adapter
34

45
from . import PREFIX, CONN_INFO
56

7+
68
schema_name = PREFIX + '_test_custom_datatype'
79
schema = dj.schema(schema_name, connection=dj.conn(**CONN_INFO))
810

11+
attribute_adapter._switch_adapated_types(True)
12+
913

1014
class GraphAdapter(dj.AttributeAdapter):
1115

@@ -34,3 +38,6 @@ class Connectivity(dj.Manual):
3438
---
3539
conn_graph = null : <graph>
3640
"""
41+
42+
43+
attribute_adapter._switch_adapated_types(False)

tests/test_adapted_attributes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
from . import schema_adapted as adapted
66
from .schema_adapted import graph
77

8+
from datajoint import attribute_adapter
9+
10+
811

912
def test_adapted_type():
13+
attribute_adapter._switch_adapated_types(True)
1014
c = adapted.Connectivity()
1115
graphs = [nx.lollipop_graph(4, 2), nx.star_graph(5), nx.barbell_graph(3, 1), nx.cycle_graph(5)]
1216
c.insert((i, g) for i, g in enumerate(graphs))
@@ -16,6 +20,7 @@ def test_adapted_type():
1620
assert_equal(len(g1.edges), len(g2.edges))
1721
assert_true(0 == len(nx.symmetric_difference(g1, g2).edges))
1822
c.delete()
23+
attribute_adapter._switch_adapated_types(False)
1924

2025

2126
# test spawned classes
@@ -24,6 +29,7 @@ def test_adapted_type():
2429

2530

2631
def test_adapted_spawned():
32+
attribute_adapter._switch_adapated_types(True)
2733
c = Connectivity() # a spawned class
2834
graphs = [nx.lollipop_graph(4, 2), nx.star_graph(5), nx.barbell_graph(3, 1), nx.cycle_graph(5)]
2935
c.insert((i, g) for i, g in enumerate(graphs))
@@ -33,13 +39,15 @@ def test_adapted_spawned():
3339
assert_equal(len(g1.edges), len(g2.edges))
3440
assert_true(0 == len(nx.symmetric_difference(g1, g2).edges))
3541
c.delete()
42+
attribute_adapter._switch_adapated_types(False)
3643

3744

3845
# test with virtual module
3946
virtual_module = dj.create_virtual_module('virtual_module', adapted.schema_name, add_objects={'graph': graph})
4047

4148

4249
def test_adapted_virtual():
50+
attribute_adapter._switch_adapated_types(True)
4351
c = virtual_module.Connectivity()
4452
graphs = [nx.lollipop_graph(4, 2), nx.star_graph(5), nx.barbell_graph(3, 1), nx.cycle_graph(5)]
4553
c.insert((i, g) for i, g in enumerate(graphs))
@@ -53,3 +61,7 @@ def test_adapted_virtual():
5361
assert_equal(len(g1.edges), len(g2.edges))
5462
assert_true(0 == len(nx.symmetric_difference(g1, g2).edges))
5563
c.delete()
64+
attribute_adapter._switch_adapated_types(False)
65+
66+
67+

0 commit comments

Comments
 (0)