# 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():


import re
import os
from collections import namedtuple
from gettext import gettext as _
from urllib.parse import urlparse
from .constants import config_dir
        try:
            with open(os.path.join(config_dir, 'url-substitution.rules'), 'rb') as f:
                raw = f.read().decode('utf-8')
        except FileNotFoundError:
            raw = ''
        rules = substitution_rules.rules = parse_rules(raw)
    return rules


def host_matches(host, compiled_host):
    parts = compiled_host.pat.split(host, 1)
    if len(parts) != 2:
        return False
    before, after = parts
    if before and (not compiled_host.prefix or not before.endswith('.')):
        return False
    if after and (not compiled_host.postfix or not after.startswith('.') or after.count('.') != 1):
        return False
    return True


def test_host_matching():
    for line in '''
            foo.com foo.com y
            foo.com *.foo.com y
            www.foo.com *.foo.com y
            www.ab.foo.com *.foo.com y
            xfoo.com *.foo.com n
            foo.co.in foo.co.* y
            www.foo.co.in *.foo.co.* y

# 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/url_substitution.py
import re
import os
from collections import namedtuple
from gettext import gettext as _
from urllib.parse import urlparse
from .constants import config_dir

        try:
            with open(os.path.join(config_dir, 'url-substitution.rules'), 'rb') as f:
                raw = f.read().decode('utf-8')
        except FileNotFoundError:
            raw = ''
        rules = substitution_rules.rules = parse_rules(raw)
    return rules


def host_matches(host, compiled_host):
    parts = compiled_host.pat.split(host, 1)
    if len(parts) != 2:
        return False
    before, after = parts
    if before and (not compiled_host.prefix or not before.endswith('.')):
        return False
    if after and (not compiled_host.postfix or not after.startswith('.') or after.count('.') != 1):
        return False
    return True


def test_host_matching():
    for line in '''
            foo.com foo.com y
            foo.com *.foo.com y
            www.foo.com *.foo.com y
            www.ab.foo.com *.foo.com y
            xfoo.com *.foo.com n
            foo.co.in foo.co.* y
            www.foo.co.in *.foo.co.* y

            www.foo.co.in foo.co.* n