# Path: module/plugins/internal/misc.py
# class Expose(object):
#     """
#     Used for decoration to declare rpc services
#     """
#     def __new__(cls, fn, *args, **kwargs):
#         hookManager.addRPC(fn.__module__, fn.__name__, fn.__doc__)
#         return fn
# 
# def encode(value, encoding='utf-8', errors='backslashreplace'):
#     """
#     Unicode string -> encoded string (default to UTF-8)
#     """
#     if isinstance(value, unicode):
#         res = value.encode(encoding, errors)
# 
#     elif isinstance(value, str):
#         decoding = get_console_encoding(sys.stdin.encoding)
#         if encoding == decoding:
#             res = value
#         else:
#             res = transcode(value, decoding, encoding)
# 
#     else:
#         res = str(value)
# 
#     return res
# 
# def exists(path):
#     path = encode(path)
# 
#     if os.path.exists(path):
#         if os.name == "nt":
#             dir, name = os.path.split(path.rstrip(os.sep))
#             return name.upper() in map(str.upper, os.listdir(dir))
#         else:
#             return True
#     else:
#         return False
# 
# def fsjoin(*args):
#     """
#     Like os.path.join, but encoding aware
#     (for safe-joining see `safejoin`)
#     """
#     return encode(os.path.join(*args))
# 
# def threaded(fn):
#     def run(*args, **kwargs):
#         hookManager.startThread(fn, *args, **kwargs)
# 
#     return run


import operator
import os
import re
import sys
import time
from ..internal.Addon import Addon
from ..internal.misc import Expose, encode, exists, fsjoin, threaded
    __name__ = "UpdateManager"
    __type__ = "hook"
    __version__ = "1.17"
    __status__ = "testing"

    __config__ = [("activated", "bool", "Activated", True),
                  ("checkinterval", "int", "Check interval in hours", 6),
                  ("autorestart", "bool", "Auto-restart pyLoad when required", True),
                  ("checkonstart", "bool", "Check for updates on startup", True),
                  ("checkperiod", "bool", "Check for updates periodically", True),
                  ("reloadplugins", "bool",
                   "Monitor plugin code changes in debug mode", True),
                  ("nodebugupdate", "bool", "Don't update plugins in debug mode", False)]

    __description__ = """Check for updates"""
    __license__ = "GPLv3"
    __authors__ = [("Walter Purcaro", "vuolter@gmail.com")]

    _VERSION = re.compile(r'^\s*__version__\s*=\s*("|\')([\d.]+)\1', re.M)

    # SERVER_URL     = "http://updatemanager.pyload.org"
    SERVER_URL = "http://updatemanager-spyload.rhcloud.com"
    CHECK_INTERVAL = 3 * 60 * 60  #: 3 hours

    def activate(self):
        if self.checkonstart:
            self.pyload.api.pauseServer()
            self.update()

            if self.do_restart is False:

# Path: module/plugins/internal/misc.py
# class Expose(object):
#     """
#     Used for decoration to declare rpc services
#     """
#     def __new__(cls, fn, *args, **kwargs):
#         hookManager.addRPC(fn.__module__, fn.__name__, fn.__doc__)
#         return fn
# 
# def encode(value, encoding='utf-8', errors='backslashreplace'):
#     """
#     Unicode string -> encoded string (default to UTF-8)
#     """
#     if isinstance(value, unicode):
#         res = value.encode(encoding, errors)
# 
#     elif isinstance(value, str):
#         decoding = get_console_encoding(sys.stdin.encoding)
#         if encoding == decoding:
#             res = value
#         else:
#             res = transcode(value, decoding, encoding)
# 
#     else:
#         res = str(value)
# 
#     return res
# 
# def exists(path):
#     path = encode(path)
# 
#     if os.path.exists(path):
#         if os.name == "nt":
#             dir, name = os.path.split(path.rstrip(os.sep))
#             return name.upper() in map(str.upper, os.listdir(dir))
#         else:
#             return True
#     else:
#         return False
# 
# def fsjoin(*args):
#     """
#     Like os.path.join, but encoding aware
#     (for safe-joining see `safejoin`)
#     """
#     return encode(os.path.join(*args))
# 
# def threaded(fn):
#     def run(*args, **kwargs):
#         hookManager.startThread(fn, *args, **kwargs)
# 
#     return run


# Path: module/plugins/hooks/UpdateManager.py
import operator
import os
import re
import sys
import time
from ..internal.Addon import Addon
from ..internal.misc import Expose, encode, exists, fsjoin, threaded

    __name__ = "UpdateManager"
    __type__ = "hook"
    __version__ = "1.17"
    __status__ = "testing"

    __config__ = [("activated", "bool", "Activated", True),
                  ("checkinterval", "int", "Check interval in hours", 6),
                  ("autorestart", "bool", "Auto-restart pyLoad when required", True),
                  ("checkonstart", "bool", "Check for updates on startup", True),
                  ("checkperiod", "bool", "Check for updates periodically", True),
                  ("reloadplugins", "bool",
                   "Monitor plugin code changes in debug mode", True),
                  ("nodebugupdate", "bool", "Don't update plugins in debug mode", False)]

    __description__ = """Check for updates"""
    __license__ = "GPLv3"
    __authors__ = [("Walter Purcaro", "vuolter@gmail.com")]

    _VERSION = re.compile(r'^\s*__version__\s*=\s*("|\')([\d.]+)\1', re.M)

    # SERVER_URL     = "http://updatemanager.pyload.org"
    SERVER_URL = "http://updatemanager-spyload.rhcloud.com"
    CHECK_INTERVAL = 3 * 60 * 60  #: 3 hours

    def activate(self):
        if self.checkonstart:
            self.pyload.api.pauseServer()
            self.update()

            if self.do_restart is False:

                self.pyload.api.unpauseServer()