# Path: celestia/gui/threaded.py
# class ThreadedVersionDownload(ThreadedBase):
#     def __init__(self, progress_queue, download_url, filename, cancel_queue):
#         ThreadedBase.__init__(self, progress_queue)
#         self.download_url = download_url
#         self.filename = filename
#         self.cancel_queue = cancel_queue
# 
#     def worker(self, queue):
#         ThreadedBase.worker(self, queue)
#         res = {
#             "error": None,
#             "total": 0,
#             "current": 0,
#             "done": False
#         }
#         partfile = "{}.part".format(self.filename)
#         try:
#             response = requests.get(self.download_url, stream=True)
#             response.raise_for_status()
#             res["total"] = int(response.headers.get('content-length') or 0)
#             queue.put(res)
#             with open(partfile, 'wb') as tmp:
#                 if not res["total"]:
#                     tmp.write(response.content)
#                 else:
#                     for chunk in response.iter_content(1024):
#                         try:
#                             cancel = self.cancel_queue.get_nowait()
#                             raise RuntimeError("The download was canceled.")
#                         except Queue.Empty:
#                             pass
#                         res["current"] += len(chunk)
#                         tmp.write(chunk)
#                         queue.put(res)
#             shutil.copy(partfile, self.filename)
#         except Exception as e:
#             res["error"] = e
#         finally:
#             os.unlink(partfile)
#         res["done"] = True
#         queue.put(res)
# 
# Path: celestia/gui/threaded.py
# def process_queue(instance, queue, loadingbox=None,
#                   success_callback=None, error_callback=None,
#                   process_callback=None, show_error=True):
#     empty = False
#     try:
#         res = queue.get_nowait()
#     except Queue.Empty:
#         res = {"done": False}
#         empty = True
#     if not res["done"]:
#         if process_callback and not empty:
#             process_callback(res)
#         instance.after(100, process_queue,
#                        instance, queue, loadingbox,
#                        success_callback, error_callback,
#                        process_callback, show_error)
#     else:
#         if loadingbox is not None:
#             loadingbox.destroy()
#         if res["error"]:
#             if show_error:
#                 showerror("Error", str(res["error"]))
#             if error_callback:
#                 error_callback()
#         elif success_callback:
#             success_callback(res)
# 
# Path: celestia/utility/tkvardescriptor.py
# class TkVarDescriptor(object):
#     def __init__(self, klass):
#         self.label = None
#         self.klass = klass
# 
#     def __get__(self, instance, objtype=None):
#         if instance is None:
#             return self
#         return instance.__dict__[self.label].get()
# 
#     def __set__(self, instance, value):
#         if self.label not in instance.__dict__:
#             instance.__dict__[self.label] = self.klass(instance)
#         instance.__dict__[self.label].set(value)
# 
#     def raw_klass(self, instance):
#         return instance.__dict__[self.label]
# 
# class TkVarDescriptorOwner(type):
#     def __new__(cls, name, bases, attrs):
#         for key, value in attrs.items():
#             if isinstance(value, TkVarDescriptor):
#                 value.label = key
#         return super(TkVarDescriptorOwner, cls).__new__(cls, name, bases, attrs)
# 
# Path: celestia/utility/config.py
# class Config(object):
#     _config = None
# 
#     @ClassProperty
#     @classmethod
#     def config(cls):
#         if cls._config is None:
#             config = resources.user.read(CONFIG_FILE)
#             if config is None:
#                 config = BASE_CONFIG
#             else:
#                 config = json.loads(config)
#             cls._config = config
#             cls.commit()
#         return cls._config
# 
#     @classmethod
#     def commit(cls):
#         resources.user.write(CONFIG_FILE, json.dumps(cls.config))


import six
    import tkinter as tk
    import tkinter.ttk as ttk
    import Tkinter as tk
    import ttk
    from tkinter.constants import N, S, E, W, NSEW, HORIZONTAL
    from tkinter.constants import BOTH, X, Y
    from tkinter.constants import TOP, BOTTOM, RIGHT, LEFT
    from tkinter.messagebox import askyesno, showinfo
    from tkinter.filedialog import asksaveasfilename
    from tkinter.font import Font
    from queue import Queue
    from Tkconstants import N, S, E, W, NSEW, HORIZONTAL
    from Tkconstants import BOTH, X, Y
    from Tkconstants import TOP, BOTTOM, RIGHT, LEFT
    from tkMessageBox import askyesno, showinfo
    from tkFileDialog import asksaveasfilename
    from tkFont import Font
    from Queue import Queue
    from celestia.utility.monotonic import monotonic
    from time import time as monotonic
from .threaded import ThreadedVersionDownload
from .threaded import process_queue
from celestia.utility.tkvardescriptor import TkVarDescriptor, TkVarDescriptorOwner
from celestia.utility.config import Config
    @total.setter
    def total(self, value):
        self._maxbytes = int(value)
        self.pb["maximum"] = self._maxbytes
        self.update_percent()

    @property
    def current(self):
        return self._curbytes

    @current.setter
    def current(self, value):
        self._curbytes = int(value)
        self.pb["value"] = self._curbytes
        self.update_percent()


class NewVersionDialog(object):
    def __init__(self, download_url, master, *args, **kwargs):
        self.download_url = download_url
        self.master = master
        if askyesno("New version!", "A new version is available!\nDo you wish to download it?"):
            self.download()

    def download(self):
        filename = asksaveasfilename(initialfile="CelestiaSunrise.exe")
        if filename:
            downloadbox = DownloadDialog(self.master)
            progress_queue = Queue()
            cancel_queue = Queue()

# Path: celestia/gui/threaded.py
# class ThreadedVersionDownload(ThreadedBase):
#     def __init__(self, progress_queue, download_url, filename, cancel_queue):
#         ThreadedBase.__init__(self, progress_queue)
#         self.download_url = download_url
#         self.filename = filename
#         self.cancel_queue = cancel_queue
# 
#     def worker(self, queue):
#         ThreadedBase.worker(self, queue)
#         res = {
#             "error": None,
#             "total": 0,
#             "current": 0,
#             "done": False
#         }
#         partfile = "{}.part".format(self.filename)
#         try:
#             response = requests.get(self.download_url, stream=True)
#             response.raise_for_status()
#             res["total"] = int(response.headers.get('content-length') or 0)
#             queue.put(res)
#             with open(partfile, 'wb') as tmp:
#                 if not res["total"]:
#                     tmp.write(response.content)
#                 else:
#                     for chunk in response.iter_content(1024):
#                         try:
#                             cancel = self.cancel_queue.get_nowait()
#                             raise RuntimeError("The download was canceled.")
#                         except Queue.Empty:
#                             pass
#                         res["current"] += len(chunk)
#                         tmp.write(chunk)
#                         queue.put(res)
#             shutil.copy(partfile, self.filename)
#         except Exception as e:
#             res["error"] = e
#         finally:
#             os.unlink(partfile)
#         res["done"] = True
#         queue.put(res)
# 
# Path: celestia/gui/threaded.py
# def process_queue(instance, queue, loadingbox=None,
#                   success_callback=None, error_callback=None,
#                   process_callback=None, show_error=True):
#     empty = False
#     try:
#         res = queue.get_nowait()
#     except Queue.Empty:
#         res = {"done": False}
#         empty = True
#     if not res["done"]:
#         if process_callback and not empty:
#             process_callback(res)
#         instance.after(100, process_queue,
#                        instance, queue, loadingbox,
#                        success_callback, error_callback,
#                        process_callback, show_error)
#     else:
#         if loadingbox is not None:
#             loadingbox.destroy()
#         if res["error"]:
#             if show_error:
#                 showerror("Error", str(res["error"]))
#             if error_callback:
#                 error_callback()
#         elif success_callback:
#             success_callback(res)
# 
# Path: celestia/utility/tkvardescriptor.py
# class TkVarDescriptor(object):
#     def __init__(self, klass):
#         self.label = None
#         self.klass = klass
# 
#     def __get__(self, instance, objtype=None):
#         if instance is None:
#             return self
#         return instance.__dict__[self.label].get()
# 
#     def __set__(self, instance, value):
#         if self.label not in instance.__dict__:
#             instance.__dict__[self.label] = self.klass(instance)
#         instance.__dict__[self.label].set(value)
# 
#     def raw_klass(self, instance):
#         return instance.__dict__[self.label]
# 
# class TkVarDescriptorOwner(type):
#     def __new__(cls, name, bases, attrs):
#         for key, value in attrs.items():
#             if isinstance(value, TkVarDescriptor):
#                 value.label = key
#         return super(TkVarDescriptorOwner, cls).__new__(cls, name, bases, attrs)
# 
# Path: celestia/utility/config.py
# class Config(object):
#     _config = None
# 
#     @ClassProperty
#     @classmethod
#     def config(cls):
#         if cls._config is None:
#             config = resources.user.read(CONFIG_FILE)
#             if config is None:
#                 config = BASE_CONFIG
#             else:
#                 config = json.loads(config)
#             cls._config = config
#             cls.commit()
#         return cls._config
# 
#     @classmethod
#     def commit(cls):
#         resources.user.write(CONFIG_FILE, json.dumps(cls.config))


# Path: celestia/gui/dialogs.py
import six
    import tkinter as tk
    import tkinter.ttk as ttk
    import Tkinter as tk
    import ttk
    from tkinter.constants import N, S, E, W, NSEW, HORIZONTAL
    from tkinter.constants import BOTH, X, Y
    from tkinter.constants import TOP, BOTTOM, RIGHT, LEFT
    from tkinter.messagebox import askyesno, showinfo
    from tkinter.filedialog import asksaveasfilename
    from tkinter.font import Font
    from queue import Queue
    from Tkconstants import N, S, E, W, NSEW, HORIZONTAL
    from Tkconstants import BOTH, X, Y
    from Tkconstants import TOP, BOTTOM, RIGHT, LEFT
    from tkMessageBox import askyesno, showinfo
    from tkFileDialog import asksaveasfilename
    from tkFont import Font
    from Queue import Queue
    from celestia.utility.monotonic import monotonic
    from time import time as monotonic
from .threaded import ThreadedVersionDownload
from .threaded import process_queue
from celestia.utility.tkvardescriptor import TkVarDescriptor, TkVarDescriptorOwner
from celestia.utility.config import Config

    @total.setter
    def total(self, value):
        self._maxbytes = int(value)
        self.pb["maximum"] = self._maxbytes
        self.update_percent()

    @property
    def current(self):
        return self._curbytes

    @current.setter
    def current(self, value):
        self._curbytes = int(value)
        self.pb["value"] = self._curbytes
        self.update_percent()


class NewVersionDialog(object):
    def __init__(self, download_url, master, *args, **kwargs):
        self.download_url = download_url
        self.master = master
        if askyesno("New version!", "A new version is available!\nDo you wish to download it?"):
            self.download()

    def download(self):
        filename = asksaveasfilename(initialfile="CelestiaSunrise.exe")
        if filename:
            downloadbox = DownloadDialog(self.master)
            progress_queue = Queue()
            cancel_queue = Queue()

            thread = ThreadedVersionDownload(progress_queue, self.download_url,