Skip to content

Commit

Permalink
fix: ♻️ adapt python wrapper to cpp interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dubusster committed Nov 15, 2024
1 parent 68b6ebb commit dc28643
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
25 changes: 15 additions & 10 deletions src/python/librir/tools/FileAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
@author: VM213788
"""

from os import PathLike
import numpy as np
from .. import low_level
from ..low_level.misc import *
from .rir_tools import (
attrs_open_file,
attrs_open_buffer,
attrs_close,
attrs_discard,
attrs_global_attribute_count,
Expand All @@ -18,7 +18,7 @@
attrs_global_attribute_value,
attrs_frame_attribute_name,
attrs_frame_attribute_value,
attrs_read_file_reader,
attrs_open_file,
attrs_timestamps,
attrs_set_times,
attrs_set_frame_attributes,
Expand All @@ -39,17 +39,22 @@ class FileAttributes(object):
Use the discard() function to avoid writting attributes to the file.
"""

def __init__(self, filename):
@classmethod
def from_buffer(cls, buffer: bytes):
handle = attrs_open_buffer(buffer)
return cls(handle)

@classmethod
def from_filename(cls, filename: PathLike):
handle = attrs_open_file(filename)
return cls(handle)

def __init__(self, handle):
"""
Constructor.
Create a FileAttributes object from a filename.
"""
self.handle = 0
self.handle = (
attrs_read_file_reader(filename)
if (isinstance(filename, bytes) or (filename == ""))
else attrs_open_file(filename)
)
self.handle = handle

# read timestamps and attributes
self._timestamps = attrs_timestamps(self.handle)
Expand Down
23 changes: 12 additions & 11 deletions src/python/librir/tools/rir_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ def attrs_open_file(filename):
raise RuntimeError("An error occured while calling 'attrs_open_file'")
return tmp

def attrs_open_buffer(buf : bytes):

def attrs_open_buffer(buf: bytes):
"""
Open attributes from an in-memory file.
Attributes are read-only.
"""
res = _tools.attrs_open_from_memory(
ct.cast(ct.c_char_p(buffer),ct.c_void_p), len(buffer))
ct.cast(ct.c_char_p(buf), ct.c_void_p), len(buf)
)
if res == 0:
raise RuntimeError("cannot read attributes from memory")
Expand Down Expand Up @@ -378,7 +379,7 @@ def attrs_timestamps(handle):

def attrs_set_times(handle, times):
_tools.attrs_set_times.argtypes = [ct.c_int, ct.POINTER(ct.c_int64), ct.c_int]
if type(times) != np.ndarray or times.dtype != np.int64:
if isinstance(times, np.ndarray) or (times.dtype != np.int64):
times = np.array(list(times), dtype=np.int64)
tmp = _tools.attrs_set_times(
handle, times.ctypes.data_as(ct.POINTER(ct.c_int64)), int(times.shape[0])
Expand Down Expand Up @@ -415,15 +416,15 @@ def attrs_set_frame_attributes(handle, frame, attributes):
klens = []
vlens = []
for k, v in attributes.items():
if type(k) == bytes:
if isinstance(k, bytes):
ks = k
elif type(k) == str:
elif isinstance(k, str):
ks = k.encode("ascii")
else:
ks = str(k).encode("ascii")
if type(v) == bytes:
if isinstance(v, bytes):
vs = v
elif type(v) == str:
elif isinstance(v, str):
vs = v.encode("ascii")
else:
vs = str(v).encode("ascii")
Expand Down Expand Up @@ -471,15 +472,15 @@ def attrs_set_global_attributes(handle, attributes):
klens = []
vlens = []
for k, v in attributes.items():
if type(k) == bytes:
if isinstance(k, bytes):
ks = k
elif type(k) == str:
elif isinstance(k, str):
ks = k.encode("utf8")
else:
ks = str(k).encode("utf8")
if type(v) == bytes:
if isinstance(v, bytes):
vs = v
elif type(v) == str:
elif isinstance(v, str):
vs = v.encode("utf8")
else:
vs = str(v).encode("utf8")
Expand Down
19 changes: 10 additions & 9 deletions src/python/librir/video_io/IRMovie.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def create_pcr_header(rows, columns, frequency=50, bits=16):
class IRMovie(object):
_header_offset = 1024
__tempfile__ = None
_file_attributes: Union[FileAttributes, None] = None
handle = -1
_calibration_nickname_mapper = {"DL": "Digital Level"}
_th = None
Expand All @@ -75,7 +76,10 @@ class IRMovie(object):
def from_filename(cls, filename):
"""Create an IRMovie object from a local filename"""
handle = open_camera_file(str(filename))
return IRMovie(handle)
instance = IRMovie(handle)
instance._file_attributes = FileAttributes.from_filename(filename)
instance._file_attributes.attributes = get_global_attributes(instance.handle)
return instance

@classmethod
def from_bytes(cls, data: bytes):
Expand All @@ -90,7 +94,11 @@ def from_bytes(cls, data: bytes):
"""

handle = open_camera_memory(data)
return cls(handle)
instance = cls(handle)
instance._file_attributes = FileAttributes.from_buffer(data)
instance._file_attributes.attributes = get_global_attributes(instance.handle)

return instance

@classmethod
def from_numpy_array(
Expand Down Expand Up @@ -148,13 +156,6 @@ def __init__(self, handle):
self._timestamps = None
self._camstatus = None
self._frame_attributes_d = {}

self._file_attributes = (
FileAttributes(self.filename)
if self.filename is not None
else FileAttributes(self.handle)
)
self._file_attributes.attributes = get_global_attributes(self.handle)
self._registration_file = None

self.calibration = "DL"
Expand Down

0 comments on commit dc28643

Please sign in to comment.