doInspection function
Inspects a Python module for interface generation.
If dump is true, the extracted interface information will be returned as
a json String.
If dump is false, null will be returned.
Implementation
Future<String?> doInspection(
PythonModuleDefinition? moduleDefinition, {
required String moduleName,
required AppType appType,
required InspectionCache cache,
required String stdlibPath,
required bool dump,
String parentModulePrefix = "",
}) async {
try {
final Progress inspectionProgress = DartpipCommandRunner.logger.progress(
"Generating Dart interface for '$parentModulePrefix$moduleName' via inspect",
);
if (parentModulePrefix.isEmpty) {
if (moduleDefinition != null) {
await PythonFfiDart.instance.prepareModule(moduleDefinition);
} else {
inspectionProgress.finish(showTiming: true);
return null;
}
}
final Module interface = PythonFfiDart.instance.importModule(
"$parentModulePrefix$moduleName",
(PythonModuleInterface<PythonFfiDelegate<Object?>, Object?> m) =>
Module.from(
m,
name: moduleName,
sanitizedName: sanitizeName(moduleName),
),
);
interface.collectChildren(
cache,
stdlibPath: stdlibPath,
parentModule: interface,
);
Object? toEncodable(Object? o) {
final Object? object = o;
return switch (object) {
null => o.toString(),
Module() ||
ClassDefinition() ||
ClassInstance() ||
Function_() ||
Object_() =>
<String, Object?>{
"type": object.runtimeType.toString(),
"value": jsonEncode(
(object as InspectEntry).value,
toEncodable: toEncodable,
),
"string": object.toString(),
},
PythonIterable<Object?, PythonFfiDelegate<Object?>, Object?>() ||
PythonIterator<Object?, PythonFfiDelegate<Object?>, Object?>() =>
<String, Object?>{
"type": object.runtimeType.toString(),
"value": jsonEncode(
(object as PythonObjectInterface<PythonFfiDelegate<Object?>,
Object?>)
.reference,
toEncodable: toEncodable,
),
},
PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>() =>
<String, Object?>{
"type": object.runtimeType.toString(),
"value": jsonEncode(object.reference, toEncodable: toEncodable),
"string": object.toString(),
},
_ => o.toString(),
};
}
final String? json = dump
? jsonEncode(
<String, Object?>{
"_module": interface.debugDump(cache: cache),
"_entries": cache.entries
.whereNot(
((int, InspectEntry) e) =>
e.$2.type == InspectEntryType.primitive,
)
.map(
((int, InspectEntry) e) => <String, Object?>{
"id": e.$1,
"entry": e.$2.debugDump(cache: cache),
},
)
.toList(),
},
toEncodable: toEncodable,
)
: null;
inspectionProgress.finish(showTiming: true);
return json;
} on PythonFfiException catch (e) {
DartpipCommandRunner.logger.trace(e.toString());
}
return null;
}