import re

attr_names = {
    "@alias":   "@aka",
    "hostname": "host",
    "machine":  "host",
    "url":      "uri",
    "website":  "uri",
    "user":     "username",
    "nicname":  "nic-hdl",
    "password": "pass",
    "!pass":    "pass",
    "mail":     "email",
    "tel":      "phone",
    "ssid":     "wifi.essid",
    "wifi.ssid":"wifi.essid",
}

attr_groups = {
    "object":   ["entity", "host", "uri", "realm"],
    "username": ["login", "login.", "username", "nic-hdl", "principal"],
    "password": ["pass", "!pass"],
    "email":    ["email", "phone"],
}

attr_order = ["object", "username", "password", "email"]

attr_prefix_re = re.compile(r"^\W+")

def attr_is_metadata(name):
    return name.startswith("@")

def attr_is_private(name):
    return name.startswith("!") or name == "pass"

def attr_is_reflink(name):
    return name.startswith("ref.")

def translate_attr(name):
    return attr_names.get(name, name)

def sort_attrs(entry):
    canonicalize = lambda k: attr_prefix_re.sub("", translate_attr(k))
    names = []
    names += sorted([k for k in entry.attributes
                       if attr_is_metadata(k)])
    for group in attr_order:
        for attr in attr_groups[group]:
            names += sorted([k for k in entry.attributes \
                               if (k == attr
                                   or (attr.endswith(".") and k.startswith(attr)))],
                            key=canonicalize)
    names += sorted([k for k in entry.attributes if k not in names],
                    key=canonicalize)
    return names

class WiFiParameters(object):
    # https://github.com/zxing/zxing/wiki/Barcode-Contents

    def __init__(self, essid, psk, sectype=None, hidden=False):
        if sectype is None:
            sectype = "WPA" if psk else "nopass"
        elif sectype not in {"WPA", "WEP", "nopass"}:
            raise ValueError("unknown Wi-Fi security type %r" % sectype)

        self.essid = essid
        self.psk = psk
        self.sectype = sectype
        self.hidden = hidden

    def _escape(self, text, quote=True):
        for char in "\\;,:\"":
            text = text.replace(char, "\\" + char)
        if quote and len(text) % 2 == 0 and re.match(r"^[0-9A-F]+$", text, re.I):
            text = '"%s"' % text
        return text

    def make_uri(self):
        data = ["S:%s" % self._escape(self.essid)]
        if self.sectype != "nopass":
            data += [
                "T:%s" % self.sectype,
                "P:%s" % self._escape(self.psk),
            ]
        if self.hidden:
            data += ["H:true"]
        return "WIFI:" + ";".join(data) + ";"

@@ -2184,31 +2184,8 @@
 and 
-len(text) %25 2 == 0 and 
 re.m
@@ -2196,16 +2196,17 @@
 (r%22%5E
+(
 %5B0-9A-F%5D
 +$%22,
@@ -2201,16 +2201,25 @@
 %5B0-9A-F%5D
+%5B0-9A-F%5D)
 +$%22, tex

simplify hex check