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)
This commit is contained in:
Georg Sauthoff 2016-09-23 11:30:18 +02:00
parent a478bfe03e
commit d5220f809e
2 changed files with 9 additions and 8 deletions

View File

@ -1 +1 @@
from inema import Internetmarke from .inema import Internetmarke

View File

@ -2,18 +2,19 @@
from datetime import datetime from datetime import datetime
from pytz import timezone from pytz import timezone
import md5 import hashlib
import json import json
from lxml import etree from lxml import etree
from zeep import Client from zeep import Client
from pkg_resources import resource_stream, Requirement from pkg_resources import resource_stream, Requirement
import requests, zipfile, StringIO import requests, zipfile
import io
import logging import logging
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
products_json = resource_stream(Requirement.parse("inema"), "inema/data/products.json") products_json = resource_stream(__name__, "data/products.json").read().decode()
marke_products = json.load(products_json) marke_products = json.loads(products_json)
def get_product_price_by_id(ext_prod_id): def get_product_price_by_id(ext_prod_id):
price_float_str = marke_products[str(ext_prod_id)]['cost_price'] 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 # concatenate with "::" separator
inp = "%s::%s::%s::%s" % (partner_id, req_ts, key_phase, key) inp = "%s::%s::%s::%s" % (partner_id, req_ts, key_phase, key)
# compute MD5 hash as 32 hex nibbles # 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 the first 8 characters
return md5_hex[:8] return md5_hex[:8]
@ -73,8 +74,8 @@ class Internetmarke(object):
def retrievePNGs(self, link): def retrievePNGs(self, link):
_logger.info("Retrieving PNGs from %s", link) _logger.info("Retrieving PNGs from %s", link)
r = requests.get(link, stream=True) r = requests.get(link, stream=True)
z = zipfile.ZipFile(StringIO.StringIO(r.content)) z = zipfile.ZipFile(io.BytesIO(r.content))
return map(lambda f: z.read(f.filename), z.infolist()) return [ z.read(f.filename) for f in z.infolist() ]
def retrieve_manifest(self, link): def retrieve_manifest(self, link):
_logger.info("Retrieving Manifest from %s", link) _logger.info("Retrieving Manifest from %s", link)