-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy path_utils.py
More file actions
172 lines (140 loc) · 5.77 KB
/
_utils.py
File metadata and controls
172 lines (140 loc) · 5.77 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
## Copyright (C) 2020 David Miguel Susano Pinto <[email protected]>
##
## This file is part of Microscope.
##
## Microscope is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Microscope is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Microscope. If not, see <http://www.gnu.org/licenses/>.
import ctypes
import os
import sys
import threading
from typing import List, Optional, Type
import serial
import microscope
import microscope.abc
# Both pySerial and serial distribution packages install an import
# package named serial. If both are installed we may have imported
# the wrong one. Check it to provide a better error message. See
# issue #232.
if not hasattr(serial, "Serial"):
if hasattr(serial, "marshall"):
raise microscope.MicroscopeError(
"incorrect imported package serial. It appears that the serial"
" package from the distribution serial, instead of pyserial, was"
" imported. Consider uninstalling serial and installing pySerial."
)
else:
raise microscope.MicroscopeError(
"imported package serial does not have Serial class"
)
def library_loader(
libname: str, dlltype: Type[ctypes.CDLL] = ctypes.CDLL, **kwargs
) -> ctypes.CDLL:
"""Load shared library.
This exists mainly to search for DLL in Windows using a standard
search path, i.e, search for dlls in ``PATH``.
Args:
libname: file name or path of the library to be loaded as
required by `dlltype`
dlltype: the class of shared library to load. Typically,
`ctypes.CDLL` but sometimes `ctypes.WinDLL` in windows.
kwargs: other arguments passed on to `dlltype`.
"""
# Python 3.8 in Windows uses an altered search path. Their
# reasons is security and I guess it would make sense if we
# installed the DLLs we need ourselves but we don't. `winmode=0`
# restores the use of the standard search path. See issue #235.
if (
os.name == "nt"
and sys.version_info >= (3, 8)
and "winmode" not in kwargs
):
winmode_kwargs = {"winmode": 0}
else:
winmode_kwargs = {}
return dlltype(libname, **winmode_kwargs, **kwargs)
class OnlyTriggersOnceOnSoftwareMixin(microscope.abc.TriggerTargetMixin):
"""Utility mixin for devices that only trigger "once" with software.
This mixin avoids code duplication for the many devices whose only
supported trigger type and trigger mode are `TriggerType.SOFTWARE`
and `TriggerMode.ONCE`.
"""
@property
def trigger_type(self) -> microscope.TriggerType:
return microscope.TriggerType.SOFTWARE
@property
def trigger_mode(self) -> microscope.TriggerMode:
return microscope.TriggerMode.ONCE
def set_trigger(
self, ttype: microscope.TriggerType, tmode: microscope.TriggerMode
) -> None:
if ttype is not microscope.TriggerType.SOFTWARE:
raise microscope.UnsupportedFeatureError(
"the only trigger type supported is software"
)
if tmode is not microscope.TriggerMode.ONCE:
raise microscope.UnsupportedFeatureError(
"the only trigger mode supported is 'once'"
)
class OnlyTriggersBulbOnSoftwareMixin(microscope.abc.TriggerTargetMixin):
"""Utility mixin for devices that only trigger "bulb" with software.
This mixin avoids code duplication for the many devices whose only
supported trigger type and trigger mode are `TriggerType.SOFTWARE`
and `TriggerMode.BULB`.
"""
@property
def trigger_type(self) -> microscope.TriggerType:
return microscope.TriggerType.SOFTWARE
@property
def trigger_mode(self) -> microscope.TriggerMode:
return microscope.TriggerMode.BULB
def set_trigger(
self, ttype: microscope.TriggerType, tmode: microscope.TriggerMode
) -> None:
if ttype is not microscope.TriggerType.SOFTWARE:
raise microscope.UnsupportedFeatureError(
"the only trigger type supported is software"
)
if tmode is not microscope.TriggerMode.BULB:
raise microscope.UnsupportedFeatureError(
"the only trigger mode supported is 'bulb'"
)
def _do_trigger(self) -> None:
raise microscope.IncompatibleStateError(
"trigger does not make sense in trigger mode bulb, only enable"
)
class SharedSerial:
"""Wraps a `Serial` instance with a lock for synchronization."""
def __init__(self, serial: serial.Serial) -> None:
self._serial = serial
self._lock = threading.RLock()
@property
def lock(self) -> threading.RLock:
return self._lock
def readline(self) -> bytes:
with self._lock:
return self._serial.readline()
def readlines(self, hint: int = -1) -> List[bytes]:
with self._lock:
return self._serial.readlines(hint)
# Beware: pySerial 3.5 changed the named of its first argument
# from terminator to expected. See issue #233.
def read_until(
self, terminator: bytes = b"\n", size: Optional[int] = None
) -> bytes:
with self._lock:
return self._serial.read_until(terminator, size=size)
def write(self, data: bytes) -> int:
with self._lock:
return self._serial.write(data)