Skip to content

Commit d6620a8

Browse files
committed
initial commit
0 parents  commit d6620a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+19422
-0
lines changed

README.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Convert JavaScript To Python
2+
3+
Using this NPM package you can convert your JavaScript code to Python and then use it .
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [CLI Command](#cli-command)
10+
- [Project Structure](#project-structure)
11+
- [Limitations](#limitations)
12+
- [License](#license)
13+
- [Contributing](#contributing)
14+
- [Code of Conduct](#code-of-conduct)
15+
- [Learn More](#learn-more)
16+
- [Issues](#issues)
17+
- [Contact](#contact)
18+
19+
## Installation
20+
21+
Provide instructions on how to install and set up your project. Include any prerequisites and step-by-step instructions to ensure a smooth installation process.
22+
23+
```bash
24+
npm i convert-js2py
25+
```
26+
27+
## Usage
28+
29+
Explain how to use your project. Include code examples, usage scenarios, and any relevant information that can help users understand how to get started.
30+
31+
```javascript
32+
// Import the convertJsToPython function from your library
33+
const { translateJSToPython } = require('convert-js2py');
34+
35+
// Define your JavaScript code that you want to convert
36+
const jsCode = `
37+
console.log("Hello, World!");
38+
`;
39+
40+
// Use the convertJsToPython function to convert the JavaScript code to Python
41+
const pythonCode = translateJSToPython(jsCode);
42+
43+
// Print the generated Python code
44+
console.log(pythonCode);
45+
```
46+
47+
## CLI Command
48+
49+
If your project includes a CLI command, provide instructions on how to use it. Include examples of how to run the command and its various options.
50+
51+
```bash
52+
npm i convert-js2py
53+
54+
# Convert a JavaScript file to Python
55+
js-to-python test.js output
56+
```
57+
58+
## Project Structure
59+
60+
``` bash
61+
js-to-python-converter/
62+
├── bin/
63+
│ └── js-to-python.js
64+
├── lib/
65+
│ ├── converter.js
66+
│ └── index.js
67+
├── package.json
68+
├── LICENSE
69+
├── CODE_OF_CONDUCT.md
70+
├── LEARN.md
71+
├── CONTRIBUTING.md
72+
└── README.md
73+
```
74+
75+
## Limitations
76+
77+
This can't convert a complex JavaScript code into a Python code now , but we'll fix this ASAP .
78+
79+
## License
80+
81+
This project is licensed under the [License Name] - see the [LICENSE](LICENSE) file for details.
82+
83+
## Contributing
84+
85+
Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
86+
87+
## Code of Conduct
88+
89+
Please read [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for our code of conduct.
90+
91+
## Learn More
92+
93+
Visit [LEARN.md](LEARN.md) for additional resources, tutorials, and documentation related to this project.
94+
95+
## Issues
96+
97+
If you encounter any issues, bugs, or have questions, please open an issue on the [Issues](link-to-issues) page.
98+
99+
## Contact
100+
101+
For further information or inquiries about this project, you can contact the developer:
102+
103+
- Developer Name: [Pabitra Banerjee](https://pabitrabanerjee.me)
104+
- Email: [rockstarpabitra2204@gmail.com](mailto:rockstarpabitra2204@gmail.com)
105+
- GitHub: [PB2204](https://github.com/PB2204)
106+
107+
## Happy Coding 🚀

js-to-python-converter/README.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Convert JavaScript To Python
2+
3+
Using this NPM package you can convert your JavaScript code to Python and then use it .
4+
5+
## Table of Contents
6+
7+
- [Installation](#installation)
8+
- [Usage](#usage)
9+
- [CLI Command](#cli-command)
10+
- [Project Structure](#project-structure)
11+
- [Limitations](#limitations)
12+
- [License](#license)
13+
- [Contributing](#contributing)
14+
- [Code of Conduct](#code-of-conduct)
15+
- [Learn More](#learn-more)
16+
- [Issues](#issues)
17+
- [Contact](#contact)
18+
19+
## Installation
20+
21+
Provide instructions on how to install and set up your project. Include any prerequisites and step-by-step instructions to ensure a smooth installation process.
22+
23+
```bash
24+
npm i convert-js2py
25+
```
26+
27+
## Usage
28+
29+
Explain how to use your project. Include code examples, usage scenarios, and any relevant information that can help users understand how to get started.
30+
31+
```javascript
32+
// Import the convertJsToPython function from your library
33+
const { translateJSToPython } = require('convert-js2py');
34+
35+
// Define your JavaScript code that you want to convert
36+
const jsCode = `
37+
console.log("Hello, World!");
38+
`;
39+
40+
// Use the convertJsToPython function to convert the JavaScript code to Python
41+
const pythonCode = translateJSToPython(jsCode);
42+
43+
// Print the generated Python code
44+
console.log(pythonCode);
45+
```
46+
47+
## CLI Command
48+
49+
If your project includes a CLI command, provide instructions on how to use it. Include examples of how to run the command and its various options.
50+
51+
```bash
52+
npm i convert-js2py
53+
54+
# Convert a JavaScript file to Python
55+
js-to-python test.js output
56+
```
57+
58+
## Project Structure
59+
60+
``` bash
61+
js-to-python-converter/
62+
├── bin/
63+
│ └── js-to-python.js
64+
├── lib/
65+
│ ├── converter.js
66+
│ └── index.js
67+
├── package.json
68+
├── LICENSE
69+
├── CODE_OF_CONDUCT.md
70+
├── LEARN.md
71+
├── CONTRIBUTING.md
72+
└── README.md
73+
```
74+
75+
## Limitations
76+
77+
This can't convert a complex JavaScript code into a Python code now , but we'll fix this ASAP .
78+
79+
## License
80+
81+
This project is licensed under the [License Name] - see the [LICENSE](LICENSE) file for details.
82+
83+
## Contributing
84+
85+
Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
86+
87+
## Code of Conduct
88+
89+
Please read [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for our code of conduct.
90+
91+
## Learn More
92+
93+
Visit [LEARN.md](LEARN.md) for additional resources, tutorials, and documentation related to this project.
94+
95+
## Issues
96+
97+
If you encounter any issues, bugs, or have questions, please open an issue on the [Issues](link-to-issues) page.
98+
99+
## Contact
100+
101+
For further information or inquiries about this project, you can contact the developer:
102+
103+
- Developer Name: [Pabitra Banerjee](https://pabitrabanerjee.me)
104+
- Email: [rockstarpabitra2204@gmail.com](mailto:rockstarpabitra2204@gmail.com)
105+
- GitHub: [PB2204](https://github.com/PB2204)
106+
107+
## Happy Coding 🚀
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
const { translateJSToPython } = require("../lib/converter");
6+
7+
const filePath = process.argv[2];
8+
const outputFolder = process.argv[3];
9+
10+
if (!filePath || !outputFolder) {
11+
console.error("Usage: js-to-python <file_path> <output_folder>");
12+
process.exit(1);
13+
}
14+
15+
try {
16+
const jsCode = fs.readFileSync(filePath, "utf-8");
17+
const pythonCode = translateJSToPython(jsCode);
18+
19+
// Create the output directory if it doesn't exist
20+
if (!fs.existsSync(outputFolder)) {
21+
fs.mkdirSync(outputFolder, { recursive: true });
22+
}
23+
24+
const outputPath = path.join(outputFolder, "output.py");
25+
fs.writeFileSync(outputPath, pythonCode);
26+
console.log(`JavaScript code converted and saved to ${outputPath}`);
27+
} catch (err) {
28+
console.error("Error:", err);
29+
process.exit(1);
30+
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const espree = require("espree");
2+
const estraverse = require("estraverse");
3+
4+
function translateJSExpressionToPython(node) {
5+
switch (node.type) {
6+
case "Literal":
7+
return JSON.stringify(node.value);
8+
case "Identifier":
9+
return node.name;
10+
case "BinaryExpression":
11+
return `(${translateJSExpressionToPython(node.left)} ${node.operator} ${translateJSExpressionToPython(node.right)})`;
12+
case "CallExpression":
13+
if (
14+
node.callee.type === "MemberExpression" &&
15+
node.callee.object.name === "console" &&
16+
node.callee.property.name === "log"
17+
) {
18+
// Convert console.log to Python print statement
19+
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
20+
return `print(${args.join(", ")})`;
21+
} else if (
22+
node.callee.type === "MemberExpression" &&
23+
node.callee.object.name === "console" &&
24+
node.callee.property.name === "error"
25+
) {
26+
// Convert console.error to Python print statement to stderr
27+
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
28+
return `print(${args.join(", ")}, file=sys.stderr)`;
29+
} else {
30+
// Handle other function calls
31+
const callee = translateJSExpressionToPython(node.callee);
32+
const args = node.arguments.map((arg) => translateJSExpressionToPython(arg));
33+
return `${callee}(${args.join(", ")})`;
34+
}
35+
case "MemberExpression":
36+
const object = translateJSExpressionToPython(node.object);
37+
const property = translateJSExpressionToPython(node.property);
38+
return `${object}.${property}`;
39+
case "TemplateLiteral":
40+
// Handle template literals by joining the cooked values
41+
return `"${node.quasis.map((quasi) => quasi.value.cooked).join("")}"`;
42+
case "AssignmentExpression":
43+
const left = translateJSExpressionToPython(node.left);
44+
const operator = node.operator;
45+
const right = translateJSExpressionToPython(node.right);
46+
return `${left} ${operator} ${right}`;
47+
case "ThisExpression":
48+
// Omit or replace this as needed
49+
return "";
50+
case "FunctionDeclaration":
51+
case "FunctionExpression":
52+
const functionName = node.id ? node.id.name : "";
53+
const params = node.params.map(param => param.name).join(", ");
54+
const functionBody = translateJSExpressionToPython(node.body);
55+
56+
return `def ${functionName}(${params}):\n${functionBody}`;
57+
default:
58+
throw new Error(`Unsupported JavaScript expression: ${node.type}`);
59+
}
60+
}
61+
62+
function translateJSToPython(jsCode) {
63+
const jsAST = espree.parse(jsCode, {
64+
ecmaVersion: 2020, // Adjust this as needed
65+
});
66+
67+
let pythonCode = "";
68+
69+
estraverse.traverse(jsAST, {
70+
enter(node) {
71+
if (node.type === "ExpressionStatement") {
72+
const translated = translateJSExpressionToPython(node.expression);
73+
pythonCode += translated + '\n'; // Append a line break
74+
}
75+
},
76+
});
77+
78+
return pythonCode;
79+
}
80+
81+
module.exports = { translateJSToPython };

js-to-python-converter/lib/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// js-to-python-converter/lib/index.js
2+
3+
const { translateJSToPython } = require("./converter");
4+
5+
module.exports = { translateJSToPython };

js-to-python-converter/node_modules/.bin/acorn

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js-to-python-converter/node_modules/.bin/acorn.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js-to-python-converter/node_modules/.bin/acorn.ps1

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)