diff --git a/bin/facturx-pdfgen b/bin/facturx-pdfgen index ccab3cd..afd5e00 100755 --- a/bin/facturx-pdfgen +++ b/bin/facturx-pdfgen @@ -89,7 +89,7 @@ def main(options, arguments): '3rd argument %s is a directory name (should be a the ' 'Factur-X PDF filename)', facturx_pdf_filename) sys.exit(1) - xml_file = open(xml_filename) + xml_file = open(xml_filename, 'rb') check_xsd = True if options.disable_xsd_check: check_xsd = False diff --git a/bin/facturx-xmlcheck b/bin/facturx-xmlcheck index eebfe87..6bbca71 100755 --- a/bin/facturx-xmlcheck +++ b/bin/facturx-xmlcheck @@ -59,13 +59,13 @@ def main(options, arguments): if not isfile(xml_filename): logger.error('%s is not a filename', xml_filename) sys.exit(1) - xml_file = open(xml_filename) + xml_file = open(xml_filename, 'rb') # The important line of code is below ! try: check_facturx_xsd( xml_file, flavor=options.flavor, facturx_level=options.facturx_level) - except Exception, e: + except Exception as e: logger.error(e) sys.exit(1) diff --git a/facturx/facturx.py b/facturx/facturx.py index 581625b..25995cd 100644 --- a/facturx/facturx.py +++ b/facturx/facturx.py @@ -63,6 +63,11 @@ FACTURX_LEVEL2xmp = { 'en16931': 'EN 16931', } +import sys +if sys.version_info[0] == 3: + unicode = str + from io import IOBase + file = IOBase def check_facturx_xsd( facturx_xml, flavor='autodetect', facturx_level='autodetect'): @@ -86,7 +91,7 @@ def check_facturx_xsd( if not isinstance(facturx_level, (type(None), str, unicode)): raise ValueError('Wrong type for facturx_level argument') facturx_xml_etree = None - if isinstance(facturx_xml, str): + if isinstance(facturx_xml, (str, bytes)): xml_string = facturx_xml elif isinstance(facturx_xml, unicode): xml_string = facturx_xml.encode('utf8') @@ -168,7 +173,7 @@ def get_facturx_xml_from_pdf(pdf_invoice, check_xsd=True): # embeddedfiles must contain an even number of elements if len(embeddedfiles) % 2 != 0: raise - embeddedfiles_by_two = zip(embeddedfiles, embeddedfiles[1:])[::2] + embeddedfiles_by_two = list(zip(embeddedfiles, embeddedfiles[1:]))[::2] logger.debug('embeddedfiles_by_two=%s', embeddedfiles_by_two) for (filename, file_obj) in embeddedfiles_by_two: logger.debug('found filename=%s', filename) @@ -217,7 +222,7 @@ def _prepare_pdf_metadata_txt(pdf_metadata): '/Author': pdf_metadata.get('author', ''), '/CreationDate': pdf_date, '/Creator': - u'factur-x Python lib v%s by Alexis de Lattre' % __version__, + 'factur-x Python lib v%s by Alexis de Lattre' % __version__, '/Keywords': pdf_metadata.get('keywords', ''), '/ModDate': pdf_date, '/Subject': pdf_metadata.get('subject', ''), @@ -319,9 +324,9 @@ def _prepare_pdf_metadata_xml(facturx_level, pdf_metadata): # TODO: should be UTF-16be ?? xml_str = etree.tostring( root, pretty_print=True, encoding="UTF-8", xml_declaration=False) - head = u''.encode( + head = ''.encode( 'utf-8') - tail = u''.encode('utf-8') + tail = ''.encode('utf-8') xml_final_str = head + xml_str + tail logger.debug('metadata XML:') logger.debug(xml_final_str) @@ -329,7 +334,7 @@ def _prepare_pdf_metadata_xml(facturx_level, pdf_metadata): # def createByteObject(string): -# string_to_encode = u'\ufeff' + string +# string_to_encode = '\ufeff' + string # x = string_to_encode.encode('utf-16be') # return ByteStringObject(x) @@ -380,6 +385,7 @@ def _facturx_update_metadata_add_attachment( '''This method is inspired from the code of the addAttachment() method of the PyPDF2 lib''' # The entry for the file + # facturx_xml_str = facturx_xml_str.encode('utf-8') md5sum = hashlib.md5(facturx_xml_str).hexdigest() md5sum_obj = createStringObject(md5sum) params_dict = DictionaryObject({ @@ -500,9 +506,9 @@ def _extract_base_info(facturx_xml_etree): def _base_info2pdf_metadata(base_info): if base_info['doc_type'] == '381': - doc_type_name = u'Refund' + doc_type_name = 'Refund' else: - doc_type_name = u'Invoice' + doc_type_name = 'Invoice' date_str = datetime.strftime(base_info['date'], '%Y-%m-%d') title = '%s: %s %s' % ( base_info['seller'], doc_type_name, base_info['number']) @@ -510,7 +516,7 @@ def _base_info2pdf_metadata(base_info): doc_type_name, base_info['number'], date_str, base_info['seller']) pdf_metadata = { 'author': base_info['seller'], - 'keywords': u'%s, Factur-X' % doc_type_name, + 'keywords': '%s, Factur-X' % doc_type_name, 'title': title, 'subject': subject, } @@ -615,7 +621,7 @@ def generate_facturx_from_binary( :rtype: string """ - if not isinstance(pdf_invoice, str): + if not isinstance(pdf_invoice, bytes): raise ValueError('pdf_invoice argument must be a string') facturx_pdf_invoice = False with NamedTemporaryFile(prefix='invoice-facturx-', suffix='.pdf') as f: @@ -641,7 +647,7 @@ def generate_facturx_from_file( (type string) or as file object :type pdf_invoice: string or file :param facturx_xml: the Factur-X XML - :type facturx_xml: string, file or etree object + :type facturx_xml: bytes, string, file or etree object :param facturx_level: the level of the Factur-X XML file. Default value is 'autodetect'. The only advantage to specifiy a particular value instead of using the autodetection is for a very very small perf improvement. @@ -746,7 +752,7 @@ def generate_facturx_from_file( pdf_metadata = _base_info2pdf_metadata(base_info) else: # clean-up pdf_metadata dict - for key, value in pdf_metadata.iteritems(): + for key, value in pdf_metadata.items(): if not isinstance(value, (str, unicode)): pdf_metadata[key] = '' facturx_level = facturx_level.lower()