# Path: FabLabKasse/shopping/backend/abstract.py
# def float_to_decimal(number, digits):
#     """
#     convert float to decimal with rounding and strict error tolerances
# 
#     If the given number cannot be represented as decimal with an error
#     within 1/1000 of the last digit, :exc:`ValueError` is raised.
# 
#     :param number: a float that is nearly equal to a decimal number
#     :type number: float | Decimal
#     :param digits: number of decimal places of the resulting value (max. 9)
#     :type digits: int
# 
#     :raise: ValueError
# 
#     >>> float_to_decimal(1.424, 3)
#     Decimal('1.424')
#     >>> float_to_decimal(0.7, 1)
#     Decimal('0.7')
#     """
# 
#     # conversion is guaranteed to be accurate at 1e12 for 0 digits
#     # larger values are maybe not correctly represented in a float, so we are careful here
#     assert isinstance(digits, int)
#     assert 0 <= digits < 10, "invalid number of digits"
#     result = Decimal(int(round(number * (10 ** digits)))) / (10 ** digits)
#     if not abs(number) < 10 ** (10 + digits):
#         raise ValueError("cannot precisely convert such a large float to Decimal")
#     if not abs(float(result) - float(number)) < (10 ** -(digits + 3)):
#         raise ValueError("attempted inaccurate conversion from {0} to {1}".format(repr(number), repr(result)))
#     return result


import requests
import logging
import simplejson
import unittest
        import codecs
from FabLabKasse.shopping.backend.abstract import float_to_decimal
from PyQt4.QtCore import pyqtSignal, QObject
from decimal import Decimal
from tempfile import NamedTemporaryFile
from base64 import b64decode
            from ConfigParser import ConfigParser
        from os.path import dirname, abspath
            return False
        try:
            cart = self._decode_json_cart(req.text)
            self._reset_error_counter()
            return cart
        except InvalidCartJSONError, exception:
            logging.warning("Cannot decode Cart JSON: {0}".format(exception))
            raise

    def _decode_json_cart(self, json):
        """decode JSON data containing the cart

        :param json: JSON encoded data
        :type json: unicode
        :raise: InvalidCartJSONError
        """
        try:
            data = simplejson.loads(json)
        except simplejson.JSONDecodeError:
            raise InvalidCartJSONError("app-checkout: JSONDecodeError")
        logging.debug(u"received cart: {0}".format(repr(data)))
        try:
            if data["status"] != "PENDING":
                raise InvalidCartJSONError(property_name="status", value=data["status"])
            if unicode(data["cartCode"]) != self.cart_id:
                raise InvalidCartJSONError(property_name="cartCode", value=data["cartCode"])
            cart = []
            for entry in data["items"]:
                if not isinstance(entry, dict):
                    raise InvalidCartJSONError(property_name="items", value=data["items"])

# Path: FabLabKasse/shopping/backend/abstract.py
# def float_to_decimal(number, digits):
#     """
#     convert float to decimal with rounding and strict error tolerances
# 
#     If the given number cannot be represented as decimal with an error
#     within 1/1000 of the last digit, :exc:`ValueError` is raised.
# 
#     :param number: a float that is nearly equal to a decimal number
#     :type number: float | Decimal
#     :param digits: number of decimal places of the resulting value (max. 9)
#     :type digits: int
# 
#     :raise: ValueError
# 
#     >>> float_to_decimal(1.424, 3)
#     Decimal('1.424')
#     >>> float_to_decimal(0.7, 1)
#     Decimal('0.7')
#     """
# 
#     # conversion is guaranteed to be accurate at 1e12 for 0 digits
#     # larger values are maybe not correctly represented in a float, so we are careful here
#     assert isinstance(digits, int)
#     assert 0 <= digits < 10, "invalid number of digits"
#     result = Decimal(int(round(number * (10 ** digits)))) / (10 ** digits)
#     if not abs(number) < 10 ** (10 + digits):
#         raise ValueError("cannot precisely convert such a large float to Decimal")
#     if not abs(float(result) - float(number)) < (10 ** -(digits + 3)):
#         raise ValueError("attempted inaccurate conversion from {0} to {1}".format(repr(number), repr(result)))
#     return result


# Path: FabLabKasse/shopping/cart_from_app/cart_model.py
import requests
import logging
import simplejson
import unittest
        import codecs
from FabLabKasse.shopping.backend.abstract import float_to_decimal
from PyQt4.QtCore import pyqtSignal, QObject
from decimal import Decimal
from tempfile import NamedTemporaryFile
from base64 import b64decode
            from ConfigParser import ConfigParser
        from os.path import dirname, abspath

            return False
        try:
            cart = self._decode_json_cart(req.text)
            self._reset_error_counter()
            return cart
        except InvalidCartJSONError, exception:
            logging.warning("Cannot decode Cart JSON: {0}".format(exception))
            raise

    def _decode_json_cart(self, json):
        """decode JSON data containing the cart

        :param json: JSON encoded data
        :type json: unicode
        :raise: InvalidCartJSONError
        """
        try:
            data = simplejson.loads(json)
        except simplejson.JSONDecodeError:
            raise InvalidCartJSONError("app-checkout: JSONDecodeError")
        logging.debug(u"received cart: {0}".format(repr(data)))
        try:
            if data["status"] != "PENDING":
                raise InvalidCartJSONError(property_name="status", value=data["status"])
            if unicode(data["cartCode"]) != self.cart_id:
                raise InvalidCartJSONError(property_name="cartCode", value=data["cartCode"])
            cart = []
            for entry in data["items"]:
                if not isinstance(entry, dict):
                    raise InvalidCartJSONError(property_name="items", value=data["items"])

                item = (int(entry["productId"]), float_to_decimal(float(entry["amount"]), 3))