Remove on_{begin,end}_{string,number}

And add `done` arg to data callback
This commit is contained in:
2025-05-25 21:01:37 -04:00
parent f92b33eec3
commit f6cd807da3
6 changed files with 95 additions and 154 deletions

View File

@@ -5,21 +5,19 @@ import os
from typing import Optional
event_callback = ctypes.CFUNCTYPE(None, ctypes.c_void_p)
data_callback = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int)
data_callback = ctypes.CFUNCTYPE(
None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int
)
class WeaselJsonCallbacks(ctypes.Structure):
_fields_ = [
("on_begin_object", event_callback),
("on_end_object", event_callback),
("on_begin_string", event_callback),
("on_string_data", data_callback),
("on_end_string", event_callback),
("on_begin_array", event_callback),
("on_end_array", event_callback),
("on_begin_number", event_callback),
("on_number_data", data_callback),
("on_end_number", event_callback),
("on_true_literal", event_callback),
("on_false_literal", event_callback),
("on_null_literal", event_callback),
@@ -40,13 +38,7 @@ class WeaselJsonCallbacksBase:
def on_end_object(self):
pass
def on_begin_string(self):
pass
def on_string_data(self, data):
pass
def on_end_string(self):
def on_string_data(self, data, done):
pass
def on_begin_array(self):
@@ -55,13 +47,7 @@ class WeaselJsonCallbacksBase:
def on_end_array(self):
pass
def on_begin_number(self):
pass
def on_number_data(self, data):
pass
def on_end_number(self):
def on_number_data(self, data, done):
pass
def on_true_literal(self):
@@ -155,22 +141,10 @@ def on_end_object(p):
self.on_end_object()
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
def on_begin_string(p):
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int)
def on_string_data(p, buf, len, done):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_begin_string()
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int)
def on_string_data(p, buf, len):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_string_data(bytes(ctypes.string_at(buf, len)))
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
def on_end_string(p):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_end_string()
self.on_string_data(bytes(ctypes.string_at(buf, len)), bool(done))
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
@@ -185,22 +159,10 @@ def on_end_array(p):
self.on_end_array()
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
def on_begin_number(p):
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int)
def on_number_data(p, buf, len, done):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_begin_number()
@ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int)
def on_number_data(p, buf, len):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_number_data(bytes(ctypes.string_at(buf, len)))
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
def on_end_number(p):
self = ctypes.cast(p, ctypes.POINTER(ctypes.py_object)).contents.value
self.on_end_number()
self.on_number_data(bytes(ctypes.string_at(buf, len)), bool(done))
@ctypes.CFUNCTYPE(None, ctypes.c_void_p)
@@ -224,14 +186,10 @@ def on_null_literal(p):
c_callbacks = WeaselJsonCallbacks(
on_begin_object,
on_end_object,
on_begin_string,
on_string_data,
on_end_string,
on_begin_array,
on_end_array,
on_begin_number,
on_number_data,
on_end_number,
on_true_literal,
on_false_literal,
on_null_literal,
@@ -240,7 +198,7 @@ c_callbacks = WeaselJsonCallbacks(
class MyCallbacks(WeaselJsonCallbacksBase):
# override callbacks
def on_string_data(self, data):
def on_string_data(self, data, done):
print(data)