# Path: lib/Utility.py
# RE_COMPILED_TYPE = type(re.compile(''))
# POE_NINJA_API = 'http://api.poe.ninja/api/Data/'
#     CONFIG_FNAME = "cfg\\app.ini"
# class MsgType(IntEnum):
# class Messenger:
# class AppException(Exception):
# class CompileException(AppException):
# class AppConfiguration:
# class NoIndent(object):
# class NoIndentEncoder(json.JSONEncoder):
# class ConfidenceLevel(IntEnum):
#     def __init__(self):
#     def send_tmsg(self, msg, log_level=logging.NOTSET, tag=None):
#     def send_msg(self, msg, log_level=logging.NOTSET, timed=False, tag=None):
#     def send_stopped(self):
#     def send_update_id(self, id):
#     def send_object(self, obj):
#     def __init__(self):
#     def load(self):
#     def load_default(self):
#     def _load_settings(self, settings=None):
#     def update(self, cfg):
#     def save(self):
# def getBytesFromURL(url, handle=None, max_attempts=1, connect_timeout=None, timeout=None, gzip=True):
# def getJsonFromURL(url, handle=None, max_attempts=1, gzip=True):
# def getDataFromUrl(url, callback, max_attempts=1):
# def str2bool(val):
# def round_up(num):
# def round_down(num):
# def dround(num, ndigits=2):
# def tmsg(msg):
# def getBaseUrl(url):
# def isAbsoluteUrl(url):
# def logexception():
# def get_verror_msg(verror, data=None):
#     def __init__(self, value):
#     def __init__(self, *args, **kwargs):
#     def default(self, o):
#     def encode(self, o):
# def utc_to_local(utc_dt):
# def namify(text):
# def normalize_id(text):


import json
import pycurl
import re
import threading
from datetime import datetime, timedelta
from json import JSONEncoder
from urllib.parse import urljoin
from lib.Utility import getJsonFromURL, AppException, config, NoIndent, NoIndentEncoder, logexception, msgr, \
    utc_to_local, CompileException, ConfidenceLevel, POE_NINJA_API
        # print('updating currency..')

        try:
            shorts = self.shorts
            rates = {}

            def get_count(currency):
                return max(currency['receive']['count'] if currency['receive'] else 0,
                           currency['pay']['count'] if currency['pay'] else 0)

            for url in CurrencyManager.CURRENCY_API:
                data = getJsonFromURL(url.format(config.league))

                if data is None and not accept_empty:
                    raise AppException("Currency update failed. Empty response from server.")

                if data:
                    # shorts.update({currency['name']: currency['shorthands'] for currency in data["currencyDetails"]})
                    rates.update({currency['currencyTypeName']: float(currency['chaosEquivalent']) for currency in data["lines"] if get_count(currency) >= self.confidence_level})


            # cur_shorts = dict(self.shorts)
            # for name in cur_shorts:
            #     shorts[name] = list(set(cur_shorts[name] + shorts.get(name, [])))

            # can use update if we want to keep information from past updates, more robust if server returns less data
            # dict(self.rates).update(rates)
            self.compile(shorts, rates, last_update=datetime.utcnow() if rates else None)
        except pycurl.error as e:
            raise AppException("Currency update failed. Connection error: {}".format(e))

# Path: lib/Utility.py
# RE_COMPILED_TYPE = type(re.compile(''))
# POE_NINJA_API = 'http://api.poe.ninja/api/Data/'
#     CONFIG_FNAME = "cfg\\app.ini"
# class MsgType(IntEnum):
# class Messenger:
# class AppException(Exception):
# class CompileException(AppException):
# class AppConfiguration:
# class NoIndent(object):
# class NoIndentEncoder(json.JSONEncoder):
# class ConfidenceLevel(IntEnum):
#     def __init__(self):
#     def send_tmsg(self, msg, log_level=logging.NOTSET, tag=None):
#     def send_msg(self, msg, log_level=logging.NOTSET, timed=False, tag=None):
#     def send_stopped(self):
#     def send_update_id(self, id):
#     def send_object(self, obj):
#     def __init__(self):
#     def load(self):
#     def load_default(self):
#     def _load_settings(self, settings=None):
#     def update(self, cfg):
#     def save(self):
# def getBytesFromURL(url, handle=None, max_attempts=1, connect_timeout=None, timeout=None, gzip=True):
# def getJsonFromURL(url, handle=None, max_attempts=1, gzip=True):
# def getDataFromUrl(url, callback, max_attempts=1):
# def str2bool(val):
# def round_up(num):
# def round_down(num):
# def dround(num, ndigits=2):
# def tmsg(msg):
# def getBaseUrl(url):
# def isAbsoluteUrl(url):
# def logexception():
# def get_verror_msg(verror, data=None):
#     def __init__(self, value):
#     def __init__(self, *args, **kwargs):
#     def default(self, o):
#     def encode(self, o):
# def utc_to_local(utc_dt):
# def namify(text):
# def normalize_id(text):


# Path: lib/CurrencyManager.py
import json
import pycurl
import re
import threading
from datetime import datetime, timedelta
from json import JSONEncoder
from urllib.parse import urljoin
from lib.Utility import getJsonFromURL, AppException, config, NoIndent, NoIndentEncoder, logexception, msgr, \
    utc_to_local, CompileException, ConfidenceLevel, POE_NINJA_API

        # print('updating currency..')

        try:
            shorts = self.shorts
            rates = {}

            def get_count(currency):
                return max(currency['receive']['count'] if currency['receive'] else 0,
                           currency['pay']['count'] if currency['pay'] else 0)

            for url in CurrencyManager.CURRENCY_API:
                data = getJsonFromURL(url.format(config.league))

                if data is None and not accept_empty:
                    raise AppException("Currency update failed. Empty response from server.")

                if data:
                    # shorts.update({currency['name']: currency['shorthands'] for currency in data["currencyDetails"]})
                    rates.update({currency['currencyTypeName']: float(currency['chaosEquivalent']) for currency in data["lines"] if get_count(currency) >= self.confidence_level})


            # cur_shorts = dict(self.shorts)
            # for name in cur_shorts:
            #     shorts[name] = list(set(cur_shorts[name] + shorts.get(name, [])))

            # can use update if we want to keep information from past updates, more robust if server returns less data
            # dict(self.rates).update(rates)
            self.compile(shorts, rates, last_update=datetime.utcnow() if rates else None)
        except pycurl.error as e:
            raise AppException("Currency update failed. Connection error: {}".format(e))

        except AppException: