Skip to content

Commit

Permalink
Merge pull request #235 from ap--/precommit-cython-lint
Browse files Browse the repository at this point in the history
pre-commit add cython-lint
  • Loading branch information
ap-- committed Feb 21, 2024
2 parents 2d9222c + 616b994 commit 89b83d6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Expand Up @@ -38,3 +38,8 @@ repos:
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/MarcoGorelli/cython-lint
rev: v0.16.0
hooks:
- id: cython-lint
- id: double-quote-cython-strings
3 changes: 3 additions & 0 deletions pyproject.toml
Expand Up @@ -72,3 +72,6 @@ ignore_missing_imports = true
[[tool.mypy.overrides]]
module = "usb.*"
ignore_missing_imports = true

[tool.cython-lint]
ignore = ['E501']
9 changes: 4 additions & 5 deletions src/seabreeze/cseabreeze/c_seabreeze.pxd
Expand Up @@ -13,9 +13,9 @@ from libcpp cimport bool

cdef extern from "api/USBEndpointTypes.h":
cpdef enum usbEndpointType:
kEndpointTypePrimaryOut, # slow speed
kEndpointTypePrimaryOut, # slow speed
kEndpointTypePrimaryIn, # slow speed
kEndpointTypeSecondaryOut, # could be high speed
kEndpointTypeSecondaryOut, # could be high speed
kEndpointTypeSecondaryIn, # could be high speed
kEndpointTypeSecondaryIn2 # generally high speed

Expand All @@ -26,6 +26,7 @@ cdef extern from "api/seabreezeapi/SeaBreezeAPI.h":

@staticmethod
SeaBreezeAPI* getInstance() except +

@staticmethod
void shutdown() except +

Expand Down Expand Up @@ -55,7 +56,6 @@ cdef extern from "api/seabreezeapi/SeaBreezeAPI.h":
int rawUSBBusAccessRead(long deviceID, long featureID, int *errorCode, unsigned char *buffer, unsigned int bufferLength, unsigned char endpoint)
int rawUSBBusAccessWrite(long deviceID, long featureID, int *errorCode, unsigned char *buffer, unsigned int bufferLength, unsigned char endpoint)


# Spectrometer capabilities
int getNumberOfSpectrometerFeatures(long id, int *errorCode)
int getSpectrometerFeatures(long deviceID, int *errorCode, long *buffer, unsigned int maxLength)
Expand All @@ -66,7 +66,7 @@ cdef extern from "api/seabreezeapi/SeaBreezeAPI.h":
double spectrometerGetMaximumIntensity(long deviceID, long spectrometerFeatureID, int *errorCode)
int spectrometerGetUnformattedSpectrumLength(long deviceID, long spectrometerFeatureID, int *errorCode)
# int spectrometerGetUnformattedSpectrum(long deviceID, long spectrometerFeatureID, int *errorCode, unsigned char *buffer, int bufferLength)
int spectrometerGetFastBufferSpectrum(long deviceID, long spectrometerFeatureID, int *errorCode, unsigned char *dataBuffer, int dataMaxLength, unsigned int numberOfSampleToRetrieve) # currently 15 max
int spectrometerGetFastBufferSpectrum(long deviceID, long spectrometerFeatureID, int *errorCode, unsigned char *dataBuffer, int dataMaxLength, unsigned int numberOfSampleToRetrieve) # currently 15 max
int spectrometerGetFormattedSpectrumLength(long deviceID, long spectrometerFeatureID, int *errorCode)
int spectrometerGetFormattedSpectrum(long deviceID, long spectrometerFeatureID, int *errorCode, double *buffer, int bufferLength) nogil
int spectrometerGetWavelengths(long deviceID, long spectrometerFeatureID, int *errorCode, double *wavelengths, int length) nogil
Expand Down Expand Up @@ -225,7 +225,6 @@ cdef extern from "api/seabreezeapi/SeaBreezeAPI.h":
int introspectionOpticalDarkPixelRangesGet(long deviceID, long featureID, int *errorCode, unsigned int *pixelIndexPairs, int maxLength)
int introspectionElectricDarkPixelRangesGet(long deviceID, long featureID, int *errorCode, unsigned int *pixelIndexPairs, int maxLength)


# Spectrum processing capabilities
int getNumberOfSpectrumProcessingFeatures(long deviceID, int *errorCode)
int getSpectrumProcessingFeatures(long deviceID, int *errorCode, long *buffer, unsigned int maxLength)
Expand Down
70 changes: 31 additions & 39 deletions src/seabreeze/cseabreeze/c_seabreeze_wrapper.pyx
Expand Up @@ -190,7 +190,6 @@ cdef class SeaBreezeAPI(object):
"""
cdef int output
cdef bytes c_devtype
cdef bytes c_buspath
cdef int c_port
c_devtype = bytes(device_type)
c_ipaddr = bytes(ip_address)
Expand Down Expand Up @@ -258,8 +257,8 @@ cdef class SeaBreezeAPI(object):
if err.error_code == _ErrorCode.NO_DEVICE:
# device used by another thread?
continue
model = dev.model
serial = dev.serial_number
_ = dev.model
_ = dev.serial_number
if not was_open_before:
dev.close()
devices.append(dev)
Expand All @@ -275,7 +274,6 @@ cdef class SeaBreezeAPI(object):
devices: list of str
list of model names that are supported by this backend
"""
cdef int num_models
cdef int error_code
cdef char c_buffer[_MAXBUFLEN]
cdef int bytes_written
Expand All @@ -290,7 +288,7 @@ cdef class SeaBreezeAPI(object):
raise SeaBreezeError(error_code=error_code)

serial = c_buffer[:bytes_written]
output.append(serial.decode("utf-8").rstrip('\x00'))
output.append(serial.decode("utf-8").rstrip("\x00"))
return output


Expand Down Expand Up @@ -436,7 +434,7 @@ cdef class SeaBreezeDevice(object):
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
serial = c_buffer[:bytes_written]
return serial.decode("utf-8").rstrip('\x00')
return serial.decode("utf-8").rstrip("\x00")

def get_model(self):
"""return the model string of the spectrometer
Expand Down Expand Up @@ -494,6 +492,8 @@ cdef class SeaBreezeDevice(object):

# create only one SeaBreezeDevice instance per handle
_seabreeze_device_instance_registry = weakref.WeakValueDictionary()


def _seabreeze_device_factory(handle):
"""return existing instances instead of creating temporary ones"""
try:
Expand Down Expand Up @@ -575,11 +575,11 @@ cdef class SeaBreezeRawUSBBusAccessFeature(SeaBreezeFeature):
cdef int error_code
cdef unsigned char out
ep_map = {
'primary_out': csb.kEndpointTypePrimaryOut, # slow speed
'primary_in': csb.kEndpointTypePrimaryIn, # slow speed
'secondary_out': csb.kEndpointTypeSecondaryOut, # could be high speed
'secondary_in': csb.kEndpointTypeSecondaryIn, # could be high speed
'secondary_in2': csb.kEndpointTypeSecondaryIn2 # generally high speed
"primary_out": csb.kEndpointTypePrimaryOut, # slow speed
"primary_in": csb.kEndpointTypePrimaryIn, # slow speed
"secondary_out": csb.kEndpointTypeSecondaryOut, # could be high speed
"secondary_in": csb.kEndpointTypeSecondaryIn, # could be high speed
"secondary_in2": csb.kEndpointTypeSecondaryIn2 # generally high speed
}
if endpoint not in ep_map.keys():
raise ValueError("endpoint not in %s" % str(ep_map.keys()))
Expand Down Expand Up @@ -697,7 +697,6 @@ cdef class SeaBreezeSpectrometerFeature(SeaBreezeFeature):
cdef int error_code
cdef int cmode
cmode = int(mode)
cdef unsigned long device_id, feature_id
with nogil:
self.sbapi.spectrometerSetTriggerMode(self.device_id, self.feature_id, &error_code, cmode)
if error_code != 0:
Expand Down Expand Up @@ -832,16 +831,15 @@ cdef class SeaBreezeSpectrometerFeature(SeaBreezeFeature):
wavelengths: `np.ndarray`
"""
cdef int error_code
cdef int bytes_written
cdef double[::1] out
cdef int out_length

wavelengths = np.zeros((self._spectrum_length, ), dtype=np.double)
out = wavelengths
out_length = wavelengths.size
with nogil:
bytes_written = self.sbapi.spectrometerGetWavelengths(self.device_id, self.feature_id, &error_code,
&out[0], out_length)
_ = self.sbapi.spectrometerGetWavelengths(self.device_id, self.feature_id, &error_code,
&out[0], out_length)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
return wavelengths
Expand Down Expand Up @@ -925,7 +923,7 @@ cdef class SeaBreezeSpectrometerFeature(SeaBreezeFeature):
for _ in range(sample_number):
# decode metadata
sm = SpectrumMetadata(
*struct.unpack('HHIQIIIIQH', data[offset : offset + 42])
*struct.unpack("HHIQIIIIQH", data[offset : offset + 42])
)

# determine pixel data format from metadata and decode spectrum
Expand Down Expand Up @@ -1215,7 +1213,7 @@ cdef class SeaBreezeIrradCalFeature(SeaBreezeFeature):
bytes_written = self.sbapi.irradCalibrationRead(self.device_id, self.feature_id, &error_code,
&out[0], out_length)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
raise SeaBreezeError(error_code=error_code)
return irrad_calibration[:bytes_written]

def write_calibration(self, calibration_array):
Expand All @@ -1230,18 +1228,17 @@ cdef class SeaBreezeIrradCalFeature(SeaBreezeFeature):
None
"""
cdef int error_code
cdef int bytes_written
cdef float[::1] out
cdef int out_length
arr = np.asarray(calibration_array, dtype=np.float32)
if not arr.ndim == 1:
raise ValueError("calibration_array needs to be 1D")
out = arr
out_length = arr.size
bytes_written = self.sbapi.irradCalibrationWrite(self.device_id, self.feature_id, &error_code,
&out[0], out_length)
_ = self.sbapi.irradCalibrationWrite(self.device_id, self.feature_id, &error_code,
&out[0], out_length)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
raise SeaBreezeError(error_code=error_code)


cdef class SeaBreezeEthernetConfigurationFeature(SeaBreezeFeature):
Expand Down Expand Up @@ -1318,7 +1315,7 @@ cdef class SeaBreezeEthernetConfigurationFeature(SeaBreezeFeature):
cdef unsigned char c_interface_index
c_interface_index = int(interface_index)
# convert mac address
mbytes = map(lambda x: chr(int(x, 16)), mac_address.split(':'))
mbytes = map(lambda x: chr(int(x, 16)), mac_address.split(":"))
assert len(mbytes) == 6
cbytes = bytes("".join(mbytes))[:6]
cdef unsigned char macAddress[6]
Expand Down Expand Up @@ -1561,7 +1558,6 @@ cdef class SeaBreezeIPv4Feature(SeaBreezeFeature):
cdef unsigned char c_interface_index
cdef unsigned char addressIndex
cdef unsigned char(*ipv4_address)[4]
cdef unsigned char* netMask
cdef unsigned char* view_addr
c_interface_index = int(interface_index)
addressIndex = int(address_index)
Expand Down Expand Up @@ -1636,7 +1632,7 @@ cdef class SeaBreezeIPv4Feature(SeaBreezeFeature):
cdef unsigned char c_interface_index
cdef unsigned char address[4]
c_interface_index = int(interface_index)
mbytes = map(int, default_gateway_address.split('.'))
mbytes = map(int, default_gateway_address.split("."))
assert len(mbytes) == 4
cbytes = bytes("".join(mbytes))[:4]
address = <const unsigned char*> PyMem_Malloc(4 * sizeof(unsigned char))
Expand Down Expand Up @@ -1669,13 +1665,13 @@ cdef class SeaBreezeIPv4Feature(SeaBreezeFeature):
cdef unsigned char netMask
c_interface_index = int(interface_index)

addr_nm = ipv4_address.split('/')
addr_nm = ipv4_address.split("/")
if len(addr_nm) == 1:
nm = 24 # default netmask
else:
nm = int(addr_nm[1])
netMask = nm
mbytes = map(int, addr_nm[0].split('.'))
mbytes = map(int, addr_nm[0].split("."))
assert len(mbytes) == 4
cbytes = bytes("".join(mbytes))[:4]
address = <const unsigned char*> PyMem_Malloc(4 * sizeof(unsigned char))
Expand Down Expand Up @@ -1794,13 +1790,13 @@ cdef class SeaBreezeDHCPServerFeature(SeaBreezeFeature):
cdef unsigned char netMask
c_interface_index = int(interface_index)

addr_nm = server_address.split('/')
addr_nm = server_address.split("/")
if len(addr_nm) == 1:
nm = 24 # default netmask
else:
nm = int(addr_nm[1])
netMask = nm
mbytes = map(int, addr_nm[0].split('.'))
mbytes = map(int, addr_nm[0].split("."))
assert len(mbytes) == 4
cbytes = bytes("".join(mbytes))[:4]
address = <const unsigned char*> PyMem_Malloc(4 * sizeof(unsigned char))
Expand Down Expand Up @@ -2034,7 +2030,7 @@ cdef class SeaBreezeWifiConfigurationFeature(SeaBreezeFeature):
Returns
-------
wifi_mode : int
mode `{0: 'client', 1: 'access point'}`
mode `{0: 'client', 1: 'access point'}`
"""
cdef int error_code
cdef unsigned char output
Expand All @@ -2053,7 +2049,7 @@ cdef class SeaBreezeWifiConfigurationFeature(SeaBreezeFeature):
----------
interface_index : int
wifi_mode : int
mode `{0: 'client', 1: 'access point'}`
mode `{0: 'client', 1: 'access point'}`
Returns
-------
Expand All @@ -2079,7 +2075,7 @@ cdef class SeaBreezeWifiConfigurationFeature(SeaBreezeFeature):
Returns
-------
security_type : int
security_type `{0: 'open', 1: 'WPA2'}`
security_type `{0: 'open', 1: 'WPA2'}`
"""
cdef int error_code
cdef unsigned char output
Expand All @@ -2098,7 +2094,7 @@ cdef class SeaBreezeWifiConfigurationFeature(SeaBreezeFeature):
----------
interface_index : int
security_type : int
security_type `{0: 'open', 1: 'WPA2'}`
security_type `{0: 'open', 1: 'WPA2'}`
Returns
-------
Expand Down Expand Up @@ -2290,7 +2286,6 @@ cdef class SeaBreezeGPIOFeature(SeaBreezeFeature):
if error_code != 0:
raise SeaBreezeError(error_code=error_code)


# unsigned int getGPIO_ValueVector(long deviceID, long featureID, int *errorCode)
def get_gpio_value_vector(self):
"""get gpio value vector
Expand Down Expand Up @@ -2434,7 +2429,7 @@ cdef class SeaBreezeGPIOFeature(SeaBreezeFeature):
pinNumber = int(pin_number)
c_mode = int(mode)
c_value = int(value)
self.sbapi.setEGPIO_Mode(self.device_id, self.feature_id, &error_code, pinNumber, c_mode, value)
self.sbapi.setEGPIO_Mode(self.device_id, self.feature_id, &error_code, pinNumber, c_mode, c_value)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)

Expand Down Expand Up @@ -2477,7 +2472,6 @@ cdef class SeaBreezeGPIOFeature(SeaBreezeFeature):
if error_code != 0:
raise SeaBreezeError(error_code=error_code)


# float getEGPIO_Value(long deviceID, long featureID, int *errorCode, unsigned char pinNumber)
def get_egpio_value(self, pin_number):
"""return the value of an egpio pin
Expand Down Expand Up @@ -2573,7 +2567,7 @@ cdef class SeaBreezeEEPROMFeature(SeaBreezeFeature):
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
if strip_zero_bytes:
return c_buffer[:bytes_written].strip('\x00')
return c_buffer[:bytes_written].strip("\x00")
return c_buffer[:bytes_written]


Expand Down Expand Up @@ -2653,7 +2647,6 @@ cdef class SeaBreezeLightSourceFeature(SeaBreezeFeature):
is_enabled: bool
"""
cdef int error_code
cdef bool_t is_enable
is_enabled = self.sbapi.lightSourceIsEnabled(self.device_id, self.feature_id, &error_code, light_source_index)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
Expand Down Expand Up @@ -2780,7 +2773,6 @@ cdef class SeaBreezeStrobeLampFeature(SeaBreezeFeature):
raise SeaBreezeError(error_code=error_code)



cdef class SeaBreezeContinuousStrobeFeature(SeaBreezeFeature):

identifier = "continuous_strobe"
Expand Down Expand Up @@ -3404,7 +3396,7 @@ cdef class SeaBreezeOpticalBenchFeature(SeaBreezeFeature):
cdef int bytes_written
cdef char buffer[_MAXBUFLEN]
bytes_written = self.sbapi.opticalBenchGetGrating(self.device_id, self.feature_id, &error_code,
&buffer[0], _MAXBUFLEN)
&buffer[0], _MAXBUFLEN)
if error_code != 0:
raise SeaBreezeError(error_code=error_code)
assert bytes_written < _MAXBUFLEN, "BUG: should increase hardcoded buffer size"
Expand Down

0 comments on commit 89b83d6

Please sign in to comment.