Utils.py

#

Jade Application Kit

  • https://codesardine.github.io/Jade-Application-Kit
  • Vitor Lopes Copyright (c) 2016 - 2019
  • https://vitorlopes.me
import os
import re
from pathlib import Path

register = {}
#
def bindings():
    return os.environ["JAK_PREFERRED_BINDING"]
#
def get_current_path():
    return str(Path('.').absolute())
#
  • Search logic for url rules, we can use regex or simple match the beginning of the domain.
  • :param request_type: WebWindowType
  • :return: function, checks against a list of urls
def check_url_rules(request_type: str, url_request: str, url_rules: tuple) -> bool:
#
    SCHEME = "https://"

    if request_type == "Block":
        url_rules=url_rules

    elif request_type == "WebBrowserTab":
        try:
            url_rules = url_rules["WebBrowserTab"]
        except KeyError:
            url_rules = ""

    elif request_type == "WebBrowserWindow":
        try:
            url_rules = url_rules["WebBrowserWindow"]
        except KeyError:
            url_rules = ""

    for rule in url_rules:
        pattern = re.compile(f"{SCHEME}{rule}")
        if url_request.startswith(f"{SCHEME}{rule}"):
            print(f"{SCHEME}{rule}:Method:startswith")
            return True
        elif re.search(pattern, url_request):
                print(f"{SCHEME}{rule}:Method:regex")
                return True
    return False
#

:Imports: from JAK.Utils import Instance

Add object instances in a dictionary, it can be used to point to references we don,t want to be garbage collected, for usage later

class Instance:
#
#
  • :Usage: Instance.get_instances()
    @staticmethod
    def get_instances() -> dict:
#
        return register
#
  • :Usage: Instance.record(“name”, object)
  • Should only be used once per instance
    @staticmethod
    def record(name: str, _type: object) -> None:
#
        register[name] = _type
        print(f"Registering ['{name}'] Instance")
#
  • :Usage: Instance.retrieve(“name”)
    @staticmethod
    def retrieve(name: str) -> object or str:
#
        try:
            return register[name]
        except KeyError:
            print(f"Instance: ['{name}'] Not Present, to add it use -> Instance.record(['{name}', object])")
            return ""
#
  • :Usage: Instance.auto(“name”, object)
  • Automatically detects if an instance is active with that name and retrieves it. If not present, creates it creates a new one and retrieves it.
  • Should only be used once per instance
    @staticmethod
    def auto(name: str, _type: object) -> object:
#
        try:
            return register[name]
        except KeyError:
            register[name] = _type
        finally:
            print(f"Registering and Retrieving ['{name}'] Instance")
            return register[name]
#
  • Run javascript in the webview after load is complete Injects will be logged in the inspector
  • :Imports: from Jak.Utils import JavaScript
  • :Usage: JavaScript.log(msg)
class JavaScript:
#
  • Outputs console.log() messages in the inspector
  • :param message: Log message
    @staticmethod
    def log(message: str) -> None:
#
        JavaScript.send(f"console.log('JAK log:{message}');")
#
  • Insert custom styles
  • :param styles: CSS -> a { color: red; }
    @staticmethod
    def css(styles: str) -> None:
#
        javascript = f"""
             var style = document.createElement('style');
             style.type = 'text/css';
             style.classList.add('jak-custom-style');
             style.innerHTML = "{JavaScript.detect_type(styles)}";
             document.getElementsByTagName('head')[0].appendChild(style);
        """
        JavaScript.send(javascript)
        JavaScript.log(f"JAK Custom Styles Applied:[{styles}]")
#
  • Triggers an alert message
  • :param message: your popcorn is ready enjoy
    @staticmethod
    def alert(message: str) -> None:
#
        JavaScript.send(f"alert('{message}');")
        JavaScript.log(f"JAK Alert:[{message}]")
#
  • Send custom JavaScript
    @staticmethod
    def send(script: str) -> None:
#
        view = Instance.retrieve("view")
        print(view)
        view.page().runJavaScript(f"{JavaScript.detect_type(script)}")
#
  • Detect if is file or string, convert to string
  • :param script: file or string
    @staticmethod
    def detect_type(script) -> str:
#
        if os.path.exists(script) and os.path.isfile(script):
            try:
                with open(script, "r") as file:
                    string = file.read()
                    return string
            except IOError:
                return False
            return True
        elif isinstance(script, str):
            return script

        else:
            print("JavaScript.send() error, file path or string.")