示例#1
0
 def encoding(self):
     if self._encoding is None:
         mime = parse_mimetype(self.headers.get('content-type'))
         self._encoding = mime.charset
     if self._encoding is None:
         with open(self.file_path, 'rb') as fh:
             self._encoding = guess_file_encoding(fh)
     return self._encoding
示例#2
0
 def encoding(self):
     if self._encoding is None:
         content_type = self.headers.get('content-type')
         if content_type is not None:
             content_type, options = cgi.parse_header(content_type)
             charset = options.get('charset', '')
             charset = stringify(charset.lower().strip())
             if charset is not None:
                 self._encoding = charset
     if self._encoding is None:
         with open(self.file_path, 'r') as fh:
             self._encoding = guess_file_encoding(fh)
     return self._encoding
示例#3
0
 def test_guess_file_encoding(self):
     with open("tests/fixtures/utf-16.txt", "rb") as fh:
         out = guess_file_encoding(fh)
         self.assertEqual("utf-16", out)
示例#4
0
 def detect_stream_encoding(self, fh):
     return guess_file_encoding(fh, self.DEFAULT_ENCODING)