Repository URL to install this package:
#!/usr/bin/python
# coding=utf-8
#
# Copyright (C) 2018-2025 by dream-alpha
#
# In case of reuse of this source code please do not remove this copyright.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For more information on the GNU General Public License see:
# <http://www.gnu.org/licenses/>.
import os
import socket
from Plugins.SystemPlugins.MountCockpit.MountCockpit import MountCockpit
from Plugins.SystemPlugins.CacheCockpit.FileManager import FileManager
from .Debug import logger
from .ServiceUtils import ALL_VIDEO
from .DelayTimer import DelayTimer
from .FileManagerUtils import FILE_TYPE_DIR, FILE_TYPE_LINK
class FileManagerCache():
def __init__(self):
logger.info("...")
self.success = True
self.database_loaded = False
self.database_loaded_callback = None
self.bookmarks = MountCockpit.getInstance().getMountedBookmarks("MVC")
self.files_total = 0
self.files_done = 0
self.file_name = ""
self.host_name = socket.gethostname()
def onDatabaseLoaded(self, callback=None):
logger.info("...")
self.database_loaded_callback = callback
if self.database_loaded:
self.onDatabaseLoadedCallback()
def onDatabaseLoadedCallback(self):
logger.info("...")
if self.database_loaded_callback:
self.database_loaded_callback(self.success)
def loadDatabase(self, dirs=None):
self.database_loaded = False
if dirs is None:
self.bookmarks = MountCockpit.getInstance().getMountedBookmarks("MVC")
dirs = self.bookmarks
logger.info("dirs: %s", dirs)
if dirs:
self.load_list = self.getDirsLoadList(dirs)
self.files_total = len(self.load_list)
self.files_done = 0
self.file_name = ""
DelayTimer(10, self.nextFileOp)
def nextFileOp(self):
# logger.info("...")
if self.load_list:
path = self.load_list.pop(0)
self.file_name = os.path.basename(path)
self.loadDatabaseFile(path)
self.files_done += 1
DelayTimer(10, self.nextFileOp)
else:
logger.debug("done.")
self.database_loaded = True
self.onDatabaseLoadedCallback()
def loadDatabaseFile(self, path):
# logger.info("path: %s", path)
afile = ()
if os.path.isfile(path):
self.newFileData(path)
elif os.path.islink(path):
self.newDirData(path, FILE_TYPE_LINK)
elif os.path.isdir(path):
self.newDirData(path, FILE_TYPE_DIR)
if afile:
if self.database_loaded:
self.onDatabaseChangedCallback()
def newDirData(self, path, _file_type):
# logger.info("path: %s, file_type: %s", path, file_type)
afile = FileManager.getInstance("MVC").getFile("table1", path)
if not afile:
logger.error("%s not in cache", path)
self.success = False
def newFileData(self, path):
# logger.info("path: %s", path)
afile = FileManager.getInstance("MVC").getFile("table1", path)
if not afile:
logger.error("%s not in cache", path)
self.success = False
def __getDirLoadList(self, adir):
# logger.debug("adir: %s", adir)
load_list = []
if os.path.exists(adir):
walk_listdir = os.listdir(adir)
for walk_name in walk_listdir:
path = os.path.join(adir, walk_name)
if os.path.isfile(path):
ext = os.path.splitext(path)[1]
if ext in ALL_VIDEO:
load_list.append(path)
else:
load_list.append(path)
load_list += self.__getDirLoadList(path)
else:
logger.error("adir does not exist: %s", adir)
return load_list
def getDirsLoadList(self, dirs):
# logger.info("dirs: %s", dirs)
load_list = []
for adir in dirs:
load_list.append(adir)
load_list += self.__getDirLoadList(adir)
return load_list