Adding values and types to PyJS

This commit is contained in:
Adsooi 2024-09-19 02:20:42 +02:00
parent a9e47dbc17
commit d566c285fd
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
2 changed files with 58 additions and 2 deletions

View file

@ -15,10 +15,16 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from re import Pattern
from PySide6.QtCore import QMetaObject, QObject
from PySide6.QtQml import QJSValue
class InvalidAttributeValueException(Exception): pass
class NotAPrimitiveException(Exception): pass
class Function: pass
class Date: pass
class URL: pass
class PyJSValue:
"""
@ -68,3 +74,34 @@ class PyJSValue:
if isinstance(value, QJSValue):
value = PyJSValue(value)
return value
def type(self) -> any:
matcher = [
(lambda: self.qjs_value.isArray(), list),
(lambda: self.qjs_value.isBool(), bool),
(lambda: self.qjs_value.isCallable(), Function),
(lambda: self.qjs_value.isDate(), Date),
(lambda: self.qjs_value.isError(), Exception),
(lambda: self.qjs_value.isNull(), None),
(lambda: self.qjs_value.isNumber(), float),
(lambda: self.qjs_value.isQMetaObject(), QMetaObject),
(lambda: self.qjs_value.isQObject(), QObject),
(lambda: self.qjs_value.isRegExp(), Pattern),
(lambda: self.qjs_value.isUndefined(), None),
(lambda: self.qjs_value.isUrl(), URL),
(lambda: self.qjs_value.isString(), str),
(lambda: self.qjs_value.isObject(), object),
]
for (test, value) in matcher:
if test():
return value
return None
def primitive(self):
"""
Returns the pythonic value of the given primitive data.
Raises a NotAPrimitiveException() if this JS value is not a primitive.
"""
if self.type() not in [bool, float, str, None]:
raise NotAPrimitiveException()
return self.qjs_value.toPrimitive().toVariant()

View file

@ -17,9 +17,10 @@
"""
import pytest
from re import Pattern
from PySide6.QtQml import QJSEngine, QJSValue
from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException
from LogarithmPlotter.util.js import PyJSValue, InvalidAttributeValueException, NotAPrimitiveException
from globals import app
@pytest.fixture()
@ -56,3 +57,21 @@ class TestPyJS:
function3 = PyJSValue(engine.evaluate("2+2"))
with pytest.raises(InvalidAttributeValueException):
function3()
def test_type(self, data):
engine, obj = data
assert PyJSValue(engine.evaluate("[]")).type() == list
assert PyJSValue(engine.evaluate("undefined")).type() is None
assert PyJSValue(engine.evaluate("/[a-z]/g")).type() == Pattern
assert PyJSValue(QJSValue(2)).type() == float
assert PyJSValue(QJSValue("3")).type() == str
assert PyJSValue(QJSValue(True)).type() == bool
def test_primitive(self, data):
engine, obj = data
assert PyJSValue(QJSValue(2)).primitive() == 2
assert PyJSValue(QJSValue("string")).primitive() == "string"
assert PyJSValue(QJSValue(True)).primitive() == True
assert PyJSValue(engine.evaluate("undefined")).primitive() is None
with pytest.raises(NotAPrimitiveException):
assert PyJSValue(engine.evaluate("[]")).primitive() == []