def annotated_errors(self): if self.xml_validator: content = packtools.etree.tostring( self.xml_validator.annotate_errors(), pretty_print=True, encoding='utf-8', xml_declaration=True) content = encoding.decode(content) return content
def read(self, filename): rec_list = [] iso_content = fs_utils.read_file(filename, 'iso-8859-1') utf8_content = encoding.decode(iso_content) utf8_content = html.unescape(utf8_content) utf8_content = utf8_content.replace("\\^", PRESERVECIRC) records = utf8_content.split('!ID ') for record in records[1:]: data = self._get_record_data(record) rec_list.append(data) return rec_list
def tostring(node, pretty_print=False, with_tail=False): """ Retorna o "tostring" do node. Exemplo: node = "<p>texto 1 <bold> texto 2 </bold> texto 3</p>" Retorna `<p>texto 1 <bold> texto 2 </bold> texto 3</p>` Retorna str """ if node is not None: return encoding.decode( etree.tostring(node, encoding='utf-8', pretty_print=pretty_print, with_tail=with_tail))
def _remove_dtd_url_schema(self, xml_file_path): try: xml_tree = xml_utils.get_xml_object(xml_file_path) except xml_utils.etree.XMLSyntaxError: pass else: if xml_tree.docinfo: url = xml_tree.docinfo.system_url dtd_file_name = os.path.basename(url) xml_tree.docinfo.system_url = dtd_file_name xml_content = encoding.decode( xml_utils.etree.tostring( xml_tree, pretty_print=False, doctype=xml_tree.docinfo.doctype, )) return xml_content
def write(self, filename, records): path = os.path.dirname(filename) if not os.path.isdir(path): os.makedirs(path) content = self._format_file(records) content = html.unescape(content) content = content.replace(PRESERVECIRC, "\\^") # converterá a entidades, os caracteres utf-8 que não tem # correspondencia em iso-8859-1 content = encoding.encode(content, "iso-8859-1") content = encoding.decode(content, "iso-8859-1") try: fs_utils.write_file(filename, content, 'iso-8859-1') except (UnicodeError, IOError, OSError) as e: logger.error("Nao foi possivel escrever o arquivo %s: %s", filename, e)
def read_file(filename, encode='utf-8'): if python_version < 3: try: with open(filename, 'r') as fp: content = fp.read() r = encoding.decode(content, encode) except (FileNotFoundError, OSError): return else: return r return try: with open(filename, 'r', encoding=encode) as fp: content = fp.read() except (FileNotFoundError, OSError): return else: return content return
def test_decode_keeps_input_value(self): self.assertEqual(encoding.decode("a"), "a")
def test_decode_converts_input_type(self): self.assertEqual(encoding.decode(b"a"), "a")