# Path: ggpo/common/util.py
# def logdebug():
#     global _loggerInitialzed
#     if not _loggerInitialzed:
#         _loggerInitialzed = True
#         loggerInit()
#     return logging.getLogger('GGPODebug')
# 
# def findUnsupportedGamesavesDir():
#     d = Settings.value(Settings.UNSUPPORTED_GAMESAVES_DIR)
#     if d and os.path.isdir(d):
#         return d
#     d = os.path.abspath(os.path.join(expanduser("~"), "ggpoUnsupportedGamesavestates"))
#     if d and os.path.isdir(d):
#         return d
#     # noinspection PyBroadException
#     try:
#         os.makedirs(d)
#         return d
#     except:
#         pass
# 
# def sha256digest(fname):
#     return hashlib.sha256(open(fname, 'rb').read()).hexdigest()


import glob
import hashlib
import json
import os
import re
import urllib
import urllib2
import time
from PyQt4 import QtCore
from ggpo.common.util import logdebug, findUnsupportedGamesavesDir, sha256digest
class SyncWorker(QtCore.QObject):
    sigFinished = QtCore.pyqtSignal(int, int, int)
    sigStatusMessage = QtCore.pyqtSignal(str)
    SAVESTATES_GITHUB_BASE_URL = 'https://raw.github.com/afurlani/ggpostates/master/'
    JSON_INDEX_FILENAME = 'index.json'
    SAVESTATES_INDEX_URL = SAVESTATES_GITHUB_BASE_URL + JSON_INDEX_FILENAME

    def __init__(self, checkonly=False):
        QtCore.QObject.__init__(self)
        self.checkonly = checkonly
        self.added = 0
        self.updated = 0
        self.nochange = 0

    def download(self):
        d = findUnsupportedGamesavesDir()
        if not d:
            self.sigStatusMessage.emit('Unsupported Savestates Directory is not set')
            self.sigFinished.emit(self.added, self.updated, self.nochange)
            return
        localJsonDigest = readLocalJsonDigest()
        # noinspection PyBroadException
        try:
            # gotta love CPython's GIL, yield thread
            time.sleep(0.05)
            response = urllib2.urlopen(self.SAVESTATES_INDEX_URL, timeout=3)
            games = json.load(response)

            for filename, shahash in games.items():
                if re.search(r'[^ .a-zA-Z0-9_-]', filename):

# Path: ggpo/common/util.py
# def logdebug():
#     global _loggerInitialzed
#     if not _loggerInitialzed:
#         _loggerInitialzed = True
#         loggerInit()
#     return logging.getLogger('GGPODebug')
# 
# def findUnsupportedGamesavesDir():
#     d = Settings.value(Settings.UNSUPPORTED_GAMESAVES_DIR)
#     if d and os.path.isdir(d):
#         return d
#     d = os.path.abspath(os.path.join(expanduser("~"), "ggpoUnsupportedGamesavestates"))
#     if d and os.path.isdir(d):
#         return d
#     # noinspection PyBroadException
#     try:
#         os.makedirs(d)
#         return d
#     except:
#         pass
# 
# def sha256digest(fname):
#     return hashlib.sha256(open(fname, 'rb').read()).hexdigest()


# Path: ggpo/common/unsupportedsavestates.py
import glob
import hashlib
import json
import os
import re
import urllib
import urllib2
import time
from PyQt4 import QtCore
from ggpo.common.util import logdebug, findUnsupportedGamesavesDir, sha256digest

class SyncWorker(QtCore.QObject):
    sigFinished = QtCore.pyqtSignal(int, int, int)
    sigStatusMessage = QtCore.pyqtSignal(str)
    SAVESTATES_GITHUB_BASE_URL = 'https://raw.github.com/afurlani/ggpostates/master/'
    JSON_INDEX_FILENAME = 'index.json'
    SAVESTATES_INDEX_URL = SAVESTATES_GITHUB_BASE_URL + JSON_INDEX_FILENAME

    def __init__(self, checkonly=False):
        QtCore.QObject.__init__(self)
        self.checkonly = checkonly
        self.added = 0
        self.updated = 0
        self.nochange = 0

    def download(self):
        d = findUnsupportedGamesavesDir()
        if not d:
            self.sigStatusMessage.emit('Unsupported Savestates Directory is not set')
            self.sigFinished.emit(self.added, self.updated, self.nochange)
            return
        localJsonDigest = readLocalJsonDigest()
        # noinspection PyBroadException
        try:
            # gotta love CPython's GIL, yield thread
            time.sleep(0.05)
            response = urllib2.urlopen(self.SAVESTATES_INDEX_URL, timeout=3)
            games = json.load(response)

            for filename, shahash in games.items():
                if re.search(r'[^ .a-zA-Z0-9_-]', filename):

                    logdebug().error("Filename {} looks suspicious, ignoring".format(filename))