Пример #1
0
def jsConvertData(jsData, jsFnc):
    """
  Description:
  ------------
  Generic conversion function for any data in the internal framework.
  This will convert to String any data coming from the Javascript Python interface.

  Any pure Python object will be converted using the json function to be then written as a string
  to the resulting page

  :param jsData: The Python Javascript data
  :param jsFnc: Optional. The conversion function

  :return:
  """
    if not hasattr(jsData, 'varData') and not hasattr(jsData, 'fncName'):
        if hasattr(jsData, 'toStr'):
            return jsData.toStr()
        else:
            try:
                return JsObject.JsObject(json.dumps(jsData))

            except TypeError as err:
                return str(jsData)

            except Exception as err:
                if isinstance(jsData, range):
                    return JsObject.JsObject(json.dumps(list(jsData)))

                raise

    return jsData
Пример #2
0
def jsConvertData(js_data: Union[str, primitives.JsDataModel, float, dict,
                                 list],
                  js_funcs: Optional[Union[list, str]],
                  depth: bool = False) -> str:
    """
  Description:
  ------------
  Generic conversion function for any data in the internal framework.
  This will convert to String any data coming from the Javascript Python interface.

  Any pure Python object will be converted using the json function to be then written as a string
  to the resulting page.

  Attributes:
  ----------
  :param Union[str, primitives.JsDataModel, float, dict, list] js_data: The Python Javascript data.
  :param Optional[Union[list, str]] js_funcs: Optional. The conversion function (not used).
  :param bool depth: Optional. Set to true of it is a nested object.
  """
    if not hasattr(js_data, 'varData') and not hasattr(js_data, 'fncName'):
        if hasattr(js_data, 'toStr'):
            return js_data.toStr()

        else:
            try:
                if depth:
                    if isinstance(js_data, dict):
                        result = []
                        for k, v in js_data.items():
                            result.append(
                                "%s: %s" %
                                (k, jsConvertData(v, js_funcs, depth=depth)))
                        return "{%s}" % ", ".join(result)

                    else:
                        result = [
                            jsConvertData(v, js_funcs, depth=depth)
                            for v in js_data
                        ]
                        return "[%s]" % ", ".join(result)

                return JsObject.JsObject(json.dumps(js_data))

            except TypeError as err:
                return str(js_data)

            except Exception as err:
                if isinstance(js_data, range):
                    return JsObject.JsObject(json.dumps(list(js_data)))

                raise

    return js_data
Пример #3
0
    def __getitem__(self, index: int):
        if not isinstance(index, int):
            return JsObject.JsObject("%s[%s]" % (self.varId, index),
                                     page=self.page)

        if index < 0:
            return JsObject.JsObject("%s[%s %s]" %
                                     (self.varId, self.length, index),
                                     page=self.page)

        return JsObject.JsObject("%s[%s]" % (self.varId, index),
                                 page=self.page)
Пример #4
0
    def __getitem__(self, index):
        if not isinstance(index, int):
            return JsObject.JsObject("%s[%s]" % (self.varId, index),
                                     report=self._report)

        if index < 0:
            return JsObject.JsObject("%s[%s %s]" %
                                     (self.varId, self.length, index),
                                     report=self._report)

        return JsObject.JsObject("%s[%s]" % (self.varId, index),
                                 report=self._report)
Пример #5
0
    def href(self,
             href: Union[str, primitives.JsDataModel] = None,
             secured: bool = False):
        """
    Description:
    ------------
    The href property sets or returns the entire URL of the current component.

    Usage::

      page.js.location.href("https://www.w3schools.com/howto/howto_js_fullscreen.asp")

    Related Pages:

      https://www.w3schools.com/jsref/prop_loc_href.asp

    Attributes:
    ----------
    :param Union[str, primitives.JsDataModel] href: Optional. Set the href property.
    :param bool secured: Optional.

    :return: A String, representing the entire URL of the page, including the protocol (like http://).
    """
        if href is None:
            return JsString.JsString("location.href", is_py_data=False)

        if not hasattr(href, 'toStr') and href.startswith("www."):
            href = r"http:\\%s" % href if not secured else r"https:\\%s" % href
        return JsObject.JsObject("location.href = %s" %
                                 JsUtils.jsConvertData(href, None))
Пример #6
0
    def href(self, href=None, secured=False):
        """
    Description:
    ------------
    The href property sets or returns the entire URL of the current p

    Usage::

      rptObj.js.location.href("https://www.w3schools.com/howto/howto_js_fullscreen.asp")

    Related Pages:

      https://www.w3schools.com/jsref/prop_loc_href.asp

    Attributes:
    ----------
    :param href: Set the href property

    :return: A String, representing the entire URL of the page, including the protocol (like http://)
    """
        if href is None:
            return JsString.JsString("location.href", isPyData=False)

        if not hasattr(href, 'toStr') and href.startswith("www."):
            href = r"http:\\%s" % href if not secured else r"https:\\%s" % href
        return JsObject.JsObject("location.href = %s" %
                                 JsUtils.jsConvertData(href, None))
Пример #7
0
    def toArgs(self):
        """
    Description:
    -----------

    """
        return JsObject.JsObject("...%s" % self.varId)
Пример #8
0
 def null(self):
     """
 Description:
 -----------
 Javascript null reference
 """
     return JsObject.JsObject("null", is_py_data=False)
Пример #9
0
  def setItem(self, key: Union[primitives.JsDataModel, str], data: Any):
    """
    Description:
    ------------
    Syntax for SAVING data to localStorage.

    The localStorage object stores data with no expiration date.
    The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

    Usage::

      jsObj.localStorage.getItem("lastname", "test")

    Related Pages:

      https://www.w3schools.com/jsref/met_storage_setitem.asp

    Attributes:
    ----------
    :param Union[primitives.JsDataModel, str] key: A String specifying the name of the key you want to set the value of.
    :param Aby data: A String specifying the value of the key you want to set the value of.

    :return: A String, representing the inserted value.
    """
    key = JsUtils.jsConvertData(key, None)
    data = JsUtils.jsConvertData(data, None)
    return JsObject.JsObject("localStorage.setItem(%s, %s)" % (key, data))
Пример #10
0
  def key(self, i: Union[primitives.JsDataModel, int]):
    """
    Description:
    ------------
    The key() method returns name of the key with the specified index.

    The localStorage object stores data with no expiration date.
    The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

    Usage::

      jsObj.localStorage.key(0)

    Related Pages:

      https://www.w3schools.com/jsref/met_storage_key.asp

    Attributes:
    ----------
    :param Union[primitives.JsDataModel, int] i: A Number representing the index of the key you want to get the name of.

    :return: A String, representing the name of the specified key
    """
    i = JsUtils.jsConvertData(i, None)
    return JsObject.JsObject("localStorage.key(%s)" % i)
Пример #11
0
  def setItem(self, key: Union[primitives.JsDataModel, str], data: Any):
    """
    Description:
    ------------
    Syntax for SAVING data to sessionStorage.

    The sessionStorage object stores data for only one session (the data is deleted when the browser tab is closed).

    Usage::

      jsObj.sessionStorage.setItem("lastname", "Smith")
      jsObj.sessionStorage.setItem("lastname", jsObj.objects.get("bin")),

    Related Pages:

      https://www.w3schools.com/Jsref/prop_win_sessionstorage.asp

    Attributes:
    ----------
    :param Union[primitives.JsDataModel, str] key: The key used to store the data in the session cache.
    :param Any data:
    """
    key = JsUtils.jsConvertData(key, None)
    data = JsUtils.jsConvertData(data, None)
    return JsObject.JsObject("sessionStorage.setItem(%s, %s)" % (key, data))
Пример #12
0
  def incr(self, incr):
    """
    Description:
    -----------

    :param incr:
    """
    return JsObject.JsObject("%s++" % incr)
Пример #13
0
 def function(self, args, returns, eval=False):
   params, values = [], []
   for i, v in enumerate(args):
     if i > 0:
       params.append("x%s" % i)
     else:
       params.append("x")
     values.append(str(v))
   if eval:
     return JsObject.JsObject("(function(%s){ return eval(%s) })(%s)" % (", ".join(params), returns, ", ".join(values)))
Пример #14
0
    def toDict(self, header):
        """
    Description:
    -----------

    :param header:
    """
        return JsObject.JsObject(
            "(function(r, h){var rec = {}; h.forEach(function(c, i){rec[c] = r[i]}); return rec})(%s, %s)"
            % (self.varId, header))
Пример #15
0
    def undefined(self):
        """
    Similar as the None in Python

    Documentation
    https://www.w3schools.com/jsref/jsref_undefined.asp

    :return: A Python Js undefined object
    """
        return JsObject.JsObject("undefined", isPyData=False)
Пример #16
0
    def null(self):
        """
    Similar as None in Python

    Documentation
    https://www.w3schools.com/js/js_datatypes.asp

    :return: A Python Js Null object
    """
        return JsObject.JsObject("null")
Пример #17
0
    def unpack(self, jsId, column=None):
        """

    :param jsId:
    :param column:
    """
        if not jsId in self._js_ids:
            column = JsUtils.jsConvertData(column, None)
            self._js_frg.append("%s.push(row[%s])" % (jsId, column))
            self._js_ids.add(jsId)
        return JsObject.JsObject(jsId, isPyData=False)
Пример #18
0
class DataReduce(object):
    """
  Description:
  -----------

  rVal  :
  val   :
  index :
  """
    rVal, val, index = JsObject.JsObject("r"), JsNumber.JsNumber(
        "o", isPyData=False), JsNumber.JsNumber("i", isPyData=False)
Пример #19
0
class DataEach:
    """
  Description:
  -----------
  Data Class for the Jquery each loop

  index : index
  data  : element
  """
    index, data = JsNumber.JsNumber(
        "index", is_py_data=False), JsObject.JsObject("data", is_py_data=False)
Пример #20
0
    def incr(self, incr: str):
        """
    Description:
    -----------
    Increment a counter.

    Attributes:
    ----------
    :param str incr: the variable name used to store the counter.
    """
        return JsObject.JsObject("%s++" % incr)
Пример #21
0
class DataReduce:
    """
  Description:
  -----------

  rVal  :
  val   :
  index :
  """
    rVal, val = JsObject.JsObject("r"), JsNumber.JsNumber("o",
                                                          is_py_data=False)
    index = JsNumber.JsNumber("i", is_py_data=False)
Пример #22
0
  def key(self, i: Union[primitives.JsDataModel, int]):
    """
    Description:
    ------------
    The sessionStorage object stores data for only one session (the data is deleted when the browser tab is closed).

    Attributes:
    ----------
    :param Union[primitives.JsDataModel, int] i: The key number.
    """
    i = JsUtils.jsConvertData(i, None)
    return JsObject.JsObject("sessionStorage.key(%s)" % i)
Пример #23
0
    def root(self):
        """
    Description:
    ------------
    The IntersectionObserver interface's read-only root property identifies the Element whose bounds are treated as the bounding box of the viewport for the element which is the observer's target.
    If the root is null, then the bounds of the actual document viewport are used.

    Related Pages:

      https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root
    """
        return JsObject.JsObject("%s.root", isPyData=False)
Пример #24
0
    def null(self):
        """
    Description:
    -----------
    Similar as None in Python.

    Related Pages:

      https://www.w3schools.com/js/js_datatypes.asp

    :return: A Python Js Null object.
    """
        return JsObject.JsObject("null")
Пример #25
0
class DataLoop:
    """
  Description:
  -----------
  Data Class used for all the loop and map in the Javascript side.
  This will get the below attributes

  val   : The current value in the loop
  index : The index item
  arr   : The full array (only available in case of arrays, map, filter, every  )
  """
    val, index, arr = JsObject.JsObject("value"), JsNumber.JsNumber(
        "index", is_py_data=False), JsArray.JsArray("arr")
Пример #26
0
    def undefined(self):
        """
    Description:
    -----------
    Similar as the None in Python.

    Related Pages:

      https://www.w3schools.com/jsref/jsref_undefined.asp

    :return: A Python Js undefined object.
    """
        return JsObject.JsObject("undefined", is_py_data=False)
Пример #27
0
    def clearWatch(self, watchId):
        """
    Stops the watchPosition() method

    Related Pages:

      https://www.w3schools.com/html/html5_geolocation.asp

    :param watchId: The ID number returned by the Geolocation.watchPosition() method when installing the handler you wish to remove.
    """
        watchId = JsUtils.jsConvertData(watchId, None)
        return JsObject.JsObject("navigator.geolocation.clearWatch(%s)" %
                                 watchId,
                                 isPyData=False)
Пример #28
0
    def shift(self):
        """
    The shift() method removes the first item of an array.

    Example
    jsObj.console.log(jsObj.objects.array.new([2, 5, 12, -3], "MyArray").shift()),
    jsObj.console.log(jsObj.objects.array.get("MyArray")),

    Documentation
    https://www.w3schools.com/jsref/jsref_shift.asp

    :return: Any type*, representing the removed array item. *An array item can be a string, a number, an array, a boolean, or any other object types that are allowed in an array.
    """
        return JsObject.JsObject("%s.shift()" % self.varId, isPyData=False)
Пример #29
0
    def pop(self):
        """
    The pop() method removes the last element of an array, and returns that element.

    Example
    jsObj.objects.array.new([2, 5, 12, -3], "MyArray")
    jsObj.objects.array.get("MyArray").pop()

    Documentation:
    https://www.w3schools.com/js/js_array_methods.asp

    :return: Any type*, representing the removed array item. *An array item can be a string, a number, an array, a boolean, or any other object types that are allowed in an array.
    """
        return JsObject.JsObject("%s.pop()" % self.varId, isPyData=False)
Пример #30
0
  def unpack(self, js_code: str, column: Union[str, primitives.JsDataModel] = None):
    """
    Description:
    -----------

    Attributes:
    ----------
    :param str js_code: The variable id to store the records.
    :param Union[str, primitives.JsDataModel] column: The column name.
    """
    if js_code not in self._js_ids:
      column = JsUtils.jsConvertData(column, None)
      self._js_frg.append("%s.push(row[%s])" % (js_code, column))
      self._js_ids.add(js_code)
    return JsObject.JsObject(js_code, is_py_data=False)