From 7a3934a5c915d91e1e2837da48998db23dde37c8 Mon Sep 17 00:00:00 2001 From: ItshMoh Date: Sun, 22 Sep 2024 02:40:00 +0530 Subject: [PATCH] Migration completed --- components/model-class.js | 48 --------------------- hooks/post-process.js | 6 --- partials/model-class.js | 52 +++++++++++++++++++++++ template/main.py.js | 89 +++++++++++++++++++++------------------ template/schema.py.js | 35 +++++++++++++++ 5 files changed, 136 insertions(+), 94 deletions(-) delete mode 100644 components/model-class.js create mode 100644 partials/model-class.js create mode 100644 template/schema.py.js diff --git a/components/model-class.js b/components/model-class.js deleted file mode 100644 index accc6787..00000000 --- a/components/model-class.js +++ /dev/null @@ -1,48 +0,0 @@ -function modelClass(name, properties, required, indentLevel) { - const className = upperFirst(name); - const indent1 = indent(indentLevel); - const indent2 = indent(indentLevel + 1); - const indent3 = indent(indentLevel + 2); - const indent4 = indent(indentLevel + 3); - - let output = `${indent1}class ${className}(Entity):\n`; - - // Render nested classes and enums - for (const [propName, prop] of Object.entries(properties)) { - const typeInfo = getTypeInfo([propName, prop]); - if (typeInfo.recursive) { - output += modelClass(typeInfo.innerType, typeInfo.properties, prop.required(), indentLevel + 1); - } else if (typeInfo.generalType === 'enum') { - output += `${indent2}class ${typeInfo.type}(str, Enum):\n`; - for (const v of typeInfo.enum) { - output += `${indent3}${v} = '${v}'\n`; - } - } - } - - // Render __init__ method - output += `${indent2}def __init__(\n`; - output += `${indent4}self`; - - for (const [propName, prop] of Object.entries(properties)) { - const typeInfo = getTypeInfo([propName, prop]); - output += `,\n${indent4}${typeInfo.pythonName}${typeInfo.pythonType ? ': ' + typeInfo.pythonType : ''}`; - } - output += '\n' + indent2 + '):\n'; - - // Render __init__ body - const initBody = Object.entries(properties).map(([propName, prop]) => { - const typeInfo = getTypeInfo([propName, prop]); - return `${indent3}self.${typeInfo.pythonName} = ${typeInfo.pythonName}`; - }); - - if (initBody.length > 0) { - output += initBody.join('\n') + '\n'; - } else { - output += indent3 + 'pass\n'; - } - - return output; - } - - module.exports = modelClass; \ No newline at end of file diff --git a/hooks/post-process.js b/hooks/post-process.js index bf0ba048..b364e3ba 100644 --- a/hooks/post-process.js +++ b/hooks/post-process.js @@ -18,11 +18,5 @@ module.exports = { } } } - - // If there are no schemas, we expect to find an anonymous one embedded in a payload. If we do have schemas we assume we don't need this. - // This will turn out to be a bug if we ever have a file with schemas, but which also has an anonymous schema embedded in an operation. - if (hasSchema) { - fs.unlinkSync(path.resolve(generator.targetDir, 'payload.py')); - } } } diff --git a/partials/model-class.js b/partials/model-class.js new file mode 100644 index 00000000..97cc9e64 --- /dev/null +++ b/partials/model-class.js @@ -0,0 +1,52 @@ +const _ = require('lodash'); +const getTypeInfo = require('../helpers/all').getTypeInfo; +const indent1 = require('../helpers/all').indent1; +const indent2 = require('../helpers/all').indent2; +const indent3 = require('../helpers/all').indent3; +const indent4 = require('../helpers/all').indent4; + +function modelClass(name, properties, required, indentLevel = 1) { + const className = _.upperFirst(name); + + let classDefinition = `${indent1(indentLevel)}class ${className}(Entity):\n`; + + Object.entries(properties).forEach(([propName, prop]) => { + const typeInfo = getTypeInfo([propName, prop]); + + if (typeInfo.recursive) { + classDefinition += modelClass( + typeInfo.innerType, typeInfo.properties, prop.required(), indentLevel + 1 + ); + } else if (typeInfo.generalType === 'enum') { + classDefinition += `${indent2(indentLevel)}class ${typeInfo.type}(str, Enum):\n`; + typeInfo.enum.forEach((value) => { + classDefinition += `${indent3(indentLevel)}${value} = '${value}'\n`; + }); + classDefinition += `${indent2(indentLevel)}\n`; + } + }); + + classDefinition += `${indent2(indentLevel)}def __init__(self,\n`; + + Object.entries(properties).forEach(([propName, prop], index) => { + const typeInfo = getTypeInfo([propName, prop]); + const separator = index === 0 ? '' : ',\n'; + classDefinition += `${separator}${indent4(indentLevel)}${typeInfo.pythonName}: ${typeInfo.pythonType}`; + }); + + classDefinition += `):\n`; + + Object.entries(properties).forEach(([propName, prop]) => { + const typeInfo = getTypeInfo([propName, prop]); + classDefinition += `${indent3(indentLevel)}self.${typeInfo.pythonName} = ${typeInfo.pythonName}\n`; + }); + + if (Object.keys(properties).length === 0) { + classDefinition += `${indent3(indentLevel)}pass\n`; + } + + classDefinition += `${indent1(indentLevel)}\n`; + return classDefinition; +} + +module.exports = { modelClass } diff --git a/template/main.py.js b/template/main.py.js index 8b419a7a..8cb4bbd1 100644 --- a/template/main.py.js +++ b/template/main.py.js @@ -1,8 +1,8 @@ const { File } = require('@asyncapi/generator-react-sdk'); const _ = require('lodash'); const { getRealSubscriber } = require('../helpers/all.js') -const {getFunctionNameByChannel } = require('../helpers/all.js') -const {getPayloadClass }= require('../helpers/all.js') +const { functionName } = require('../helpers/all.js') +const { payloadClass }= require('../helpers/all.js') const {getFirstPublisherMessenger } = require('../helpers/all.js') const {getMessengers } = require('../helpers/all.js') @@ -14,14 +14,10 @@ import time import messaging `; - if (asyncapi.components() && asyncapi.components().schemas()) { - Object.entries(asyncapi.components().schemas()).forEach(([schemaName, schema]) => { - const moduleName = _.lowerFirst(schemaName); - code += `from ${moduleName} import ${_.upperFirst(schemaName)}\n`; - }); - } else { - code += 'from payload import Payload\n'; - } + Object.entries(asyncapi.components().schemas()).map(([schemaName, schema]) => { + const moduleName = _.lowerFirst(schemaName); + code += `from ${moduleName} import ${_.upperFirst(schemaName)}\n`; + }); code += ` # Config has the connection properties. @@ -32,7 +28,22 @@ def getConfig(): return config `; - + const channels = asyncapi.channels(); + + Object.entries(channels).map(([channelName, channel]) => { + const sub = getRealSubscriber([asyncapi.info(), params, channel]); + if (sub) { + const fnName = functionName([channelName, channel]); + const payloadClasses = payloadClass(sub); + const varName = _.lowerFirst(payloadClasses); + code += `def ${fnName}(client, userdata, msg): + jsonString = msg.payload.decode('utf-8') + logging.info('Received json: ' + jsonString) + ${_.lowerFirst(payloadClasses)} = ${payloadClasses}.from_json(jsonString) + logging.info('Received message: ' + str(${_.lowerFirst(payloadClasses)})) +`; + } + }) code += ` def main(): @@ -41,48 +52,46 @@ def main(): config = getConfig() `; - // const publishMessenger = getFirstPublisherMessenger(params, asyncapi); - // const messengers = getMessengers(params, asyncapi); + const publishMessenger = getFirstPublisherMessenger([params, asyncapi]); + const messengers = getMessengers([params, asyncapi]); - // messengers.forEach(messenger => { - // if (messenger.subscribeTopic) { - // code += ` ${messenger.name} = messaging.Messaging(config, '${messenger.subscribeTopic}', ${messenger.functionName})\n`; - // } else { - // code += ` ${messenger.name} = messaging.Messaging(config)\n`; - // } + messengers.forEach(messenger => { + if (messenger.subscribeTopic) { + code += ` ${messenger.name} = messaging.Messaging(config, '${messenger.subscribeTopic}', ${messenger.functionName})\n`; + } else { + code += ` ${messenger.name} = messaging.Messaging(config)\n`; + } - // if (publishMessenger) { - // code += ` ${messenger.name}.loop_start()\n`; - // } else { - // code += ` ${messenger.name}.loop_forever()\n`; - // } - // }); + if (publishMessenger) { + code += ` ${messenger.name}.loop_start()\n`; + } else { + code += ` ${messenger.name}.loop_forever()\n`; + } + }); -// if (publishMessenger) { -// code += ` -// # Example of how to publish a message. You will have to add arguments to the constructor on the next line: -// payload = ${publishMessenger.payloadClass}() -// payloadJson = payload.to_json() -// while True: -// ${publishMessenger.name}.publish('${publishMessenger.publishTopic}', payloadJson) -// time.sleep(1) -// `; -// } + if (publishMessenger) { + code += ` + # Example of how to publish a message. You will have to add arguments to the constructor on the next line: + payload = ${publishMessenger.payloadClass}() + payloadJson = payload.to_json() + while True: + ${publishMessenger.name}.publish('${publishMessenger.publishTopic}', payloadJson) + time.sleep(1) +`; + } + + code += `if __name__ == '__main__': + main()` return code; } function MainFile({ asyncapi, params }) { const generatedCode = generatePythonCode(asyncapi, params); - // const allChannels = channels.all() - console.log("HIIII",asyncapi.channels()) - console.log("typeof",typeof asyncapi.channels()) - return ( {generatedCode} - {/* {`heello from msin`} */} ); } diff --git a/template/schema.py.js b/template/schema.py.js new file mode 100644 index 00000000..bf134c49 --- /dev/null +++ b/template/schema.py.js @@ -0,0 +1,35 @@ +const { File } = require('@asyncapi/generator-react-sdk'); +const { modelClass } = require('../partials/model-class.js') +const getImports = require('../helpers/all.js').getImports +const convertToFileName = require('../helpers/utils.js') + +function generateModelCode(schemaName, schema) { + let code = `from enum import Enum +from typing import Sequence +from entity import Entity +`; + + const code_imports = getImports(schema); + code += code_imports + '\n'; + + code += modelClass(schemaName, schema.properties(), schema.required() | [] , 0); + + return code; +} + +function ModelFile({ asyncapi }) { + const files = []; + const schemas = asyncapi.components().schemas() + + Object.entries(schemas).forEach(([schemaName, schema]) => { + const generatedCode = generateModelCode(schemaName, schema); + files.push( + + {generatedCode} + + ); + }); + return files; +} + +module.exports = ModelFile;