pytwojs 1.0.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,8 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: pytwojs
3
- Version: 1.0.0
3
+ Version: 1.2.0
4
4
  Summary: A high-performance Python library that relies on nodejs to execute JavaScript
5
5
  Author: Samwe
6
6
  Author-email: 1281722462@qq.com
7
7
  Requires-Python: >=3.8.0
8
- Dynamic: author
9
- Dynamic: author-email
10
- Dynamic: requires-python
11
- Dynamic: summary
8
+ Requires-Dist: pyminifyjs
@@ -4,3 +4,7 @@ class JSError(Exception):
4
4
 
5
5
  class JSException(JSError):
6
6
  pass
7
+
8
+
9
+ class NodeRunTimeNotFoundError(JSError):
10
+ pass
@@ -2,6 +2,7 @@ from __future__ import annotations
2
2
  __package__ = "pytwojs"
3
3
 
4
4
  import json
5
+ import re
5
6
  import subprocess
6
7
  import typing
7
8
  import threading
@@ -9,8 +10,10 @@ import time
9
10
  import uuid
10
11
  import weakref
11
12
 
12
- from .utils import make_cmd
13
- from .exceptions import JSException
13
+ import pyminifyjs
14
+
15
+ from .utils import make_cmd, remove_prefix
16
+ from .exceptions import JSException, NodeRunTimeNotFoundError
14
17
  from ._program import EXEC_END_FLAGS, EXEC_DONE_FLAGS
15
18
 
16
19
 
@@ -30,7 +33,10 @@ class NodeInterpreter:
30
33
 
31
34
  def __init__(self) -> None:
32
35
  cmd = make_cmd()
33
- self._node = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
36
+ try:
37
+ self._node = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
38
+ except Exception:
39
+ raise NodeRunTimeNotFoundError('Could not found runtime(nodejs)')
34
40
  self._global_interpreter_lock = threading.Lock()
35
41
  self._finalizer = weakref.finalize(self, _close, self._node)
36
42
 
@@ -41,11 +47,36 @@ class NodeInterpreter:
41
47
  :return: None
42
48
  """
43
49
  with self._global_interpreter_lock:
44
- self._write(code + b"\n")
50
+ code += b"\n"
51
+ code = pyminifyjs.minify_js(code.decode("utf-8"))
52
+ self._write(code.encode("utf-8"))
45
53
  self._flush()
46
54
  self._write(EXEC_END_FLAGS)
47
55
  self._flush()
56
+ _out = b''
57
+ is_err = False
48
58
  while out := self._readline():
59
+ if is_err:
60
+ _out += out
61
+ try:
62
+ _match = self._extract_json(_out)
63
+ if not _match:
64
+ continue
65
+
66
+ out_obj = json.loads(_match.group(), strict=False)
67
+ except json.JSONDecodeError:
68
+ continue
69
+ else:
70
+ try:
71
+ raise JSException(out_obj['toString'])
72
+ finally:
73
+ self.close()
74
+
75
+ if out.strip()[:8] == b"Uncaught":
76
+ _out += out.strip()[8:]
77
+ is_err = True
78
+ continue
79
+
49
80
  try:
50
81
  out_obj = json.loads(out)
51
82
  if isinstance(out_obj, dict):
@@ -66,8 +97,8 @@ class NodeInterpreter:
66
97
  result = self._reach_result_line()
67
98
 
68
99
  if result[:8] == b"Uncaught":
69
- py_obj = json.loads(result.removeprefix(b"Uncaught").strip())
70
- raise JSException(py_obj['toString'])
100
+ pyobj = json.loads(remove_prefix(result, b"Uncaught").strip())
101
+ raise JSException(pyobj['toString'])
71
102
 
72
103
  return JSProxyWrapper(self, result, code.strip())
73
104
 
@@ -98,6 +129,10 @@ class NodeInterpreter:
98
129
  def close(self) -> None:
99
130
  self._finalizer()
100
131
 
132
+ @staticmethod
133
+ def _extract_json(out: bytes):
134
+ return re.search(rb'\{.*\}', out, re.S)
135
+
101
136
  def _write(self, code: bytes):
102
137
  self._node.stdin.write(code)
103
138
 
@@ -107,6 +142,9 @@ class NodeInterpreter:
107
142
  def _readline(self):
108
143
  return self._node.stdout.readline()
109
144
 
145
+ def _communicate(self):
146
+ return self._node.communicate()
147
+
110
148
  def __enter__(self) -> NodeInterpreter:
111
149
  return self
112
150
 
@@ -136,7 +174,7 @@ class JSProxyWrapper:
136
174
  result = self._node_repl._reach_result_line()
137
175
 
138
176
  if result[:8] == b"Uncaught":
139
- pyobj = json.loads(result.removeprefix(b"Uncaught").strip())
177
+ pyobj = json.loads(remove_prefix(result, b"Uncaught").strip())
140
178
  raise JSException(pyobj['toString'])
141
179
 
142
180
  if not is_obj:
@@ -0,0 +1,29 @@
1
+ import os
2
+ import platform
3
+ import typing
4
+ from ._program import PROGRAM, FLAGS
5
+
6
+
7
+ _version = platform.python_version_tuple()
8
+ _IS_LESS_THAN_PY39 = float(f"{_version[0]}.{_version[1]}") < 3.9
9
+
10
+
11
+ def get_node_env():
12
+ node = os.environ.get('NODE_PATH') if os.environ.get('NODE_PATH') else os.environ.get('NODE')
13
+ if not node:
14
+ return "node"
15
+ return node
16
+
17
+
18
+ def make_cmd():
19
+ node = get_node_env()
20
+ return [node, FLAGS, PROGRAM]
21
+
22
+
23
+ def remove_prefix(s: typing.Union[str, bytes], prefix: typing.Union[str, bytes]):
24
+ if not _IS_LESS_THAN_PY39:
25
+ return s.removeprefix(prefix)
26
+ length = len(prefix)
27
+ if s[:length] == prefix:
28
+ return s[length:]
29
+ return s
@@ -1,11 +1,8 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: pytwojs
3
- Version: 1.0.0
3
+ Version: 1.2.0
4
4
  Summary: A high-performance Python library that relies on nodejs to execute JavaScript
5
5
  Author: Samwe
6
6
  Author-email: 1281722462@qq.com
7
7
  Requires-Python: >=3.8.0
8
- Dynamic: author
9
- Dynamic: author-email
10
- Dynamic: requires-python
11
- Dynamic: summary
8
+ Requires-Dist: pyminifyjs
@@ -7,4 +7,5 @@ pytwojs/utils.py
7
7
  pytwojs.egg-info/PKG-INFO
8
8
  pytwojs.egg-info/SOURCES.txt
9
9
  pytwojs.egg-info/dependency_links.txt
10
+ pytwojs.egg-info/requires.txt
10
11
  pytwojs.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ pyminifyjs
@@ -9,11 +9,14 @@ DESCRIPTION = 'A high-performance Python library that relies on nodejs to execut
9
9
 
10
10
  setup(
11
11
  name="pytwojs",
12
- version="1.0.0",
12
+ version="1.2.0",
13
13
  packages=find_packages(),
14
14
  author=AUTHOR,
15
15
  author_email=EMAIL,
16
16
  python_requires=REQUIRES_PYTHON,
17
- description=DESCRIPTION
17
+ description=DESCRIPTION,
18
+ install_requires=[
19
+ "pyminifyjs"
20
+ ]
18
21
  )
19
22
 
@@ -1,14 +0,0 @@
1
- import os
2
- from ._program import PROGRAM, FLAGS
3
-
4
-
5
- def get_node_env():
6
- node = os.environ.get('NODE_PATH') if os.environ.get('NODE_PATH') else os.environ.get('NODE')
7
- if not node:
8
- return "node"
9
- return node
10
-
11
-
12
- def make_cmd():
13
- node = get_node_env()
14
- return [node, FLAGS, PROGRAM]
File without changes
File without changes
File without changes