Python codecs.escape_decode() Examples

The following are 30 code examples of codecs.escape_decode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module codecs , or try the search function .
Example #1
Source File: mid3v2.py    From bugatsinho.github.io with GNU General Public License v3.0 7 votes vote down vote up
def value_from_fsnative(arg, escape):
    """Takes an item from argv and returns a text_type value without
    surrogate escapes or raises ValueError.
    """

    assert isinstance(arg, fsnative)

    if escape:
        bytes_ = fsn2bytes(arg)
        if PY2:
            bytes_ = bytes_.decode("string_escape")
        else:
            bytes_ = codecs.escape_decode(bytes_)[0]
        arg = bytes2fsn(bytes_)

    text = fsn2text(arg, strict=True)
    return text 
Example #2
Source File: utils.py    From FPLbot with MIT License 6 votes vote down vote up
def understat_players_data(session):
    """Returns a dict containing general player data retrieved from
    https://understat.com/.
    """
    logger.info("Getting Understat players data.")
    html = await fetch(session, "https://understat.com/league/EPL/")

    soup = BeautifulSoup(html, "html.parser")
    scripts = soup.find_all("script")
    pattern = re.compile(r"var\s+playersData\s+=\s+JSON.parse\(\'(.*?)\'\);")

    for script in scripts:
        match = re.search(pattern, script.string)
        if match:
            break

    byte_data = codecs.escape_decode(match.group(1))
    player_data = json.loads(byte_data[0].decode("utf-8"))

    # Convert Understat player name to FPL player name
    for player in player_data:
        player["team_title"] = understat_team_converter(player["team_title"])
        player["player_name"] = understat_player_converter(player["player_name"])

    return player_data 
Example #3
Source File: core.py    From plyara with Apache License 2.0 5 votes vote down vote up
def p_base64_with_args(self, p):
        '''base64_with_args : LPAREN STRING RPAREN'''
        # Remove parens and leading/trailing quotes
        b64_mod = [x for x in p if x not in (None, '(', ')')][0].strip('"')
        b64_data, _ = escape_decode(b64_mod)
        if len(b64_data) != 64:
            raise Exception("Base64 dictionary length {}, must be 64 characters".format(len(b64_data)))
        if re.search(rb'(.).*\1', b64_data):
            raise Exception("Duplicate character in Base64 dictionary")
        mod_str_mod = YaraBase64(b64_mod)
        logger.debug('Matched string modifier(s): {}'.format(b64_mod))
        self._add_element(ElementTypes.STRINGS_MODIFIER, mod_str_mod) 
Example #4
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_errors(self):
        decode = codecs.escape_decode
        self.assertRaises(ValueError, decode, br"\x")
        self.assertRaises(ValueError, decode, br"[\x]")
        self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6))
        self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6))
        self.assertRaises(ValueError, decode, br"\x0")
        self.assertRaises(ValueError, decode, br"[\x0]")
        self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
        self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) 
Example #5
Source File: string_escape.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #6
Source File: string_escape.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #7
Source File: string_escape.py    From unity-python with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #8
Source File: pickle.py    From android_universal with MIT License 5 votes vote down vote up
def load_string(self):
        data = self.readline()[:-1]
        # Strip outermost quotes
        if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'':
            data = data[1:-1]
        else:
            raise UnpicklingError("the STRING opcode argument must be quoted")
        self.append(self._decode_string(codecs.escape_decode(data)[0])) 
Example #9
Source File: string_escape.py    From canape with GNU General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #10
Source File: utils.py    From understat with MIT License 5 votes vote down vote up
def decode_data(match):
    """Returns data in the match's first group decoded to JSON."""

    byte_data = codecs.escape_decode(match.group(1))
    json_data = json.loads(byte_data[0].decode("utf-8"))

    return json_data 
Example #11
Source File: string_escape.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #12
Source File: string_escape.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #13
Source File: test_codecs.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        self.assertEqual(codecs.escape_decode(""), ("", 0)) 
Example #14
Source File: string_escape.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #15
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_errors(self):
        decode = codecs.escape_decode
        self.assertRaises(ValueError, decode, br"\x")
        self.assertRaises(ValueError, decode, br"[\x]")
        self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6))
        self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6))
        self.assertRaises(ValueError, decode, br"\x0")
        self.assertRaises(ValueError, decode, br"[\x0]")
        self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
        self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) 
Example #16
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_escape(self):
        decode = codecs.escape_decode
        check = coding_checker(self, decode)
        check(b"[\\\n]", b"[]")
        check(br'[\"]', b'["]')
        check(br"[\']", b"[']")
        check(br"[\\]", br"[\]")
        check(br"[\a]", b"[\x07]")
        check(br"[\b]", b"[\x08]")
        check(br"[\t]", b"[\x09]")
        check(br"[\n]", b"[\x0a]")
        check(br"[\v]", b"[\x0b]")
        check(br"[\f]", b"[\x0c]")
        check(br"[\r]", b"[\x0d]")
        check(br"[\7]", b"[\x07]")
        check(br"[\8]", br"[\8]")
        check(br"[\78]", b"[\x078]")
        check(br"[\41]", b"[!]")
        check(br"[\418]", b"[!8]")
        check(br"[\101]", b"[A]")
        check(br"[\1010]", b"[A0]")
        check(br"[\501]", b"[A]")
        check(br"[\x41]", b"[A]")
        check(br"[\X41]", br"[\X41]")
        check(br"[\x410]", b"[A0]")
        for b in range(256):
            if b not in b'\n"\'\\abtnvfr01234567x':
                b = bytes([b])
                check(b'\\' + b, b'\\' + b) 
Example #17
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_raw(self):
        decode = codecs.escape_decode
        for b in range(256):
            b = bytes([b])
            if b != b'\\':
                self.assertEqual(decode(b + b'0'), (b + b'0', 2)) 
Example #18
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        self.assertEqual(codecs.escape_decode(b""), (b"", 0))
        self.assertEqual(codecs.escape_decode(bytearray()), (b"", 0)) 
Example #19
Source File: pickle.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def load_string(self):
        data = self.readline()[:-1]
        # Strip outermost quotes
        if len(data) >= 2 and data[0] == data[-1] and data[0] in b'"\'':
            data = data[1:-1]
        else:
            raise UnpicklingError("the STRING opcode argument must be quoted")
        self.append(self._decode_string(codecs.escape_decode(data)[0])) 
Example #20
Source File: string_escape.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #21
Source File: string_escape.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #22
Source File: string_escape.py    From datafari with Apache License 2.0 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #23
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_errors(self):
        decode = codecs.escape_decode
        self.assertRaises(ValueError, decode, br"\x")
        self.assertRaises(ValueError, decode, br"[\x]")
        self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6))
        self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6))
        self.assertRaises(ValueError, decode, br"\x0")
        self.assertRaises(ValueError, decode, br"[\x0]")
        self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
        self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) 
Example #24
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_escape(self):
        decode = codecs.escape_decode
        check = coding_checker(self, decode)
        check(b"[\\\n]", b"[]")
        check(br'[\"]', b'["]')
        check(br"[\']", b"[']")
        check(br"[\\]", br"[\]")
        check(br"[\a]", b"[\x07]")
        check(br"[\b]", b"[\x08]")
        check(br"[\t]", b"[\x09]")
        check(br"[\n]", b"[\x0a]")
        check(br"[\v]", b"[\x0b]")
        check(br"[\f]", b"[\x0c]")
        check(br"[\r]", b"[\x0d]")
        check(br"[\7]", b"[\x07]")
        check(br"[\8]", br"[\8]")
        check(br"[\78]", b"[\x078]")
        check(br"[\41]", b"[!]")
        check(br"[\418]", b"[!8]")
        check(br"[\101]", b"[A]")
        check(br"[\1010]", b"[A0]")
        check(br"[\501]", b"[A]")
        check(br"[\x41]", b"[A]")
        check(br"[\X41]", br"[\X41]")
        check(br"[\x410]", b"[A0]")
        for b in range(256):
            b = chr(b)
            if b not in '\n"\'\\abtnvfr01234567x':
                check('\\' + b, '\\' + b) 
Example #25
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_raw(self):
        decode = codecs.escape_decode
        for b in range(256):
            b = chr(b)
            if b != '\\':
                self.assertEqual(decode(b + '0'), (b + '0', 2)) 
Example #26
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty(self):
        self.assertEqual(codecs.escape_decode(""), ("", 0)) 
Example #27
Source File: string_escape.py    From ImageFusion with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #28
Source File: string_escape.py    From ImageFusion with MIT License 5 votes vote down vote up
def decode(self, input, final=False):
        return codecs.escape_decode(input, self.errors)[0] 
Example #29
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_escape_decode_errors(self):
        self.assertEqual(codecs.escape_decode("abc", None), (b"abc", 3))

        self.assertEqual(b"?", codecs.escape_decode("\\x", 'replace')[0])
        self.assertEqual(b"?", codecs.escape_decode("\\x2", 'replace')[0])
        self.assertEqual(b"?I", codecs.escape_decode("\\xI", 'replace')[0])
        self.assertEqual(b"?II", codecs.escape_decode("\\xII", 'replace')[0])
        self.assertEqual(b"?I", codecs.escape_decode("\\x1I", 'replace')[0])
        self.assertEqual(b"?I1", codecs.escape_decode("\\xI1", 'replace')[0])

        self.assertEqual(b"abc", codecs.escape_decode("abc\\x", 'ignore')[0])
        self.assertEqual(b"abc", codecs.escape_decode("abc\\x2", 'ignore')[0])
        self.assertEqual(b"abcI", codecs.escape_decode("abc\\xI", 'ignore')[0])
        self.assertEqual(b"abcII", codecs.escape_decode("abc\\xII", 'ignore')[0])
        self.assertEqual(b"abcI", codecs.escape_decode("abc\\x1I", 'ignore')[0])
        self.assertEqual(b"abcI1", codecs.escape_decode("abc\\xI1", 'ignore')[0])

        self.assertRaisesRegex(ValueError, r"Trailing \\ in string", codecs.escape_decode, b"\\", None)
        self.assertRaisesRegex(ValueError, r"Trailing \\ in string", codecs.escape_decode, b"\\", 'strict')
        self.assertRaisesRegex(ValueError, r"Trailing \\ in string", codecs.escape_decode, b"\\", 'replace')
        self.assertRaisesRegex(ValueError, r"Trailing \\ in string", codecs.escape_decode, b"\\", 'ignore')
        self.assertRaisesRegex(ValueError, r"Trailing \\ in string", codecs.escape_decode, b"\\", 'non-existent')

        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\xii")
        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\x1i")
        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\xii", 'strict')
        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\x1i", 'strict')
        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\xii", None)
        self.assertRaisesRegex(ValueError, r"invalid \\x escape at position 3", codecs.escape_decode, b"abc\\x1i", None)

        for errors in ['backslashreplace', 'xmlcharrefreplace', 'namereplace', 'surrogateescape', 'surrogatepass', 'non-existent', '']:
            self.assertRaisesRegex(ValueError, "decoding error; unknown error handling code: " + errors, codecs.escape_decode, b"abc\\xii", errors)
            self.assertRaisesRegex(ValueError, "decoding error; unknown error handling code: " + errors, codecs.escape_decode, b"abc\\x1i", errors)

        self.assertRaises(TypeError, codecs.escape_decode, None)
        self.assertRaises(TypeError, codecs.escape_decode, None, None)
        self.assertRaises(ValueError, codecs.escape_decode, rb"\x", None)
        self.assertRaises(ValueError, codecs.escape_decode, r"\x", None) 
Example #30
Source File: utils.py    From FPLbot with MIT License 5 votes vote down vote up
def understat_matches_data(session, player):
    """Sets the 'matches' attribute of the given player to the data found on
    https://understat.com/player/<player_id>.
    """
    logger.info(f"Getting {player['player_name']} Understat matches data.")
    html = await fetch(session, f"https://understat.com/player/{player['id']}")

    soup = BeautifulSoup(html, "html.parser")
    scripts = soup.find_all("script")
    pattern = re.compile(r"var\s+matchesData\s+=\s+JSON.parse\(\'(.*?)\'\);")

    for script in scripts:
        match = re.search(pattern, script.string)
        if match:
            break

    # If no match could be found, retry (probably rate limited?)
    try:
        byte_data = codecs.escape_decode(match.group(1))
        matches_data = json.loads(byte_data[0].decode("utf-8"))

        for fixture in matches_data:
            fixture["h_team"] = understat_team_converter(fixture["h_team"])
            fixture["a_team"] = understat_team_converter(fixture["a_team"])

        player["understat_history"] = matches_data
    except UnboundLocalError:
        await understat_matches_data(session, player)

    return player