Skip to content

Commit

Permalink
Added missing bindings for XLinkError_t and Pylance typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
themarpe committed Oct 4, 2022
1 parent 00ad914 commit 6ac7f89
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
44 changes: 36 additions & 8 deletions generate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
)
import typing
import json
json = dict
from pathlib import Path
from typing import Set
''') + contents

# Create 'create' overloads
Expand All @@ -59,20 +61,46 @@

# Writeout changes
file.seek(0)
file.truncate(0)
file.write(final_stubs)

# node fixes
with open(f'{DIRECTORY}/depthai/node.pyi' ,'r+') as file:
# Read
contents = file.read()

# Add imports
stubs_import = textwrap.dedent('''
from pathlib import Path
from typing import Set
''') + contents

# Remove import depthai.*
final_stubs = re.sub(r"import depthai\.\S*", "", stubs_import)

# Writeout changes
file.seek(0)
file.truncate(0)
file.write(final_stubs)

# Flush previous stdout
sys.stdout.flush()

# Check syntax
# Check syntax (Mypy and later Pylance/Pyright)
# Windows limitation - another process cannot normally read temporary file that is opened by this process
# Close first and delete manually afterwards
config = tempfile.NamedTemporaryFile(mode='w', delete=False)
config.write('[mypy]\nignore_errors = True\n')
config.close()
print(f'Mypy config file: {config.name}')
subprocess.check_call([sys.executable, '-m' 'mypy', f'{DIRECTORY}/{MODULE_NAME}', f'--config-file={config.name}'], env=env)
os.unlink(config.name)
try:
config = tempfile.NamedTemporaryFile(mode='w', delete=False)
config.write('[mypy]\nignore_errors = True\n')
config.close()
print(f'Mypy config file: {config.name}')
# Mypy check
subprocess.check_call([sys.executable, '-m' 'mypy', f'{DIRECTORY}/{MODULE_NAME}', f'--config-file={config.name}'], env=env)
finally:
os.unlink(config.name)

# # TODO(thamarpe) - Pylance / Pyright check
# subprocess.check_call([sys.executable, '-m' 'pyright', f'{DIRECTORY}/{MODULE_NAME}'], env=env)

except subprocess.CalledProcessError as err:
exit(err.returncode)
Expand Down
21 changes: 21 additions & 0 deletions src/XLinkBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void XLinkBindings::bind(pybind11::module &m, void *pCallstack)
py::enum_<XLinkDeviceState_t> xLinkDeviceState(m, "XLinkDeviceState");
py::enum_<XLinkProtocol_t> xLinkProtocol(m, "XLinkProtocol");
py::enum_<XLinkPlatform_t> xLinkPlatform(m, "XLinkPlatform");
py::enum_<XLinkError_t> xLinkError(m, "XLinkError_t");
py::class_<XLinkConnection, std::shared_ptr<XLinkConnection> > xLinkConnection(m, "XLinkConnection", DOC(dai, XLinkConnection));

// pybind11 limitation of having actual classes as exceptions
Expand Down Expand Up @@ -138,6 +139,26 @@ void XLinkBindings::bind(pybind11::module &m, void *pCallstack)
.def_static("bootBootloader", &XLinkConnection::bootBootloader, py::arg("devInfo"))
;

xLinkError
.value("X_LINK_SUCCESS", X_LINK_SUCCESS)
.value("X_LINK_ALREADY_OPEN", X_LINK_ALREADY_OPEN)
.value("X_LINK_COMMUNICATION_NOT_OPEN", X_LINK_COMMUNICATION_NOT_OPEN)
.value("X_LINK_COMMUNICATION_FAIL", X_LINK_COMMUNICATION_FAIL)
.value("X_LINK_COMMUNICATION_UNKNOWN_ERROR", X_LINK_COMMUNICATION_UNKNOWN_ERROR)
.value("X_LINK_DEVICE_NOT_FOUND", X_LINK_DEVICE_NOT_FOUND)
.value("X_LINK_TIMEOUT", X_LINK_TIMEOUT)
.value("X_LINK_ERROR", X_LINK_ERROR)
.value("X_LINK_OUT_OF_MEMORY", X_LINK_OUT_OF_MEMORY)
.value("X_LINK_INSUFFICIENT_PERMISSIONS", X_LINK_INSUFFICIENT_PERMISSIONS)
.value("X_LINK_DEVICE_ALREADY_IN_USE", X_LINK_DEVICE_ALREADY_IN_USE)
.value("X_LINK_NOT_IMPLEMENTED", X_LINK_NOT_IMPLEMENTED)
.value("X_LINK_INIT_USB_ERROR", X_LINK_INIT_USB_ERROR)
.value("X_LINK_INIT_TCP_IP_ERROR", X_LINK_INIT_TCP_IP_ERROR)
.value("X_LINK_INIT_PCIE_ERROR", X_LINK_INIT_PCIE_ERROR)
.export_values()
;


//// Exceptions

// Applicable if above pybind11 limitation is removed
Expand Down

0 comments on commit 6ac7f89

Please sign in to comment.