# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: constants/dataTypes.py

# 
# Path: helpers/binaryHelper.py
# def uleb128Encode(num):
# def packData(__data, __dataType):
# def binaryWrite(structure = None):
# 
# Path: helpers/s3.py
# def getWriteReplayBucketName():
# def getReadReplayBucketName(scoreID):
# def clientFactory(*, region, endpoint, accessKeyId, secretAccessKey):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


import io
import os
from botocore.exceptions import ClientError
from timeout_decorator import timeout
from common import generalUtils
from common.log import logUtils as log
from common.sentry import sentry
from constants import exceptions
from constants import dataTypes
from helpers import binaryHelper
from helpers import s3
from objects import glob
def _getFirstReplayFileName(scoreID):
	"""
	Iterates over all REPLAYS_FOLDERS in config, and returns the
	path of the replay. It starts from the first folder, if the replay
	is not there, it tries with the second folder and so on.
	Returns None if there's no such file in any of the folders.

	:param scoreID:
	:return: path or None
	"""
	for folder in glob.conf["REPLAYS_FOLDERS"]:
		fileName = "{}/replay_{}.osr".format(folder, scoreID)
		if os.path.isfile(fileName):
			return fileName
	return None


def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
	if all(v is None for v in (scoreID, scoreData)) or all(v is not None for v in (scoreID, scoreData)):
		raise AttributeError("Either scoreID or scoreData must be provided, not neither or both")

	if scoreData is None:
		scoreData = glob.db.fetch(
			"SELECT scores.*, users.username FROM scores LEFT JOIN users ON scores.userid = users.id "
			"WHERE scores.id = %s",
			[scoreID]
		)
	else:
		scoreID = scoreData["id"]
	if scoreData is None or scoreID is None:

# Path: constants/exceptions.py
# class invalidArgumentsException(Exception):
# class loginFailedException(Exception):
# class userBannedException(Exception):
# class userLockedException(Exception):
# class noBanchoSessionException(Exception):
# class osuApiFailException(Exception):
# class fileNotFoundException(Exception):
# class invalidBeatmapException(Exception):
# class unsupportedGameModeException(Exception):
# class beatmapTooLongException(Exception):
# class need2FAException(Exception):
# class noAPIDataError(Exception):
# class scoreNotFoundError(Exception):
# class ppCalcException(Exception):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, handler):
# 	def __init__(self, handler, f):
# 	def __init__(self, handler):
# 	def __init__(self, handler, who, ip):
# 	def __init__(self, exception):
# 
# Path: constants/dataTypes.py

# 
# Path: helpers/binaryHelper.py
# def uleb128Encode(num):
# def packData(__data, __dataType):
# def binaryWrite(structure = None):
# 
# Path: helpers/s3.py
# def getWriteReplayBucketName():
# def getReadReplayBucketName(scoreID):
# def clientFactory(*, region, endpoint, accessKeyId, secretAccessKey):
# 
# Path: objects/glob.py
# 		VERSION = f.read().strip()
# 	VERSION = "Unknown"
# ACHIEVEMENTS_VERSION = 1
# DATADOG_PREFIX = "lets"


# Path: helpers/replayHelper.py
import io
import os
from botocore.exceptions import ClientError
from timeout_decorator import timeout
from common import generalUtils
from common.log import logUtils as log
from common.sentry import sentry
from constants import exceptions
from constants import dataTypes
from helpers import binaryHelper
from helpers import s3
from objects import glob

def _getFirstReplayFileName(scoreID):
	"""
	Iterates over all REPLAYS_FOLDERS in config, and returns the
	path of the replay. It starts from the first folder, if the replay
	is not there, it tries with the second folder and so on.
	Returns None if there's no such file in any of the folders.

	:param scoreID:
	:return: path or None
	"""
	for folder in glob.conf["REPLAYS_FOLDERS"]:
		fileName = "{}/replay_{}.osr".format(folder, scoreID)
		if os.path.isfile(fileName):
			return fileName
	return None


def buildFullReplay(scoreID=None, scoreData=None, rawReplay=None):
	if all(v is None for v in (scoreID, scoreData)) or all(v is not None for v in (scoreID, scoreData)):
		raise AttributeError("Either scoreID or scoreData must be provided, not neither or both")

	if scoreData is None:
		scoreData = glob.db.fetch(
			"SELECT scores.*, users.username FROM scores LEFT JOIN users ON scores.userid = users.id "
			"WHERE scores.id = %s",
			[scoreID]
		)
	else:
		scoreID = scoreData["id"]
	if scoreData is None or scoreID is None:

		raise exceptions.scoreNotFoundError()