def __init__(self, notifier=DEFAULT_NOTIFIER): self.home_dir = AbsolutePath(str(Path.home())) self.app_dir = AbsolutePath.join(self.home_dir, f".{self.app_name}").mkdir() self.dbs_dir = AbsolutePath.join(self.app_dir, "databases").mkdir() self.lang_dir = AbsolutePath.join(self.app_dir, "languages").mkdir() self.config_path = AbsolutePath.join(self.app_dir, "config.json") self.config = Config() self.databases = {} # type: Dict[AbsolutePath, Optional[Database]] self.languages = {} # type: Dict[AbsolutePath, Optional[DefaultLanguage]] self.notifier = notifier # Load database names. for entry in FileSystem.scandir(self.dbs_dir.path): # type: os.DirEntry if entry.is_dir(): self.databases[AbsolutePath(entry.path)] = None # Load language names. for entry in FileSystem.scandir(self.lang_dir.path): # type: os.DirEntry path = AbsolutePath(entry.path) if path.isfile() and path.extension == "txt": self.languages[path] = None # Load default languages names. for entry in FileSystem.scandir( os.path.join(package_dir(), "language", "default") ): path = AbsolutePath(entry.path) if path.isfile() and path.extension == "txt": print("Checking embedded language", path.title) user_path = AbsolutePath.join(self.lang_dir, path.get_basename()) if user_path in self.languages: if user_path.get_date_modified() < path.get_date_modified(): user_path.delete() path.copy_file_to(user_path) print("Updated embedded language", path.title) else: print("User language more up-to-date", path.title) else: path.copy_file_to(user_path) self.languages[user_path] = None print("Installed embedded language", path.title) # Load config file. if self.config_path.exists(): assert self.config_path.isfile() self.config.update(parse_json(self.config_path)) # Load language. lang_path = AbsolutePath.join(self.lang_dir, f"{self.config.language}.txt") if lang_path not in self.languages: if self.config.language == DefaultLanguage.__language__: print( "[Default language]", DefaultLanguage.__language__, file=sys.stderr ) dff_dump(language_to_dict(DefaultLanguage, extend=False), lang_path) else: raise exceptions.MissingLanguageFile(self.config.language) self.languages[lang_path] = self._load_lang(lang_path)
def list_files(self, output: str): self.database.list_files(output) output_path = AbsolutePath(output) if not output_path.isfile(): raise OSError("Unable to output videos file names in %s" % output_path) return str(output_path)
def __init__( self, src: AbsolutePath, dst: AbsolutePath, *, buffer_size: int = 0, notifier: Notifier = DEFAULT_NOTIFIER, notify_end: bool = True, ): src = AbsolutePath.ensure(src) dst = AbsolutePath.ensure(dst) buffer_size = buffer_size or 32 * 1024 * 1024 assert buffer_size > 0 if not src.isfile(): raise core_exceptions.NotAFileError(src) if dst.exists(): raise FileExistsError(dst) dst_dir = dst.get_directory() if not dst_dir.isdir(): raise NotADirectoryError(dst_dir) disk_usage = shutil.disk_usage(dst_dir.path) total = src.get_size() if total >= disk_usage.free: raise core_exceptions.DiskSpaceError(total, disk_usage.free) self.src = src self.dst = dst self.total = total self.buffer_size = buffer_size self.notifier = notifier self.cancel = False self.notify_end = notify_end
def flush_json_data( data: object, previous_file_path: AbsolutePath, target_file_path: AbsolutePath, next_file_path: AbsolutePath, ): # Store JSON data to next file with open(next_file_path.path, "w") as output_file: json.dump(data, output_file) # Remove previous file previous_file_path.delete() # Move target file to previous file if target_file_path.isfile(): FileSystem.rename(target_file_path.path, previous_file_path.path) assert not target_file_path.isfile() assert previous_file_path.isfile() # Move next file to target file FileSystem.rename(next_file_path.path, target_file_path.path) # Next file deleted # Previous file may exists # Target file contains data assert not next_file_path.exists() assert target_file_path.isfile()
def change_video_path(self, video_id: int, path: AbsolutePath) -> AbsolutePath: path = AbsolutePath.ensure(path) assert path.isfile() video = self.select_one("video", (), video_id=video_id) assert video.filename != path old_filename = video.filename self.modify("video", [video_id], filename=str(path)) self.save() self.__notify_filename_modified() return old_filename