From d5220f809eeacc8fc5b8241c3e87167c398b05ff Mon Sep 17 00:00:00 2001 From: Georg Sauthoff Date: Fri, 23 Sep 2016 11:30:18 +0200 Subject: [PATCH] fix Python 3 compatibiltiy the result still is compatible with Python 2 (tested with 2.7) - use relative import in `__init__.py` such that the import of `inema` also works with `PYTHONPATH` set to the repository - use another method to load the json file, i.e. one that also works when the package isn't installed - replace some deprecated imports (that aren't available in Python 3) with their sucessors - replace some Python 2 specific constructs (e.g. `map` that implicitly converts to a list in Python 2 but just returns a map object in Python 3) --- inema/__init__.py | 2 +- inema/inema.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/inema/__init__.py b/inema/__init__.py index 97c287e..c59da1b 100644 --- a/inema/__init__.py +++ b/inema/__init__.py @@ -1 +1 @@ -from inema import Internetmarke +from .inema import Internetmarke diff --git a/inema/inema.py b/inema/inema.py index 40d66e9..676138b 100644 --- a/inema/inema.py +++ b/inema/inema.py @@ -2,18 +2,19 @@ from datetime import datetime from pytz import timezone -import md5 +import hashlib import json from lxml import etree from zeep import Client from pkg_resources import resource_stream, Requirement -import requests, zipfile, StringIO +import requests, zipfile +import io import logging _logger = logging.getLogger(__name__) -products_json = resource_stream(Requirement.parse("inema"), "inema/data/products.json") -marke_products = json.load(products_json) +products_json = resource_stream(__name__, "data/products.json").read().decode() +marke_products = json.loads(products_json) def get_product_price_by_id(ext_prod_id): price_float_str = marke_products[str(ext_prod_id)]['cost_price'] @@ -31,7 +32,7 @@ def gen_1c4a_hdr(partner_id, key_phase, key): # concatenate with "::" separator inp = "%s::%s::%s::%s" % (partner_id, req_ts, key_phase, key) # compute MD5 hash as 32 hex nibbles - md5_hex = md5.new(inp).hexdigest() + md5_hex = hashlib.md5(inp.encode('utf8')).hexdigest() # return the first 8 characters return md5_hex[:8] @@ -73,8 +74,8 @@ class Internetmarke(object): def retrievePNGs(self, link): _logger.info("Retrieving PNGs from %s", link) r = requests.get(link, stream=True) - z = zipfile.ZipFile(StringIO.StringIO(r.content)) - return map(lambda f: z.read(f.filename), z.infolist()) + z = zipfile.ZipFile(io.BytesIO(r.content)) + return [ z.read(f.filename) for f in z.infolist() ] def retrieve_manifest(self, link): _logger.info("Retrieving Manifest from %s", link)