Initial commit

This commit is contained in:
thecookingsenpai 2023-12-25 13:28:22 +01:00
commit 3f98422c4f
3 changed files with 8174 additions and 0 deletions

49
README.md Normal file
View File

@ -0,0 +1,49 @@
# Nodepy - A library for using nodejs commands and files in Python
## License
This program is distributed under the terms of the CC BY-NC-SA 2.0 license
## Installation
wget https://github.com/thecookingsenpai/nodepy
Now you can copy the jspyth.py file into your project directory and
import jspyth
And proceed to the usage as shown below
## Usage
### NOTE: This readme is WIP
### NOTE: This program is in beta state
Nodepy allows you to use nodejs commands and files in Python.
You can either use it through:
cmd = jspyth.JSCommand(is_file="False", command=["console.log", ["hello world"], ["eventual", "libraries", "to", "include"]], real_time=True)
result = cmd.run()
Or with a file:
cmd = jspyth.JSCommand(is_file="True", file="filename,js", real_time=True)
result = cmd.run()
Where
real_time = True/False
Allows you to see the output in real time.
The parameters part of the command and the include libraries are optional and should be used based on the command provided.
## Techical informations
The whole library is a wrapper around subthreading and is designed to be blocking, meaning that the subthread will be executed and awaited by Python.
The only prerequisite is having node installed and to npm install the needed libraries to include.

8050
aaa.html Normal file

File diff suppressed because one or more lines are too long

75
jspyth.py Normal file
View File

@ -0,0 +1,75 @@
import os
from subprocess import Popen, PIPE, STDOUT
class JSCommand:
# ANCHOR Manages the creation of a new JSCommand object
def __init__(self, is_file = False, command = [None, [], []], file="", real_time = False):
self.real_time = real_time
if is_file:
if file.strip=="":
raise Exception("File path is empty")
self.file = file
self.is_file = True
else:
if command[0]==None or command[0].strip()=="":
raise Exception("Command is empty")
self.command = command
self.is_file = False
# ANCHOR Run management
def run(self):
if self.is_file:
self.execute_file()
else:
self.execute_cmd()
# ANCHOR Single command execution
def execute_cmd(self):
# NOTE Command structure
# [
# command_string,
# ["arguments", "to", "pass"],
# ["libraries", "to", "include"]
# ]
cmd_string = self.command[0]
cmd_args = self.command[1]
cmd_include = self.command[2]
temp_file = "temp.js"
temp_buffer = ""
# NOTE Include libraries
for include in cmd_include:
temp_buffer += "const " + include + " = require('" + include + "');\r\n"
temp_buffer += cmd_string + "("
# NOTE Add arguments
for arg in cmd_args:
temp_buffer += "'" + arg + "', "
# NOTE Remove last comma if there are arguments
if len(cmd_args)>0:
temp_buffer = temp_buffer[:-2]
temp_buffer += ");"
# NOTE Write to file
with open(temp_buffer, "w+") as f:
f.write(temp_buffer)
# NOTE Execute file
self.file = temp_file
return self.execute_file()
# ANCHOR Whole file execution
def execute_file(self):
# NOTE Execute file
self.pid = Popen(["node", self.file], stdout=PIPE, stderr=STDOUT)
self.exec_return = ""
# NOTE Blocking execution until process is finished
while True:
line = self.pid.stdout.readline()
self.exec_return += line + "\r\n"
# NOTE Real time output if requested
if self.real_time:
print(line)
if not line: break
# NOTE Remove temp file
os.remove(self.file)
# NOTE Returns the output of the command
return self.exec_return