Skip to content

Commit 7a3934a

Browse files
committed
Migration completed
1 parent 313b74d commit 7a3934a

File tree

5 files changed

+136
-94
lines changed

5 files changed

+136
-94
lines changed

components/model-class.js

-48
This file was deleted.

hooks/post-process.js

-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,5 @@ module.exports = {
1818
}
1919
}
2020
}
21-
22-
// 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.
23-
// 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.
24-
if (hasSchema) {
25-
fs.unlinkSync(path.resolve(generator.targetDir, 'payload.py'));
26-
}
2721
}
2822
}

partials/model-class.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const _ = require('lodash');
2+
const getTypeInfo = require('../helpers/all').getTypeInfo;
3+
const indent1 = require('../helpers/all').indent1;
4+
const indent2 = require('../helpers/all').indent2;
5+
const indent3 = require('../helpers/all').indent3;
6+
const indent4 = require('../helpers/all').indent4;
7+
8+
function modelClass(name, properties, required, indentLevel = 1) {
9+
const className = _.upperFirst(name);
10+
11+
let classDefinition = `${indent1(indentLevel)}class ${className}(Entity):\n`;
12+
13+
Object.entries(properties).forEach(([propName, prop]) => {
14+
const typeInfo = getTypeInfo([propName, prop]);
15+
16+
if (typeInfo.recursive) {
17+
classDefinition += modelClass(
18+
typeInfo.innerType, typeInfo.properties, prop.required(), indentLevel + 1
19+
);
20+
} else if (typeInfo.generalType === 'enum') {
21+
classDefinition += `${indent2(indentLevel)}class ${typeInfo.type}(str, Enum):\n`;
22+
typeInfo.enum.forEach((value) => {
23+
classDefinition += `${indent3(indentLevel)}${value} = '${value}'\n`;
24+
});
25+
classDefinition += `${indent2(indentLevel)}\n`;
26+
}
27+
});
28+
29+
classDefinition += `${indent2(indentLevel)}def __init__(self,\n`;
30+
31+
Object.entries(properties).forEach(([propName, prop], index) => {
32+
const typeInfo = getTypeInfo([propName, prop]);
33+
const separator = index === 0 ? '' : ',\n';
34+
classDefinition += `${separator}${indent4(indentLevel)}${typeInfo.pythonName}: ${typeInfo.pythonType}`;
35+
});
36+
37+
classDefinition += `):\n`;
38+
39+
Object.entries(properties).forEach(([propName, prop]) => {
40+
const typeInfo = getTypeInfo([propName, prop]);
41+
classDefinition += `${indent3(indentLevel)}self.${typeInfo.pythonName} = ${typeInfo.pythonName}\n`;
42+
});
43+
44+
if (Object.keys(properties).length === 0) {
45+
classDefinition += `${indent3(indentLevel)}pass\n`;
46+
}
47+
48+
classDefinition += `${indent1(indentLevel)}\n`;
49+
return classDefinition;
50+
}
51+
52+
module.exports = { modelClass }

template/main.py.js

+49-40
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { File } = require('@asyncapi/generator-react-sdk');
22
const _ = require('lodash');
33
const { getRealSubscriber } = require('../helpers/all.js')
4-
const {getFunctionNameByChannel } = require('../helpers/all.js')
5-
const {getPayloadClass }= require('../helpers/all.js')
4+
const { functionName } = require('../helpers/all.js')
5+
const { payloadClass }= require('../helpers/all.js')
66
const {getFirstPublisherMessenger } = require('../helpers/all.js')
77
const {getMessengers } = require('../helpers/all.js')
88

@@ -14,14 +14,10 @@ import time
1414
import messaging
1515
`;
1616

17-
if (asyncapi.components() && asyncapi.components().schemas()) {
18-
Object.entries(asyncapi.components().schemas()).forEach(([schemaName, schema]) => {
19-
const moduleName = _.lowerFirst(schemaName);
20-
code += `from ${moduleName} import ${_.upperFirst(schemaName)}\n`;
21-
});
22-
} else {
23-
code += 'from payload import Payload\n';
24-
}
17+
Object.entries(asyncapi.components().schemas()).map(([schemaName, schema]) => {
18+
const moduleName = _.lowerFirst(schemaName);
19+
code += `from ${moduleName} import ${_.upperFirst(schemaName)}\n`;
20+
});
2521

2622
code += `
2723
# Config has the connection properties.
@@ -32,7 +28,22 @@ def getConfig():
3228
return config
3329
`;
3430

35-
31+
const channels = asyncapi.channels();
32+
33+
Object.entries(channels).map(([channelName, channel]) => {
34+
const sub = getRealSubscriber([asyncapi.info(), params, channel]);
35+
if (sub) {
36+
const fnName = functionName([channelName, channel]);
37+
const payloadClasses = payloadClass(sub);
38+
const varName = _.lowerFirst(payloadClasses);
39+
code += `def ${fnName}(client, userdata, msg):
40+
jsonString = msg.payload.decode('utf-8')
41+
logging.info('Received json: ' + jsonString)
42+
${_.lowerFirst(payloadClasses)} = ${payloadClasses}.from_json(jsonString)
43+
logging.info('Received message: ' + str(${_.lowerFirst(payloadClasses)}))
44+
`;
45+
}
46+
})
3647

3748
code += `
3849
def main():
@@ -41,48 +52,46 @@ def main():
4152
config = getConfig()
4253
`;
4354

44-
// const publishMessenger = getFirstPublisherMessenger(params, asyncapi);
45-
// const messengers = getMessengers(params, asyncapi);
55+
const publishMessenger = getFirstPublisherMessenger([params, asyncapi]);
56+
const messengers = getMessengers([params, asyncapi]);
4657

47-
// messengers.forEach(messenger => {
48-
// if (messenger.subscribeTopic) {
49-
// code += ` ${messenger.name} = messaging.Messaging(config, '${messenger.subscribeTopic}', ${messenger.functionName})\n`;
50-
// } else {
51-
// code += ` ${messenger.name} = messaging.Messaging(config)\n`;
52-
// }
58+
messengers.forEach(messenger => {
59+
if (messenger.subscribeTopic) {
60+
code += ` ${messenger.name} = messaging.Messaging(config, '${messenger.subscribeTopic}', ${messenger.functionName})\n`;
61+
} else {
62+
code += ` ${messenger.name} = messaging.Messaging(config)\n`;
63+
}
5364

54-
// if (publishMessenger) {
55-
// code += ` ${messenger.name}.loop_start()\n`;
56-
// } else {
57-
// code += ` ${messenger.name}.loop_forever()\n`;
58-
// }
59-
// });
65+
if (publishMessenger) {
66+
code += ` ${messenger.name}.loop_start()\n`;
67+
} else {
68+
code += ` ${messenger.name}.loop_forever()\n`;
69+
}
70+
});
6071

61-
// if (publishMessenger) {
62-
// code += `
63-
// # Example of how to publish a message. You will have to add arguments to the constructor on the next line:
64-
// payload = ${publishMessenger.payloadClass}()
65-
// payloadJson = payload.to_json()
66-
// while True:
67-
// ${publishMessenger.name}.publish('${publishMessenger.publishTopic}', payloadJson)
68-
// time.sleep(1)
69-
// `;
70-
// }
72+
if (publishMessenger) {
73+
code += `
74+
# Example of how to publish a message. You will have to add arguments to the constructor on the next line:
75+
payload = ${publishMessenger.payloadClass}()
76+
payloadJson = payload.to_json()
77+
while True:
78+
${publishMessenger.name}.publish('${publishMessenger.publishTopic}', payloadJson)
79+
time.sleep(1)
80+
`;
81+
}
82+
83+
code += `if __name__ == '__main__':
84+
main()`
7185

7286
return code;
7387
}
7488

7589
function MainFile({ asyncapi, params }) {
7690
const generatedCode = generatePythonCode(asyncapi, params);
77-
// const allChannels = channels.all()
78-
console.log("HIIII",asyncapi.channels())
79-
console.log("typeof",typeof asyncapi.channels())
80-
8191

8292
return (
8393
<File name="main.py">
8494
{generatedCode}
85-
{/* {`heello from msin`} */}
8695
</File>
8796
);
8897
}

template/schema.py.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { File } = require('@asyncapi/generator-react-sdk');
2+
const { modelClass } = require('../partials/model-class.js')
3+
const getImports = require('../helpers/all.js').getImports
4+
const convertToFileName = require('../helpers/utils.js')
5+
6+
function generateModelCode(schemaName, schema) {
7+
let code = `from enum import Enum
8+
from typing import Sequence
9+
from entity import Entity
10+
`;
11+
12+
const code_imports = getImports(schema);
13+
code += code_imports + '\n';
14+
15+
code += modelClass(schemaName, schema.properties(), schema.required() | [] , 0);
16+
17+
return code;
18+
}
19+
20+
function ModelFile({ asyncapi }) {
21+
const files = [];
22+
const schemas = asyncapi.components().schemas()
23+
24+
Object.entries(schemas).forEach(([schemaName, schema]) => {
25+
const generatedCode = generateModelCode(schemaName, schema);
26+
files.push(
27+
<File name={`${convertToFileName(schemaName)}.py`}>
28+
{generatedCode}
29+
</File>
30+
);
31+
});
32+
return files;
33+
}
34+
35+
module.exports = ModelFile;

0 commit comments

Comments
 (0)