Skip to content

Commit

Permalink
fix(bindgen): Python canonical types for CLI::Range
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Aug 11, 2023
1 parent 8f58bb4 commit 2428e75
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/bindgen/canonical-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function canonicalType(parameterType) {
// Strip extras
const canonical = parameterType.split(' ')[0]
return canonical
}

export default canonicalType
14 changes: 11 additions & 3 deletions src/bindgen/python/function-module-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@ import snakeCase from '../snake-case.js'

import interfaceJsonTypeToPythonType from './interface-json-type-to-python-type.js'
import interfaceJsonTypeToInterfaceType from '../interface-json-type-to-interface-type.js'
import canonicalType from '../canonical-type.js'

function functionModuleArgs(interfaceJson) {
let functionArgs = ""
interfaceJson['inputs'].forEach((value) => {
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
functionArgs += ` ${snakeCase(value.name)}: ${pythonType},\n`
})
interfaceJson['parameters'].forEach((value) => {
if (value.name === "memory-io" || value.name === "version") {
return
}
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
if (interfaceJsonTypeToInterfaceType.has(value.type)) {
if(value.required && value.itemsExpectedMax > 1) {
functionArgs += ` ${snakeCase(value.name)}: List[${pythonType}] = [],\n`
} else {
functionArgs += ` ${snakeCase(value.name)}: Optional[${pythonType}] = None,\n`
}
} else {
functionArgs += ` ${snakeCase(value.name)}: ${pythonType}`
if(value.itemsExpected > 1) {
functionArgs += ` ${snakeCase(value.name)}: List[${pythonType}]`
} else {
functionArgs += ` ${snakeCase(value.name)}: ${pythonType}`

}
if(value.type === "BOOL") {
functionArgs += ` = False,\n`
} else if(value.type === "TEXT") {
Expand Down
10 changes: 7 additions & 3 deletions src/bindgen/python/function-module-docstring.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import snakeCase from "../snake-case.js"

import interfaceJsonTypeToPythonType from './interface-json-type-to-python-type.js'
import canonicalType from '../canonical-type.js'

function functionModuleDocstring(interfaceJson) {
let docstring = `"""${interfaceJson.description}
`
interfaceJson['inputs'].forEach((value) => {
const description = value.description.replaceAll('\n', ' ')
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
docstring += `\n :param ${snakeCase(value.name)}: ${description}\n`
docstring += ` :type ${snakeCase(value.name)}: ${pythonType}\n`
})
interfaceJson['parameters'].forEach((value) => {
if (value.name === "memory-io" || value.name === "version") {
return
}
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
const description = value.description.replaceAll('\n', ' ')
docstring += `\n :param ${snakeCase(value.name)}: ${description}\n`
docstring += ` :type ${snakeCase(value.name)}: ${pythonType}\n`
})
const jsonOutputs = interfaceJson['outputs']
jsonOutputs.forEach((value) => {
const description = value.description.replaceAll('\n', ' ')
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
docstring += `\n :return: ${description}\n`
docstring += ` :rtype: ${pythonType}\n`
})
Expand Down
4 changes: 3 additions & 1 deletion src/bindgen/python/function-module-return-type.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import interfaceJsonTypeToPythonType from './interface-json-type-to-python-type.js'
import canonicalType from '../canonical-type.js'

function functionModuleReturnType(interfaceJson) {
let returnType = ""
const jsonOutputs = interfaceJson['outputs']
if (jsonOutputs.length > 1) {
returnType += "Tuple["
jsonOutputs.forEach((value) => {
const pythonType = interfaceJsonTypeToPythonType.get(value.type)
const canonical = canonicalType(value.type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
returnType += `${pythonType}, `
})
returnType = returnType.substring(0, returnType.length - 2)
Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/python/interface-json-type-to-python-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ const interfaceJsonTypeToPythonType = new Map([
['BOOL', 'bool'],
['TEXT', 'str'],
['INT', 'int'],
['INT:INT', 'int'],
['UINT', 'int'],
['UINT:UINT', 'int'],
['FLOAT', 'float'],
['FLOAT:FLOAT', 'float'],
['INPUT_JSON', 'Dict'],
['OUTPUT_JSON', 'Dict'],
])
Expand Down
4 changes: 3 additions & 1 deletion src/bindgen/python/wasi/wasi-function-module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra'

import snakeCase from '../../snake-case.js'
import canonicalType from '../../canonical-type.js'

import functionModuleImports from '../function-module-imports.js'
import functionModuleReturnType from '../function-module-return-type.js'
Expand Down Expand Up @@ -171,7 +172,8 @@ from itkwasm import (

let postOutput = ''
function toPythonType(type, value) {
const pythonType = interfaceJsonTypeToPythonType.get(type)
const canonical = canonicalType(type)
const pythonType = interfaceJsonTypeToPythonType.get(canonical)
switch (pythonType) {
case "os.PathLike":
return `Path(${value}.data.path)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ const interfaceJsonTypeToTypeScriptType = new Map([
['BOOL', 'boolean'],
['TEXT', 'string'],
['INT', 'number'],
['INT:INT', 'number'],
['UINT', 'number'],
['UINT:UINT', 'number'],
['FLOAT', 'number'],
['FLOAT:FLOAT', 'number[]'],
['FLOAT:FLOAT', 'number'],
['INPUT_JSON', 'Object'],
['OUTPUT_JSON', 'Object'],
])
Expand Down
9 changes: 5 additions & 4 deletions src/bindgen/typescript/options-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { markdownTable } from 'markdown-table'
import interfaceJsonTypeToTypeScriptType from './interface-json-type-to-typescript-type.js'
import typesRequireImport from './types-require-import.js'
import camelCase from '../camel-case.js'
import canonicalType from '../canonical-type.js'
import writeIfOverrideNotPresent from '../write-if-override-not-present.js'

function optionsModule (srcOutputDir, interfaceJson, modulePascalCase, nodeTextCamel, moduleKebabCase, haveOptions) {
Expand All @@ -27,13 +28,13 @@ function optionsModule (srcOutputDir, interfaceJson, modulePascalCase, nodeTextC
return
}

const canonicalType = parameter.type.split(' ')[0]
if (!interfaceJsonTypeToTypeScriptType.has(canonicalType)) {
console.error(`Unexpected parameter type: ${canonicalType}`)
const canonical = canonicalType(parameter.type)
if (!interfaceJsonTypeToTypeScriptType.has(canonical)) {
console.error(`Unexpected parameter type: ${canonical}`)
process.exit(1)
}
optionsInterfaceContent += ` /** ${parameter.description} */\n`
let parameterType = interfaceJsonTypeToTypeScriptType.get(canonicalType)
let parameterType = interfaceJsonTypeToTypeScriptType.get(canonical)
if (typesRequireImport.includes(parameterType)) {
optionsImportTypes.add(parameterType)
}
Expand Down
1 change: 0 additions & 1 deletion src/itk-wasm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ function bindgen(options) {
// Building for emscripten can generate duplicate .umd.wasm and .wasm binaries
// Also filter libraries.
let filteredWasmBinaries = wasmBinaries.filter(binary => !binary.endsWith('.umd.wasm') && !path.basename(binary).startsWith('lib'))
console.log(filteredWasmBinaries)

switch (iface) {
case 'typescript':
Expand Down

0 comments on commit 2428e75

Please sign in to comment.