changeset 192:b0f096d2339e java-code-model

documentation of orthanc.RegisterRestCallback()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jul 2024 18:25:53 +0200
parents 30ed49f2487f
children 66cd20e970d8
files CodeAnalysis/CustomFunctions.json CodeAnalysis/GenerateOrthancSDK.py CodeAnalysis/GlobalFunctions.mustache CodeAnalysis/PythonDocumentation.mustache Sources/Autogenerated/orthanc.pyi Sources/Autogenerated/sdk_GlobalFunctions.impl.h Sources/Plugin.cpp
diffstat 7 files changed, 75 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/CodeAnalysis/CustomFunctions.json	Tue Jul 02 18:00:52 2024 +0200
+++ b/CodeAnalysis/CustomFunctions.json	Tue Jul 02 18:25:53 2024 +0200
@@ -1,2 +1,25 @@
 [
+  {
+    "short_name" : "RegisterRestCallback",
+    "implementation" : "RegisterRestCallback",
+    "args" : [
+      {
+        "sdk_name" : "pathRegularExpression",
+        "sdk_type" : "const char *"
+      },
+      {
+        "sdk_name" : "callback",
+        "sdk_type" : "Callable",
+        "callable_type" : "typing.Callable[[RestOutput, str], None]"
+      }
+    ],
+    "documentation" : {
+      "args" : {
+        "pathRegularExpression" : "Regular expression for the URI. May contain groups.",
+        "callback" : "The callback function to handle the REST call."
+      },
+      "description" : [ "Register a REST callback." ]
+    },
+    "return_sdk_type" : "void"
+  }
 ]
--- a/CodeAnalysis/GenerateOrthancSDK.py	Tue Jul 02 18:00:52 2024 +0200
+++ b/CodeAnalysis/GenerateOrthancSDK.py	Tue Jul 02 18:25:53 2024 +0200
@@ -172,6 +172,8 @@
             arg_type = GetShortName(a['sdk_class'])
         elif a['sdk_type'] in [ 'int32_t', 'uint32_t', 'uint8_t', 'uint16_t', 'uint64_t' ]:
             arg_type = 'int'
+        elif a['sdk_type'] == 'Callable':
+            arg_type = a['callable_type']
         else:
             raise Exception('Argument type not implemented: %s' % a['sdk_type'])
         args_declaration.append('%s: %s' % (arg_name, arg_type))
@@ -264,6 +266,12 @@
                 'initialization' : ' = 0',
             })
             tuple_format += t['format']
+        elif arg['sdk_type'] == 'Callable':
+            # This is only used to generate the documentation file "orthanc.pyi"
+            args.append({
+                'name' : arg['name'],
+            })
+            tuple_format += 'O'
         else:
             print('Ignoring function with unsupported argument type: %s(), type = %s' % (f['c_function'], arg['sdk_type']))
             return None
@@ -319,6 +327,7 @@
 
 
 globalFunctions = []
+customFunctions = []
 
 for f in model['global_functions']:
     g = FormatFunction(f)
@@ -326,9 +335,8 @@
         globalFunctions.append(g)
 
 for f in CUSTOM_FUNCTIONS:
-    g = FormatFunction(f)
-    if g != None:
-        globalFunctions.append(g)
+    f['documentation'] = DocumentFunction(f)
+    customFunctions.append(f)
 
 
 enumerations = []
@@ -410,11 +418,13 @@
 sortedClasses = sorted(classes, key = lambda x: x['class_name'])
 sortedEnumerations = sorted(enumerations, key = lambda x: x['name'])
 sortedGlobalFunctions = sorted(globalFunctions, key = lambda x: x['c_function'])
+sortedCustomFunctions = sorted(customFunctions, key = lambda x: x['short_name'])
 
 with open(os.path.join(ROOT, 'GlobalFunctions.mustache'), 'r') as f:
     with open(os.path.join(TARGET, 'sdk_GlobalFunctions.impl.h'), 'w') as h:
         h.write(renderer.render(f.read(), {
             'global_functions' : sortedGlobalFunctions,
+            'custom_functions' : sortedCustomFunctions,
         }))
 
 with open(os.path.join(ROOT, 'sdk.cpp.mustache'), 'r') as f:
@@ -423,6 +433,7 @@
             'classes' : sortedClasses,
             'enumerations' : sortedEnumerations,
             'global_functions' : sortedGlobalFunctions,
+            'custom_functions' : sortedCustomFunctions,
         }))
 
 with open(os.path.join(ROOT, 'sdk.h.mustache'), 'r') as f:
@@ -437,6 +448,7 @@
             'classes' : sortedClasses,
             'enumerations' : sortedEnumerations,
             'global_functions' : sortedGlobalFunctions,
+            'custom_functions' : sortedCustomFunctions,
         }))
 
 
@@ -444,6 +456,6 @@
 for c in sortedClasses:
     countMethods += len(c['methods'])
 
-print('\nNumber of wrapped global functions: %d' % len(sortedGlobalFunctions))
+print('\nNumber of wrapped global functions: %d' % len(sortedGlobalFunctions + sortedCustomFunctions))
 print('Number of wrapped methods: %d\n' % countMethods)
-print('Total number of wrapped global functions or methods: %d\n' % (len(sortedGlobalFunctions) + countMethods))
+print('Total number of wrapped global functions or methods: %d\n' % (len(sortedGlobalFunctions + sortedCustomFunctions) + countMethods))
--- a/CodeAnalysis/GlobalFunctions.mustache	Tue Jul 02 18:00:52 2024 +0200
+++ b/CodeAnalysis/GlobalFunctions.mustache	Tue Jul 02 18:25:53 2024 +0200
@@ -22,6 +22,13 @@
 // WARNING: Auto-generated file. Do not modify it by hand.
 
 
+// Forward declaration of the custom global functions
+{{#custom_functions}}
+extern PyObject *{{implementation}}(PyObject* module, PyObject *args);
+{{/custom_functions}}
+// End of forward declarations
+
+
 {{#global_functions}}
 static PyObject* sdk_{{c_function}}(PyObject* module, PyObject* args)
 {
@@ -38,6 +45,10 @@
   { "{{short_name}}", sdk_{{c_function}}, METH_VARARGS,
     "Generated from C function {{c_function}}()" },
 {{/global_functions}}
+{{#custom_functions}}
+  { "{{short_name}}", {{implementation}}, METH_VARARGS,
+    "Implemented in C++ function {{implementation}}()" },
+{{/custom_functions}}
   { NULL, NULL }
 };
 
--- a/CodeAnalysis/PythonDocumentation.mustache	Tue Jul 02 18:00:52 2024 +0200
+++ b/CodeAnalysis/PythonDocumentation.mustache	Tue Jul 02 18:25:53 2024 +0200
@@ -23,6 +23,8 @@
 
 
 import enum
+import typing
+
 
 {{#enumerations}}
 
@@ -43,6 +45,9 @@
 {{#global_functions}}
 {{> function_documentation}}
 {{/global_functions}}
+{{#custom_functions}}
+{{> function_documentation}}
+{{/custom_functions}}
 
 
 {{#classes}}
--- a/Sources/Autogenerated/orthanc.pyi	Tue Jul 02 18:00:52 2024 +0200
+++ b/Sources/Autogenerated/orthanc.pyi	Tue Jul 02 18:25:53 2024 +0200
@@ -23,6 +23,8 @@
 
 
 import enum
+import typing
+
 
 
 class ChangeType(enum.Enum):
@@ -2088,6 +2090,16 @@
       data (bytes): The content of the memory buffer.
     """
     ...
+# Register a REST callback
+def RegisterRestCallback(path_regular_expression: str, callback: typing.Callable[[RestOutput, str], None]) -> None:
+    """
+    Register a REST callback.
+
+    Args:
+      path_regular_expression (str): Regular expression for the URI. May contain groups.
+      callback (typing.Callable[[RestOutput, str], None]): The callback function to handle the REST call.
+    """
+    ...
 
 
 class DicomInstance:
--- a/Sources/Autogenerated/sdk_GlobalFunctions.impl.h	Tue Jul 02 18:00:52 2024 +0200
+++ b/Sources/Autogenerated/sdk_GlobalFunctions.impl.h	Tue Jul 02 18:25:53 2024 +0200
@@ -22,6 +22,11 @@
 // WARNING: Auto-generated file. Do not modify it by hand.
 
 
+// Forward declaration of the custom global functions
+extern PyObject *RegisterRestCallback(PyObject* module, PyObject *args);
+// End of forward declarations
+
+
 static PyObject* sdk_OrthancPluginAutodetectMimeType(PyObject* module, PyObject* args)
 {
   PythonLock::LogCall("Calling Python global function: OrthancPluginAutodetectMimeType()");
@@ -2012,6 +2017,8 @@
     "Generated from C function OrthancPluginUncompressImage()" },
   { "WriteFile", sdk_OrthancPluginWriteFile, METH_VARARGS,
     "Generated from C function OrthancPluginWriteFile()" },
+  { "RegisterRestCallback", RegisterRestCallback, METH_VARARGS,
+    "Implemented in C++ function RegisterRestCallback()" },
   { NULL, NULL }
 };
 
--- a/Sources/Plugin.cpp	Tue Jul 02 18:00:52 2024 +0200
+++ b/Sources/Plugin.cpp	Tue Jul 02 18:25:53 2024 +0200
@@ -284,11 +284,6 @@
   std::list<PyMethodDef> functions;
 
   {
-    PyMethodDef f = { "RegisterRestCallback", RegisterRestCallback, METH_VARARGS, "" };
-    functions.push_back(f);
-  }
-
-  {
     PyMethodDef f = { "RegisterOnChangeCallback", RegisterOnChangeCallback, METH_VARARGS, "" };
     functions.push_back(f);
   }