Skip to content

Commit d6df97e

Browse files
committed
maint: move patching for autodoc from setup.py to doc/conf.py.
1 parent d2d207d commit d6df97e

File tree

2 files changed

+72
-67
lines changed

2 files changed

+72
-67
lines changed

doc/conf.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,74 @@
88
## notice and this notice are preserved. This file is offered as-is,
99
## without any warranty.
1010

11+
import ctypes
1112
import datetime
1213
import sys
14+
import unittest.mock
1315

1416

1517
sys.path.insert(0, "../microscope")
1618

1719

20+
# autodoc imports the modules to be documented. Modules that wrap the
21+
# device C libraries will load that library. This would require all
22+
# device libraries to be available to build the documentation.
23+
# autodoc can mocks different modules (see `autodoc_mock_imports`) but
24+
# that's not enough for all of our cases.
25+
26+
27+
def patch_cdll():
28+
real_c_dll = ctypes.CDLL
29+
30+
mocked_c_libs = [
31+
# Andor's SDK for (EM)CCD cameras. Loading of this libary
32+
# should be in a separate microcope._wrappers.atmcd module and
33+
# then autodoc could just mock it.
34+
"atmcd32d",
35+
"atmcd32d.so",
36+
"atmcd64d",
37+
"atmcd64d.so",
38+
# pvcam's SDK. Loading of this shared library should be in a
39+
# separate microcope._wrappers.pvcam module and then autodoc
40+
# could just mock it.
41+
"pvcam.so",
42+
"pvcam32",
43+
"pvcam64",
44+
]
45+
46+
def cdll_diversion(name, *args, **kwargs):
47+
if name in mocked_c_libs:
48+
return unittest.mock.MagicMock()
49+
else:
50+
return real_c_dll(name, *args, **kwargs)
51+
52+
ctypes.WinDLL = cdll_diversion
53+
ctypes.CDLL = cdll_diversion
54+
55+
56+
patch_cdll()
57+
58+
59+
def patch_sizeof():
60+
# autodoc is mocking microscope._wrappers.dcamapi4 but we create
61+
# one of these structs for a singleton.
62+
real_sizeof = ctypes.sizeof
63+
64+
def sizeof_diversion(struct):
65+
if (
66+
hasattr(struct, "___sphinx_mock__")
67+
and str(struct) == "microscope._wrappers.dcamapi4.API_INIT"
68+
):
69+
return 40 # doesn't really matter
70+
else:
71+
return real_sizeof(struct)
72+
73+
ctypes.sizeof = sizeof_diversion
74+
75+
76+
patch_sizeof()
77+
78+
1879
# This should ve read from setup.py. Maybe we should use
1980
# pkg_resources to avoid duplication?
2081
author = ""

setup.py

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
## notice and this notice are preserved. This file is offered as-is,
99
## without any warranty.
1010

11-
import ctypes
1211
import distutils.cmd
13-
import unittest.mock
1412

1513
import setuptools
1614
import setuptools.command.sdist
@@ -32,36 +30,6 @@
3230
has_sphinx = False
3331

3432

35-
# List of C libraries that microscope can make use of, and may be
36-
# required to support specific devices. We need to create stubs of
37-
# them to build the documentation. Their names here are their name
38-
# when used to construct ctypes' CDLL and WinDLL.
39-
optional_c_libs = [
40-
# Alpao SDK
41-
"ASDK",
42-
"libasdk.so",
43-
# Andor's atcore (SDK3)
44-
"atcore",
45-
"atcore.so",
46-
# Andor's SDK for (EM)CCD cameras
47-
"atmcd32d",
48-
"atmcd32d.so",
49-
"atmcd64d",
50-
"atmcd64d.so",
51-
# Andor's atutility (SDK3)
52-
"atutility",
53-
"atutility.so",
54-
# Boston Micromachines Corporation (BMC) SDK
55-
"BMC",
56-
"libBMC.so.3",
57-
# Mirao52e SDK (windows only)
58-
"mirao52e",
59-
# pvcam's SDK
60-
"pvcam.so",
61-
"pvcam32",
62-
"pvcam64",
63-
]
64-
6533
# Shadow the sphinx provided command, in order to run sphinx-apidoc
6634
# before sphinx-build. This builds the rst files with the actual
6735
# package inline documentation.
@@ -79,60 +47,33 @@
7947

8048
apidoc_ini_args = ["sphinx-apidoc"]
8149

82-
# Building the documentation using the module docstrings causes
83-
# the modules to be imported. Modules that wrap the optional C
84-
# libraries will try to access the library functions, which means
85-
# that those optional C libraries are required to build the
86-
# documentation. So we patch ctypes.CDLL to intercept a request
87-
# for those libraries and inject a stub instead.
88-
#
89-
# At import time, some of our modules will also make calls to
90-
# functions in the optional C libraries. Because of this, a stub
91-
# for the library is not enough, those functions will need to
92-
# behave well enough to not cause an error during import.
93-
stub_c_attrs = {
94-
"AT_InitialiseLibrary.return_value": 0, # AT_SUCCESS
95-
"AT_InitialiseUtilityLibrary.return_value": 0, # AT_SUCCESS
96-
}
97-
stub_c_dll = unittest.mock.MagicMock()
98-
stub_c_dll.configure_mock(**stub_c_attrs)
99-
100-
real_c_dll = ctypes.CDLL
101-
102-
def cdll_diversion(name, *args, **kwargs):
103-
if name in optional_c_libs:
104-
return stub_c_dll
105-
else:
106-
return real_c_dll(name, *args, **kwargs)
107-
10850
class BuildDoc(sphinx.setup_command.BuildDoc):
10951
def run(self):
11052
apidoc.main(
11153
apidoc_ini_args
11254
+ [
11355
"--separate", # each module on its own page
56+
"--private", # include private modules
11457
"--module-first",
11558
"--tocfile",
11659
"index",
11760
"--output-dir",
11861
"doc/api",
11962
"microscope",
120-
# exclude win32 so docs will build on other platforms
121-
"microscope/win32.py",
12263
# exclude the testsuite
12364
"microscope/testsuite/",
65+
# exclude the wrappers to shared libraries
66+
"microscope/_wrappers/",
12467
# exclude the deprecated devices and deviceserver that
12568
# are kept for backwards compatibility only.
12669
"microscope/devices.py",
12770
"microscope/deviceserver.py",
71+
"microscope/lasers/",
72+
"microscope/cameras/_SDK3.py",
73+
"microscope/cameras/_SDK3Cam.py",
12874
]
12975
)
130-
131-
with unittest.mock.patch("ctypes.CDLL", new=cdll_diversion):
132-
with unittest.mock.patch(
133-
"ctypes.WinDLL", new=cdll_diversion, create=True
134-
):
135-
super().run()
76+
super().run()
13677

13778
else:
13879

@@ -215,5 +156,8 @@ def make_distribution(self):
215156
"source_dir": ("setup.py", "doc"),
216157
},
217158
},
218-
cmdclass={"build_sphinx": BuildDoc, "sdist": sdist},
159+
cmdclass={
160+
"build_sphinx": BuildDoc,
161+
"sdist": sdist,
162+
},
219163
)

0 commit comments

Comments
 (0)