# Path: guicavane/Config.py
# class Config(Singleton):
#     """ Configuration manager. Uses json as config format. """
# 
#     def __init__(self):
#         Singleton.__init__(self)
#         self.config_file = CONFIG_FILE
# 
#         self.data = {}
# 
#         if os.path.exists(self.config_file):
#             with open(self.config_file) as filehandler:
#                 data = filehandler.read()
# 
#                 try:
#                     self.data = json.loads(data)
#                 except ValueError:
#                     self.data = DEFAULT_VALUES
#         else:
#             self.data = DEFAULT_VALUES
# 
#         self.save()
# 
#     def get_key(self, key):
#         """ Returns the value of the given key or the default
#         value if it isn't set on the config or None
#         if the key doesn't exists. """
# 
#         return self.data.get(key, get_default(key))
# 
#     def set_key(self, key, value):
#         """ Sets the value of the key and writes the config file. """
# 
#         self.data[key] = value
#         self.save()
# 
#     def append_key(self, key, value):
#         """ If the key is a list, appends the value to the list. """
# 
#         assert type(self.data[key]) == list
# 
#         if not key in self.data:
#             self.data[key] = get_default(key)
# 
#         assert type(self.data[key]) == list
# 
#         self.data[key].append(value)
# 
#         self.save()
# 
#     def remove_key(self, key, value):
#         """ If the key is a list, removes the value from it. """
# 
#         assert type(self.data[key]) == list
#         assert value in self.data[key]
# 
#         self.data[key].remove(value)
#         self.save()
# 
#     def save(self):
#         """ Saves the config on the disk file and fills
#         the empty values with default ones. """
# 
#         for key in DEFAULT_VALUES:
#             if key not in self.data:
#                 self.data[key] = get_default(key)
# 
#         with open(self.config_file, "w") as filehandler:
#             json_data = json.dumps(self.data, sort_keys=True, indent=4)
#             filehandler.write(json_data + "\n")
# 
# Path: guicavane/Paths.py
# CACHE_DIR = TEMP_DIR
# 
# COOKIES_FILE = os.path.join(CONFIG_DIR, "cookies.jar")
# 
# Path: guicavane/Utils/Log.py
# def console(name=None, logger=None, format=None):
#     handler = logging.StreamHandler()
# 
#     if "-v" in sys.argv:
#         level = logging.DEBUG
#     else:
#         level = logging.WARN
# 
#     return _get_logger(name, logger, handler, level, format)
# 
# Path: guicavane/Constants.py
# DEFAULT_REQUEST_TIMEOUT = 15  # None for use python's default
# 
# CUSTOM_DNS = {
#     "www.cuevana.tv": "184.107.187.234",
# }


import os
import time
import urllib
import socket
import httplib
import urllib2
import cookielib
import functools
from StringIO import StringIO
from Cached import Cached
from guicavane.Config import Config
from guicavane.Paths import CACHE_DIR, COOKIES_FILE
from guicavane.Utils.Log import console
from guicavane.Constants import DEFAULT_REQUEST_TIMEOUT, CUSTOM_DNS
                urllib.urlcleanup()
                tried += 1
                time.sleep(1)


        error = "Can't download. Error: '%s'" % error
        long_error = error + ", args: '%s'" % (str(args) + str(kwargs))
        log.error(long_error)

        raise DownloadError(error)

    return deco

def save_cookies(callback):
    """ Save cookies in each request """

    @functools.wraps(callback)
    def deco(self, *args, **kwargs):
        data = callback(self, *args, **kwargs)
        self.cookiejar.save()
        return data

    return deco



def CustomResolver(host):
    config = Config.get()
    if config.get_key("use_custom_resolve"):
        if host in CUSTOM_DNS:

# Path: guicavane/Config.py
# class Config(Singleton):
#     """ Configuration manager. Uses json as config format. """
# 
#     def __init__(self):
#         Singleton.__init__(self)
#         self.config_file = CONFIG_FILE
# 
#         self.data = {}
# 
#         if os.path.exists(self.config_file):
#             with open(self.config_file) as filehandler:
#                 data = filehandler.read()
# 
#                 try:
#                     self.data = json.loads(data)
#                 except ValueError:
#                     self.data = DEFAULT_VALUES
#         else:
#             self.data = DEFAULT_VALUES
# 
#         self.save()
# 
#     def get_key(self, key):
#         """ Returns the value of the given key or the default
#         value if it isn't set on the config or None
#         if the key doesn't exists. """
# 
#         return self.data.get(key, get_default(key))
# 
#     def set_key(self, key, value):
#         """ Sets the value of the key and writes the config file. """
# 
#         self.data[key] = value
#         self.save()
# 
#     def append_key(self, key, value):
#         """ If the key is a list, appends the value to the list. """
# 
#         assert type(self.data[key]) == list
# 
#         if not key in self.data:
#             self.data[key] = get_default(key)
# 
#         assert type(self.data[key]) == list
# 
#         self.data[key].append(value)
# 
#         self.save()
# 
#     def remove_key(self, key, value):
#         """ If the key is a list, removes the value from it. """
# 
#         assert type(self.data[key]) == list
#         assert value in self.data[key]
# 
#         self.data[key].remove(value)
#         self.save()
# 
#     def save(self):
#         """ Saves the config on the disk file and fills
#         the empty values with default ones. """
# 
#         for key in DEFAULT_VALUES:
#             if key not in self.data:
#                 self.data[key] = get_default(key)
# 
#         with open(self.config_file, "w") as filehandler:
#             json_data = json.dumps(self.data, sort_keys=True, indent=4)
#             filehandler.write(json_data + "\n")
# 
# Path: guicavane/Paths.py
# CACHE_DIR = TEMP_DIR
# 
# COOKIES_FILE = os.path.join(CONFIG_DIR, "cookies.jar")
# 
# Path: guicavane/Utils/Log.py
# def console(name=None, logger=None, format=None):
#     handler = logging.StreamHandler()
# 
#     if "-v" in sys.argv:
#         level = logging.DEBUG
#     else:
#         level = logging.WARN
# 
#     return _get_logger(name, logger, handler, level, format)
# 
# Path: guicavane/Constants.py
# DEFAULT_REQUEST_TIMEOUT = 15  # None for use python's default
# 
# CUSTOM_DNS = {
#     "www.cuevana.tv": "184.107.187.234",
# }


# Path: guicavane/Utils/UrlOpen.py
import os
import time
import urllib
import socket
import httplib
import urllib2
import cookielib
import functools
from StringIO import StringIO
from Cached import Cached
from guicavane.Config import Config
from guicavane.Paths import CACHE_DIR, COOKIES_FILE
from guicavane.Utils.Log import console
from guicavane.Constants import DEFAULT_REQUEST_TIMEOUT, CUSTOM_DNS

                urllib.urlcleanup()
                tried += 1
                time.sleep(1)


        error = "Can't download. Error: '%s'" % error
        long_error = error + ", args: '%s'" % (str(args) + str(kwargs))
        log.error(long_error)

        raise DownloadError(error)

    return deco

def save_cookies(callback):
    """ Save cookies in each request """

    @functools.wraps(callback)
    def deco(self, *args, **kwargs):
        data = callback(self, *args, **kwargs)
        self.cookiejar.save()
        return data

    return deco



def CustomResolver(host):
    config = Config.get()
    if config.get_key("use_custom_resolve"):
        if host in CUSTOM_DNS:

            host = CUSTOM_DNS[host]