def _init_database(self, initialize): # Initialize local database if initialize: import resources.lib.database.db_local as db_local self.LOCAL_DB = db_local.NFLocalDatabase() # Initialize shared database use_mysql = G.ADDON.getSettingBool('use_mysql') if initialize or use_mysql: import resources.lib.database.db_shared as db_shared from resources.lib.common.exceptions import DBMySQLConnectionError, DBMySQLError try: shared_db_class = db_shared.get_shareddb_class( use_mysql=use_mysql) self.SHARED_DB = shared_db_class() except (DBMySQLConnectionError, DBMySQLError) as exc: import resources.lib.kodi.ui as ui if isinstance(exc, DBMySQLError): # There is a problem with the database ui.show_addon_error_info(exc) # The MySQL database cannot be reached, fallback to local SQLite database # When this code is called from addon, is needed apply the change also in the # service, so disabling it run the SettingsMonitor self.ADDON.setSettingBool('use_mysql', False) ui.show_notification(self.ADDON.getLocalizedString(30206), time=10000) shared_db_class = db_shared.get_shareddb_class() self.SHARED_DB = shared_db_class()
def _init_database(self): # Initialize local database import resources.lib.database.db_local as db_local self.LOCAL_DB = db_local.NFLocalDatabase() # Initialize shared database import resources.lib.database.db_shared as db_shared from resources.lib.database.db_exceptions import MySQLConnectionError try: shared_db_class = db_shared.get_shareddb_class() self.SHARED_DB = shared_db_class() except MySQLConnectionError: # The MySQL database cannot be reached, fallback to local SQLite database # When this code is called from addon, is needed apply the change also in the # service, so disabling it run the SettingsMonitor import resources.lib.kodi.ui as ui self.ADDON.setSettingBool('use_mysql', False) ui.show_notification(self.ADDON.getLocalizedString(30206), time=10000) shared_db_class = db_shared.get_shareddb_class(force_sqlite=True) self.SHARED_DB = shared_db_class()
def init_globals(self, argv, skip_database_initialize=False): """Initialized globally used module variables. Needs to be called at start of each plugin instance! This is an ugly hack because Kodi doesn't execute statements defined on module level if reusing a language invoker.""" self.PY_IS_VER2 = sys.version_info.major == 2 self.COOKIES = {} self.ADDON = xbmcaddon.Addon() self.ADDON_ID = self.ADDON.getAddonInfo('id') self.PLUGIN = self.ADDON.getAddonInfo('name') self.VERSION = self.ADDON.getAddonInfo('version') self.DEFAULT_FANART = self.ADDON.getAddonInfo('fanart') self.ICON = self.ADDON.getAddonInfo('icon') self.ADDON_DATA_PATH = self.ADDON.getAddonInfo('path') # Addon folder self.DATA_PATH = self.ADDON.getAddonInfo( 'profile') # Addon user data folder # Add absolute paths of embedded py modules to python system directory module_paths = [ os.path.join(self.ADDON_DATA_PATH, 'modules', 'mysql-connector-python') ] for path in module_paths: path = xbmc.translatePath(path) if path not in sys.path: sys.path.insert(0, path) self.CACHE_PATH = os.path.join(self.DATA_PATH, 'cache') self.COOKIE_PATH = os.path.join(self.DATA_PATH, 'COOKIE') self.CACHE_TTL = self.ADDON.getSettingInt('cache_ttl') * 60 self.CACHE_METADATA_TTL = ( self.ADDON.getSettingInt('cache_metadata_ttl') * 24 * 60 * 60) self.URL = urlparse(argv[0]) try: self.PLUGIN_HANDLE = int(argv[1]) except IndexError: self.PLUGIN_HANDLE = 0 self.BASE_URL = '{scheme}://{netloc}'.format(scheme=self.URL[0], netloc=self.URL[1]) self.PATH = g.py2_decode(unquote(self.URL[2][1:])) try: self.PARAM_STRING = argv[2][1:] except IndexError: self.PARAM_STRING = '' self.REQUEST_PARAMS = dict(parse_qsl(self.PARAM_STRING)) self.reset_time_trace() self.TIME_TRACE_ENABLED = self.ADDON.getSettingBool('enable_timing') self.IPC_OVER_HTTP = self.ADDON.getSettingBool('enable_ipc_over_http') if not skip_database_initialize: # Initialize local database import resources.lib.database.db_local as db_local self.LOCAL_DB = db_local.NFLocalDatabase() # Initialize shared database import resources.lib.database.db_shared as db_shared from resources.lib.database.db_exceptions import MySQLConnectionError try: shared_db_class = db_shared.get_shareddb_class() self.SHARED_DB = shared_db_class() except MySQLConnectionError: # The MySQL database cannot be reached, fallback to local SQLite database # When this code is called from addon, is needed apply the change also in the # service, so disabling it run the SettingsMonitor import resources.lib.kodi.ui as ui self.ADDON.setSettingBool('use_mysql', False) ui.show_notification(self.ADDON.getLocalizedString(30206), time=10000) shared_db_class = db_shared.get_shareddb_class( force_sqlite=True) self.SHARED_DB = shared_db_class() self.settings_monitor_suspend( False) # Reset the value in case of addon crash try: os.mkdir(self.DATA_PATH) except OSError: pass self._init_cache()