# Path: vise/config.py
# def color(key, default):
#     ans = colors().get(key)
#     if not ans or ans == 'default':
#         ans = default
#     return ans
# 
# @lru_cache()
# def font_sizes():
#     uff = (load_config(user=True).get('fonts') or {}).get('sizes') or {}
#     dff = load_config(user=False)['fonts']['sizes']
#     sizes = {}
# 
#     def conv(x):
#         if x == 'default':
#             return 0
#         try:
#             return int(x)
#         except Exception:
#             pass
# 
#     def get_ff(x):
#         if isinstance(x, dict):
#             return conv(x.get(hostname) or x.get('*'))
#         return conv(x)
# 
#     for ftype, val in uff.items():
#         val = get_ff(val)
#         if isinstance(val, int):
#             sizes[ftype] = val
# 
#     for ftype, val in dff.items():
#         if ftype not in sizes:
#             val = get_ff(val)
#             if isinstance(val, int):
#                 sizes[ftype] = val
# 
#     return sizes
# 
# Path: vise/constants.py
# DOWNLOADS_URL = 'vise:downloads'
# WELCOME_URL = 'vise:welcome'
# STATUS_BAR_HEIGHT = 24
# FOLLOW_LINK_KEY_MAP = {getattr(Qt.Key, 'Key_' + x.upper()): x for x in string.ascii_lowercase + string.digits}
# VISE_SCHEME = 'vise'
# def _get_cache_dir():
# def _get_config_dir():
# def get_windows_username():
# def local_socket_address():
# 
# Path: vise/resources.py
# def get_data_as_file(name):
#     return open(get_data_as_path(name), 'rb')


import glob
import json
import os
import apsw
from base64 import standard_b64decode, standard_b64encode
from binascii import hexlify
from collections import defaultdict
from functools import lru_cache
from PyQt6.QtGui import QKeySequence
from PyQt6.QtWebEngineCore import (QWebEngineProfile, QWebEngineScript,
                                   QWebEngineSettings)
from PyQt6.QtWidgets import QApplication
from .config import color, font_sizes
from .constants import (DOWNLOADS_URL, VISE_SCHEME, appname, cache_dir,
                        config_dir)
from .resources import get_data_as_file
    from .url_intercept import Interceptor
    from .vise_scheme import UrlSchemeHandler
        from .downloads import download_requested
    from .config import font_families, font_sizes
        from .downloads import download_requested
    from .utils import safe_disconnect
        from .utils import parse_url
        os.makedirs(path)
    except FileExistsError:
        pass


def create_script(
    name, src, world=QWebEngineScript.ScriptWorldId.ApplicationWorld, injection_point=QWebEngineScript.InjectionPoint.DocumentCreation, on_subframes=True
):
    script = QWebEngineScript()
    script.setSourceCode(src)
    script.setName(name)
    script.setWorldId(world)
    script.setInjectionPoint(injection_point)
    script.setRunsOnSubFrames(on_subframes)
    return script


TITLE_TOKEN = None


@lru_cache()
def client_script():
    global TITLE_TOKEN
    TITLE_TOKEN = hexlify(os.urandom(32)).decode('ascii')
    name = '%s-client.js' % appname
    f = get_data_as_file(name)
    src = f.read().decode('utf-8')
    src = src.replace('__DOWNLOADS_URL__', DOWNLOADS_URL)
    src = src.replace('HINT_FONT_SIZE', str(font_sizes().get('hint-size')))
    src = src.replace('SELECTED_HINT_BACKGROUND', color('selected hint background', 'khaki'))

# Path: vise/config.py
# def color(key, default):
#     ans = colors().get(key)
#     if not ans or ans == 'default':
#         ans = default
#     return ans
# 
# @lru_cache()
# def font_sizes():
#     uff = (load_config(user=True).get('fonts') or {}).get('sizes') or {}
#     dff = load_config(user=False)['fonts']['sizes']
#     sizes = {}
# 
#     def conv(x):
#         if x == 'default':
#             return 0
#         try:
#             return int(x)
#         except Exception:
#             pass
# 
#     def get_ff(x):
#         if isinstance(x, dict):
#             return conv(x.get(hostname) or x.get('*'))
#         return conv(x)
# 
#     for ftype, val in uff.items():
#         val = get_ff(val)
#         if isinstance(val, int):
#             sizes[ftype] = val
# 
#     for ftype, val in dff.items():
#         if ftype not in sizes:
#             val = get_ff(val)
#             if isinstance(val, int):
#                 sizes[ftype] = val
# 
#     return sizes
# 
# Path: vise/constants.py
# DOWNLOADS_URL = 'vise:downloads'
# WELCOME_URL = 'vise:welcome'
# STATUS_BAR_HEIGHT = 24
# FOLLOW_LINK_KEY_MAP = {getattr(Qt.Key, 'Key_' + x.upper()): x for x in string.ascii_lowercase + string.digits}
# VISE_SCHEME = 'vise'
# def _get_cache_dir():
# def _get_config_dir():
# def get_windows_username():
# def local_socket_address():
# 
# Path: vise/resources.py
# def get_data_as_file(name):
#     return open(get_data_as_path(name), 'rb')


# Path: vise/settings.py
import glob
import json
import os
import apsw
from base64 import standard_b64decode, standard_b64encode
from binascii import hexlify
from collections import defaultdict
from functools import lru_cache
from PyQt6.QtGui import QKeySequence
from PyQt6.QtWebEngineCore import (QWebEngineProfile, QWebEngineScript,
                                   QWebEngineSettings)
from PyQt6.QtWidgets import QApplication
from .config import color, font_sizes
from .constants import (DOWNLOADS_URL, VISE_SCHEME, appname, cache_dir,
                        config_dir)
from .resources import get_data_as_file
    from .url_intercept import Interceptor
    from .vise_scheme import UrlSchemeHandler
        from .downloads import download_requested
    from .config import font_families, font_sizes
        from .downloads import download_requested
    from .utils import safe_disconnect
        from .utils import parse_url

        os.makedirs(path)
    except FileExistsError:
        pass


def create_script(
    name, src, world=QWebEngineScript.ScriptWorldId.ApplicationWorld, injection_point=QWebEngineScript.InjectionPoint.DocumentCreation, on_subframes=True
):
    script = QWebEngineScript()
    script.setSourceCode(src)
    script.setName(name)
    script.setWorldId(world)
    script.setInjectionPoint(injection_point)
    script.setRunsOnSubFrames(on_subframes)
    return script


TITLE_TOKEN = None


@lru_cache()
def client_script():
    global TITLE_TOKEN
    TITLE_TOKEN = hexlify(os.urandom(32)).decode('ascii')
    name = '%s-client.js' % appname
    f = get_data_as_file(name)
    src = f.read().decode('utf-8')
    src = src.replace('__DOWNLOADS_URL__', DOWNLOADS_URL)
    src = src.replace('HINT_FONT_SIZE', str(font_sizes().get('hint-size')))
    src = src.replace('SELECTED_HINT_BACKGROUND', color('selected hint background', 'khaki'))

    src = src.replace('HINT_FOREGROUND', color('hint foreground', 'black'))