|
| 1 | +import type { ModelsAsDataLanguage } from "../models-as-data"; |
| 2 | +import { sharedExtensiblePredicates, sharedKinds } from "../shared"; |
| 3 | +import { Mode } from "../../shared/mode"; |
| 4 | +import type { MethodArgument } from "../../method"; |
| 5 | +import { EndpointType, getArgumentsList } from "../../method"; |
| 6 | +import { |
| 7 | + parsePythonAccessPath, |
| 8 | + pythonEndpointType, |
| 9 | + pythonMethodPath, |
| 10 | + pythonMethodSignature, |
| 11 | + pythonPath, |
| 12 | +} from "./access-paths"; |
| 13 | + |
| 14 | +export const python: ModelsAsDataLanguage = { |
| 15 | + availableModes: [Mode.Framework], |
| 16 | + createMethodSignature: ({ typeName, methodName }) => |
| 17 | + `${typeName}#${methodName}`, |
| 18 | + endpointTypeForEndpoint: (method) => pythonEndpointType(method), |
| 19 | + predicates: { |
| 20 | + source: { |
| 21 | + extensiblePredicate: sharedExtensiblePredicates.source, |
| 22 | + supportedKinds: sharedKinds.source, |
| 23 | + supportedEndpointTypes: [EndpointType.Method, EndpointType.Function], |
| 24 | + // extensible predicate sourceModel( |
| 25 | + // string type, string path, string kind |
| 26 | + // ); |
| 27 | + generateMethodDefinition: (method) => [ |
| 28 | + method.packageName, |
| 29 | + pythonPath( |
| 30 | + method.typeName, |
| 31 | + method.methodName, |
| 32 | + method.endpointType, |
| 33 | + method.output, |
| 34 | + ), |
| 35 | + method.kind, |
| 36 | + ], |
| 37 | + readModeledMethod: (row) => { |
| 38 | + const packageName = row[0] as string; |
| 39 | + const { |
| 40 | + typeName, |
| 41 | + methodName, |
| 42 | + endpointType, |
| 43 | + path: output, |
| 44 | + } = parsePythonAccessPath(row[1] as string); |
| 45 | + return { |
| 46 | + type: "source", |
| 47 | + output, |
| 48 | + kind: row[2] as string, |
| 49 | + provenance: "manual", |
| 50 | + signature: pythonMethodSignature(typeName, methodName), |
| 51 | + endpointType, |
| 52 | + packageName, |
| 53 | + typeName, |
| 54 | + methodName, |
| 55 | + methodParameters: "", |
| 56 | + }; |
| 57 | + }, |
| 58 | + }, |
| 59 | + sink: { |
| 60 | + extensiblePredicate: sharedExtensiblePredicates.sink, |
| 61 | + supportedKinds: sharedKinds.sink, |
| 62 | + supportedEndpointTypes: [EndpointType.Method, EndpointType.Function], |
| 63 | + // extensible predicate sinkModel( |
| 64 | + // string type, string path, string kind |
| 65 | + // ); |
| 66 | + generateMethodDefinition: (method) => { |
| 67 | + return [ |
| 68 | + method.packageName, |
| 69 | + pythonPath( |
| 70 | + method.typeName, |
| 71 | + method.methodName, |
| 72 | + method.endpointType, |
| 73 | + method.input, |
| 74 | + ), |
| 75 | + method.kind, |
| 76 | + ]; |
| 77 | + }, |
| 78 | + readModeledMethod: (row) => { |
| 79 | + const packageName = row[0] as string; |
| 80 | + const { |
| 81 | + typeName, |
| 82 | + methodName, |
| 83 | + endpointType, |
| 84 | + path: input, |
| 85 | + } = parsePythonAccessPath(row[1] as string); |
| 86 | + return { |
| 87 | + type: "sink", |
| 88 | + input, |
| 89 | + kind: row[2] as string, |
| 90 | + provenance: "manual", |
| 91 | + signature: pythonMethodSignature(typeName, methodName), |
| 92 | + endpointType, |
| 93 | + packageName, |
| 94 | + typeName, |
| 95 | + methodName, |
| 96 | + methodParameters: "", |
| 97 | + }; |
| 98 | + }, |
| 99 | + }, |
| 100 | + summary: { |
| 101 | + extensiblePredicate: sharedExtensiblePredicates.summary, |
| 102 | + supportedKinds: sharedKinds.summary, |
| 103 | + supportedEndpointTypes: [EndpointType.Method, EndpointType.Function], |
| 104 | + // extensible predicate summaryModel( |
| 105 | + // string type, string path, string input, string output, string kind |
| 106 | + // ); |
| 107 | + generateMethodDefinition: (method) => [ |
| 108 | + method.packageName, |
| 109 | + pythonMethodPath( |
| 110 | + method.typeName, |
| 111 | + method.methodName, |
| 112 | + method.endpointType, |
| 113 | + ), |
| 114 | + method.input, |
| 115 | + method.output, |
| 116 | + method.kind, |
| 117 | + ], |
| 118 | + readModeledMethod: (row) => { |
| 119 | + const packageName = row[0] as string; |
| 120 | + const { typeName, methodName, endpointType, path } = |
| 121 | + parsePythonAccessPath(row[1] as string); |
| 122 | + if (path !== "") { |
| 123 | + throw new Error("Summary path must be a method"); |
| 124 | + } |
| 125 | + return { |
| 126 | + type: "summary", |
| 127 | + input: row[2] as string, |
| 128 | + output: row[3] as string, |
| 129 | + kind: row[4] as string, |
| 130 | + provenance: "manual", |
| 131 | + signature: pythonMethodSignature(typeName, methodName), |
| 132 | + endpointType, |
| 133 | + packageName, |
| 134 | + typeName, |
| 135 | + methodName, |
| 136 | + methodParameters: "", |
| 137 | + }; |
| 138 | + }, |
| 139 | + }, |
| 140 | + neutral: { |
| 141 | + extensiblePredicate: sharedExtensiblePredicates.neutral, |
| 142 | + supportedKinds: sharedKinds.neutral, |
| 143 | + // extensible predicate neutralModel( |
| 144 | + // string type, string path, string kind |
| 145 | + // ); |
| 146 | + generateMethodDefinition: (method) => [ |
| 147 | + method.packageName, |
| 148 | + pythonMethodPath( |
| 149 | + method.typeName, |
| 150 | + method.methodName, |
| 151 | + method.endpointType, |
| 152 | + ), |
| 153 | + method.kind, |
| 154 | + ], |
| 155 | + readModeledMethod: (row) => { |
| 156 | + const packageName = row[0] as string; |
| 157 | + const { typeName, methodName, endpointType, path } = |
| 158 | + parsePythonAccessPath(row[1] as string); |
| 159 | + if (path !== "") { |
| 160 | + throw new Error("Neutral path must be a method"); |
| 161 | + } |
| 162 | + return { |
| 163 | + type: "neutral", |
| 164 | + kind: row[2] as string, |
| 165 | + provenance: "manual", |
| 166 | + signature: pythonMethodSignature(typeName, methodName), |
| 167 | + endpointType, |
| 168 | + packageName, |
| 169 | + typeName, |
| 170 | + methodName, |
| 171 | + methodParameters: "", |
| 172 | + }; |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + getArgumentOptions: (method) => { |
| 177 | + // Argument and Parameter are equivalent in Python, but we'll use Argument in the model editor |
| 178 | + const argumentsList = getArgumentsList(method.methodParameters).map( |
| 179 | + (argument, index): MethodArgument => { |
| 180 | + if (argument.endsWith(":")) { |
| 181 | + return { |
| 182 | + path: `Argument[${argument}]`, |
| 183 | + label: `Argument[${argument}]`, |
| 184 | + }; |
| 185 | + } |
| 186 | + |
| 187 | + return { |
| 188 | + path: `Argument[${index}]`, |
| 189 | + label: `Argument[${index}]: ${argument}`, |
| 190 | + }; |
| 191 | + }, |
| 192 | + ); |
| 193 | + |
| 194 | + return { |
| 195 | + options: [ |
| 196 | + { |
| 197 | + path: "Argument[self]", |
| 198 | + label: "Argument[self]", |
| 199 | + }, |
| 200 | + ...argumentsList, |
| 201 | + ], |
| 202 | + // If there are no arguments, we will default to "Argument[self]" |
| 203 | + defaultArgumentPath: |
| 204 | + argumentsList.length > 0 ? argumentsList[0].path : "Argument[self]", |
| 205 | + }; |
| 206 | + }, |
| 207 | +}; |
0 commit comments