The Seven Year Itch.

MENU

Python 3.7的新特性

October 29, 2018 • Read: 146 • Python

Python 3.7 新特性

# -*- encoding:utf-8 -*-
"""
@ Created by Seven on  2018/10/26 
"""
from enum import Enum
from dataclasses import dataclass
from typing import Optional, Any, List, TypeVar, Type, Callable, cast


T = TypeVar("T")
EnumT = TypeVar("EnumT", bound=Enum)


def from_bool(x: Any) -> bool:
    assert isinstance(x, bool)
    return x


def from_str(x: Any) -> str:
    assert isinstance(x, str)
    return x


def from_int(x: Any) -> int:
    assert isinstance(x, int) and not isinstance(x, bool)
    return x


def from_none(x: Any) -> Any:
    assert x is None
    return x


def from_union(fs, x):
    for f in fs:
        try:
            return f(x)
        except:
            pass
    assert False


def to_enum(c: Type[EnumT], x: Any) -> EnumT:
    assert isinstance(x, c)
    return x.value


def from_list(f: Callable[[Any], T], x: Any) -> List[T]:
    assert isinstance(x, list)
    return [f(y) for y in x]


def to_class(c: Type[T], x: Any) -> dict:
    assert isinstance(x, c)
    return cast(Any, x).to_dict()


class Group(Enum):
    SS = "SS"
    SSR = "SSR"


class Method(Enum):
    AES_256_CFB = "aes-256-cfb"
    CHACHA20_IETF = "chacha20-ietf"


class Obfs(Enum):
    HTTP_SIMPLE = "http_simple"
    PLAIN = "plain"
    TLS1_2__TICKET_AUTH = "tls1.2_ticket_auth"


class Password(Enum):
    SOO9S_COM = "soo9s.com"
    SOO9S_ME = "soo9s.me"


class Protocol(Enum):
    AUTH_AES128_MD5 = "auth_aes128_md5"
    AUTH_AES128_SHA1 = "auth_aes128_sha1"
    AUTH_CHAIN_A = "auth_chain_a"


class Protocolparam(Enum):
    THE_5113_CCT_IN_W = "5113:cctInW"


@dataclass
class Config:
    enable: bool
    password: Password
    method: Method
    remarks: str
    server: str
    obfs: Obfs
    protocol: Protocol
    protocolparam: Protocolparam
    group: Group
    server_port: int
    remarks_base64: str
    obfsparam: Optional[str]

    @staticmethod
    def from_dict(obj: Any) -> 'Config':
        assert isinstance(obj, dict)
        enable = from_bool(obj.get("enable"))
        password = Password(obj.get("password"))
        method = Method(obj.get("method"))
        remarks = from_str(obj.get("remarks"))
        server = from_str(obj.get("server"))
        obfs = Obfs(obj.get("obfs"))
        protocol = Protocol(obj.get("protocol"))
        protocolparam = Protocolparam(obj.get("protocolparam"))
        group = Group(obj.get("group"))
        server_port = from_int(obj.get("server_port"))
        remarks_base64 = from_str(obj.get("remarks_base64"))
        obfsparam = from_union([from_str, from_none], obj.get("obfsparam"))
        return Config(enable, password, method, remarks, server, obfs, protocol, protocolparam, group, server_port, remarks_base64, obfsparam)

    def to_dict(self) -> dict:
        result: dict = {}
        result["enable"] = from_bool(self.enable)
        result["password"] = to_enum(Password, self.password)
        result["method"] = to_enum(Method, self.method)
        result["remarks"] = from_str(self.remarks)
        result["server"] = from_str(self.server)
        result["obfs"] = to_enum(Obfs, self.obfs)
        result["protocol"] = to_enum(Protocol, self.protocol)
        result["protocolparam"] = to_enum(Protocolparam, self.protocolparam)
        result["group"] = to_enum(Group, self.group)
        result["server_port"] = from_int(self.server_port)
        result["remarks_base64"] = from_str(self.remarks_base64)
        result["obfsparam"] = from_union([from_str, from_none], self.obfsparam)
        return result


@dataclass
class Export:
    random: bool
    auth_pass: None
    use_online_pac: bool
    ttl: int
    export_global: bool
    reconnect_times: int
    index: int
    proxy_type: int
    proxy_host: None
    auth_user: None
    proxy_auth_pass: None
    is_default: bool
    pac_url: None
    configs: List[Config]
    proxy_port: int
    random_algorithm: int
    proxy_enable: bool
    enabled: bool
    autoban: bool
    proxy_auth_user: None
    share_over_lan: bool
    local_port: int

    @staticmethod
    def from_dict(obj: Any) -> 'Export':
        assert isinstance(obj, dict)
        random = from_bool(obj.get("random"))
        auth_pass = from_none(obj.get("authPass"))
        use_online_pac = from_bool(obj.get("useOnlinePac"))
        ttl = from_int(obj.get("TTL"))
        export_global = from_bool(obj.get("global"))
        reconnect_times = from_int(obj.get("reconnectTimes"))
        index = from_int(obj.get("index"))
        proxy_type = from_int(obj.get("proxyType"))
        proxy_host = from_none(obj.get("proxyHost"))
        auth_user = from_none(obj.get("authUser"))
        proxy_auth_pass = from_none(obj.get("proxyAuthPass"))
        is_default = from_bool(obj.get("isDefault"))
        pac_url = from_none(obj.get("pacUrl"))
        configs = from_list(Config.from_dict, obj.get("configs"))
        proxy_port = from_int(obj.get("proxyPort"))
        random_algorithm = from_int(obj.get("randomAlgorithm"))
        proxy_enable = from_bool(obj.get("proxyEnable"))
        enabled = from_bool(obj.get("enabled"))
        autoban = from_bool(obj.get("autoban"))
        proxy_auth_user = from_none(obj.get("proxyAuthUser"))
        share_over_lan = from_bool(obj.get("shareOverLan"))
        local_port = from_int(obj.get("localPort"))
        return Export(random, auth_pass, use_online_pac, ttl, export_global, reconnect_times, index, proxy_type, proxy_host, auth_user, proxy_auth_pass, is_default, pac_url, configs, proxy_port, random_algorithm, proxy_enable, enabled, autoban, proxy_auth_user, share_over_lan, local_port)

    def to_dict(self) -> dict:
        result: dict = {}
        result["random"] = from_bool(self.random)
        result["authPass"] = from_none(self.auth_pass)
        result["useOnlinePac"] = from_bool(self.use_online_pac)
        result["TTL"] = from_int(self.ttl)
        result["global"] = from_bool(self.export_global)
        result["reconnectTimes"] = from_int(self.reconnect_times)
        result["index"] = from_int(self.index)
        result["proxyType"] = from_int(self.proxy_type)
        result["proxyHost"] = from_none(self.proxy_host)
        result["authUser"] = from_none(self.auth_user)
        result["proxyAuthPass"] = from_none(self.proxy_auth_pass)
        result["isDefault"] = from_bool(self.is_default)
        result["pacUrl"] = from_none(self.pac_url)
        result["configs"] = from_list(lambda x: to_class(Config, x), self.configs)
        result["proxyPort"] = from_int(self.proxy_port)
        result["randomAlgorithm"] = from_int(self.random_algorithm)
        result["proxyEnable"] = from_bool(self.proxy_enable)
        result["enabled"] = from_bool(self.enabled)
        result["autoban"] = from_bool(self.autoban)
        result["proxyAuthUser"] = from_none(self.proxy_auth_user)
        result["shareOverLan"] = from_bool(self.share_over_lan)
        result["localPort"] = from_int(self.local_port)
        return result


def export_from_dict(s: Any) -> Export:
    return Export.from_dict(s)


def export_to_dict(x: Export) -> Any:
    return to_class(Export, x)
Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code