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 __transfer_db_path(path: AbsolutePath, old_folder: AbsolutePath, new_folder: AbsolutePath): old_name = old_folder.title old_basename = path.get_basename() assert old_basename.startswith(old_name) new_basename = f"{new_folder.title}{old_basename[(len(old_name)):]}" new_path = AbsolutePath.join(new_folder, new_basename) re_path = AbsolutePath.join(new_folder, old_basename) if re_path.exists(): print("Database: renaming", re_path, file=sys.stderr) if new_path.exists(): raise exceptions.PathAlreadyExists(new_path) FileSystem.rename(re_path.path, new_path.path) assert not re_path.exists() assert new_path.exists() return new_path