# Module to manage the loading of models and submodels in the Sapiens standard.
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
class SapiensLoader:
	def __init__(self, model_path='', show_errors=True, display_error_point=False):
		try:
			self.__show_errors = bool(show_errors) if type(show_errors) in (bool, int, float) else True
			self.__display_error_point = bool(display_error_point) if type(display_error_point) in (bool, int, float) else False
			if not isinstance(model_path, str) and self.__show_errors: raise TypeError('The value must be a string.')
			model_path = str(model_path).strip()
			self.__value = model_path
			try:
				from warnings import filterwarnings
				from logging import getLogger, ERROR, disable, CRITICAL
				from os import environ
				from dotenv import load_dotenv
				filterwarnings('ignore')
				filterwarnings('ignore', category=UserWarning, module='torch.distributed')
				getLogger('torch.distributed.elastic.multiprocessing.redirects').setLevel(ERROR)
				environ['DISABLE_MODEL_SOURCE_CHECK'] = 'True'
				load_dotenv()
				disable(CRITICAL)
			except: pass
			from traceback import print_exc
			self.__print_exc = print_exc
		except Exception as error:
			try:
				if self.__show_errors:
					error_message = 'ERROR in SapiensLoader.__init__: '+str(error)
					print(error_message)
					try: self.__print_exc() if self.__display_error_point else None
					except: pass
			except: pass
	def __str__(self): return self.__value
	def __repr__(self): return f'SapiensLoader({self.__value!r})'
class DataLoader:
	def __init__(self, sapiens_loader=None, show_errors=True, display_error_point=False):
		try:
			self.__show_errors = bool(show_errors) if type(show_errors) in (bool, int, float) else True
			self.__display_error_point = bool(display_error_point) if type(display_error_point) in (bool, int, float) else False
			if not isinstance(sapiens_loader, SapiensLoader) and self.__show_errors: raise TypeError('The value must be a SapiensLoader.')
			try:
				from warnings import filterwarnings
				from logging import getLogger, ERROR, disable, CRITICAL
				from os import environ
				from dotenv import load_dotenv
				filterwarnings('ignore')
				filterwarnings('ignore', category=UserWarning, module='torch.distributed')
				getLogger('torch.distributed.elastic.multiprocessing.redirects').setLevel(ERROR)
				environ['DISABLE_MODEL_SOURCE_CHECK'] = 'True'
				load_dotenv()
				disable(CRITICAL)
			except: pass
			from traceback import print_exc
			self.__print_exc = print_exc
			self.input = str(sapiens_loader).strip()
			self.output = self.input
		except Exception as error:
			try:
				if self.__show_errors:
					error_message = 'ERROR in DataLoader.__init__: '+str(error)
					print(error_message)
					try: self.__print_exc() if self.__display_error_point else None
					except: pass
			except: pass
	def open(self, progress=True):
		try:
			progress = bool(progress) if type(progress) in (bool, int, float) else True
			from SUBMODELS import SUBMODELS
			self.output = SUBMODELS(show_errors=self.__show_errors, display_error_point=self.__display_error_point).loadModel(model_path=self.input, progress=progress)
			self.output = str(self.output).strip()
			return True if self.output else False
		except Exception as error:
			try:
				if self.__show_errors:
					error_message = 'ERROR in DataLoader.open: '+str(error)
					print(error_message)
					try: self.__print_exc() if self.__display_error_point else None
					except: pass
			except: pass
			return False		
	def close(self):
		try:
			def _delete_path(path=''):
				path = str(path).strip()
				from os.path import isfile, isdir, exists
				from os import remove
				from shutil import rmtree
				from pathlib import Path
				if isfile(path): remove(path)
				elif isdir(path): rmtree(path)
				else: rmtree(Path(path).parent)
				return not exists(path)
			return _delete_path(path=self.output) if self.output and (self.output != self.input) else True
		except Exception as error:
			try:
				if self.__show_errors:
					error_message = 'ERROR in DataLoader.close: '+str(error)
					print(error_message)
					try: self.__print_exc() if self.__display_error_point else None
					except: pass
			except: pass
			return False
	def delete(self):
		try:
			from SUBMODELS import SUBMODELS
			return SUBMODELS(show_errors=self.__show_errors, display_error_point=self.__display_error_point).deleteModel(self.input)
		except Exception as error:
			try:
				if self.__show_errors:
					error_message = 'ERROR in DataLoader.delete: '+str(error)
					print(error_message)
					try: self.__print_exc() if self.__display_error_point else None
					except: pass
			except: pass
			return False
# Module to manage the loading of models and submodels in the Sapiens standard.
# --------------------------> A SAPIENS TECHNOLOGY®️ PRODUCTION) <--------------------------
