# -*- coding: utf-8 -*-

import re
from urllib.parse import urlparse

domain_re = re.compile(r'^(?!-)[a-z0-9-]{1,63}(?<!-)(?:\.(?!-)[a-z0-9-]{1,63}(?<!-)){0,125}\.(?!-)(?![0-9]+$)[a-z0-9-]{1,63}(?<!-)$')
box_re = re.compile(r'^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*$')

def is_valid_domain(domain):
    return bool(domain_re.match(domain))

def is_valid_port(port):
    try:
        port = int(port)
        return 0 < port < 65536 and port not in (22, 25, 80, 8080)
    except TypeError:
        return False

def is_valid_email(email):
    parts = email.split('@')
    return len(parts) == 2 and bool(box_re.match(parts[0])) and bool(domain_re.match(parts[1]))

def is_valid_repo_url(url):
    # Check if URL is valid http(s) and doesn't contain extra parts
    try:
        parsed = urlparse(url)
        return parsed.scheme in ('http', 'https') and not parsed.params and not parsed.query and not parsed.fragment
    except ValueError:
        return False
