From d566c285fdb694f6b34da63d7c20200bb8c725df Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Thu, 19 Sep 2024 02:20:42 +0200 Subject: [PATCH] Adding values and types to PyJS --- LogarithmPlotter/util/js.py | 39 ++++++++++++++++++++++++++++++++++++- tests/python/test_pyjs.py | 21 +++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/LogarithmPlotter/util/js.py b/LogarithmPlotter/util/js.py index 53ba4eb..ffdab5c 100644 --- a/LogarithmPlotter/util/js.py +++ b/LogarithmPlotter/util/js.py @@ -15,10 +15,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ - +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() \ No newline at end of file diff --git a/tests/python/test_pyjs.py b/tests/python/test_pyjs.py index 18c52eb..5742cfc 100644 --- a/tests/python/test_pyjs.py +++ b/tests/python/test_pyjs.py @@ -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() == []