Skip to content

Commit 39bef78

Browse files
Determine location of module by looking at module attributes
Instead of version checking. Work around the circularity of importing new labscript_devices (which imports labscript_utils.device_registry) by unconditionally importing the new module first, rolling it back if it turns out labscript_devices is old and defines the device registry itself.
1 parent c87ba99 commit 39bef78

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
try:
2-
import importlib.metadata as importlib_metadata
3-
except ImportError:
4-
import importlib_metadata
5-
import packaging.version
1+
from ._device_registry import *
62

7-
use_local_machinery = True
3+
# Backwards compatibility for labscript-devices < 3.1. If labscript_devices defines the
4+
# device registry as well, undo the above import and use the contents of
5+
# labscript_devices instead. The above import must be done first so that the names are
6+
# available to labscript_devices during the below import, since as of 3.1 it imports
7+
# this module as well.
88
try:
9-
labscript_device_version_str = importlib_metadata.version('labscript_devices')
10-
labscript_devices_version = packaging.version.parse(labscript_device_version_str)
11-
use_local_machinery = labscript_devices_version >= packaging.version.parse('3.1.0.dev16')
12-
except importlib_metadata.PackageNotFoundError:
9+
from labscript_devices import ClassRegister
10+
if ClassRegister.__module__ == 'labscript_devices':
11+
for name in _device_registry.__all__:
12+
del globals()[name]
13+
from labscript_devices import *
14+
except ImportError:
1315
pass
14-
15-
# import and use the labscript_devices code if an old version of the package is used.
16-
# This ensures that new labscript_utils with old labscript_devices doesn't attempt to
17-
# register labscript_devices classes twice
18-
if not use_local_machinery:
19-
from labscript_devices import *
20-
else:
21-
from ._device_registry import *

labscript_utils/device_registry/_device_registry.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import sys
32
import importlib
43
import imp
54
import warnings
@@ -45,6 +44,20 @@ class belong to each device. Tab and parser classes must be passed to register_c
4544
"""
4645

4746

47+
__all__ = [
48+
'LABSCRIPT_DEVICES_DIRS',
49+
'labscript_device',
50+
'BLACS_worker',
51+
'BLACS_tab',
52+
'runviewer_parser',
53+
'import_class_by_fullname',
54+
'deprecated_import_alias',
55+
'get_BLACS_tab',
56+
'get_runviewer_parser',
57+
'register_classes',
58+
]
59+
60+
4861
def _get_import_paths(import_names):
4962
"""For the given list of packages, return all folders containing their submodules.
5063
If the packages do not exist, ignore them."""

0 commit comments

Comments
 (0)