MCPcopy Create free account
hub / github.com/clips/pattern / _detectEncoding

Method _detectEncoding

pattern/web/soup/BeautifulSoup.py:1867–1932  ·  view source on GitHub ↗

Given a document, tries to detect its XML encoding.

(self, xml_data, isHTML=False)

Source from the content-addressed store, hash-verified

1865 return newdata
1866
1867 def _detectEncoding(self, xml_data, isHTML=False):
1868 """Given a document, tries to detect its XML encoding."""
1869 xml_encoding = sniffed_xml_encoding = None
1870 try:
1871 if xml_data[:4] == '\x4c\x6f\xa7\x94':
1872 # EBCDIC
1873 xml_data = self._ebcdic_to_ascii(xml_data)
1874 elif xml_data[:4] == '\x00\x3c\x00\x3f':
1875 # UTF-16BE
1876 sniffed_xml_encoding = 'utf-16be'
1877 xml_data = unicode(xml_data, 'utf-16be').encode('utf-8')
1878 elif (len(xml_data) >= 4) and (xml_data[:2] == '\xfe\xff') \
1879 and (xml_data[2:4] != '\x00\x00'):
1880 # UTF-16BE with BOM
1881 sniffed_xml_encoding = 'utf-16be'
1882 xml_data = unicode(xml_data[2:], 'utf-16be').encode('utf-8')
1883 elif xml_data[:4] == '\x3c\x00\x3f\x00':
1884 # UTF-16LE
1885 sniffed_xml_encoding = 'utf-16le'
1886 xml_data = unicode(xml_data, 'utf-16le').encode('utf-8')
1887 elif (len(xml_data) >= 4) and (xml_data[:2] == '\xff\xfe') and \
1888 (xml_data[2:4] != '\x00\x00'):
1889 # UTF-16LE with BOM
1890 sniffed_xml_encoding = 'utf-16le'
1891 xml_data = unicode(xml_data[2:], 'utf-16le').encode('utf-8')
1892 elif xml_data[:4] == '\x00\x00\x00\x3c':
1893 # UTF-32BE
1894 sniffed_xml_encoding = 'utf-32be'
1895 xml_data = unicode(xml_data, 'utf-32be').encode('utf-8')
1896 elif xml_data[:4] == '\x3c\x00\x00\x00':
1897 # UTF-32LE
1898 sniffed_xml_encoding = 'utf-32le'
1899 xml_data = unicode(xml_data, 'utf-32le').encode('utf-8')
1900 elif xml_data[:4] == '\x00\x00\xfe\xff':
1901 # UTF-32BE with BOM
1902 sniffed_xml_encoding = 'utf-32be'
1903 xml_data = unicode(xml_data[4:], 'utf-32be').encode('utf-8')
1904 elif xml_data[:4] == '\xff\xfe\x00\x00':
1905 # UTF-32LE with BOM
1906 sniffed_xml_encoding = 'utf-32le'
1907 xml_data = unicode(xml_data[4:], 'utf-32le').encode('utf-8')
1908 elif xml_data[:3] == '\xef\xbb\xbf':
1909 # UTF-8 with BOM
1910 sniffed_xml_encoding = 'utf-8'
1911 xml_data = unicode(xml_data[3:], 'utf-8').encode('utf-8')
1912 else:
1913 sniffed_xml_encoding = 'ascii'
1914 pass
1915 except:
1916 xml_encoding_match = None
1917 xml_encoding_match = re.compile(
1918 '^<\?.*encoding=[\'"](.*?)[\'"].*\?>&#x27;).match(xml_data)
1919 if not xml_encoding_match and isHTML:
1920 regexp = re.compile('<\s*meta[^>]+charset=([^>]*?)[;\'">]&#x27;, re.I)
1921 xml_encoding_match = regexp.search(xml_data)
1922 if xml_encoding_match is not None:
1923 xml_encoding = xml_encoding_match.groups()[0].lower()
1924 if isHTML:

Callers 1

__init__Method · 0.95

Calls 5

_ebcdic_to_asciiMethod · 0.95
lenFunction · 0.85
encodeMethod · 0.45
matchMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected