def __init__(self): self.app_name = "Pysaurus" self.home_dir = AbsolutePath(str(Path.home())) self.app_dir = AbsolutePath.join(self.home_dir, f".{self.app_name}").mkdir() self.db_path = AbsolutePath.join(self.app_dir, "databases.db") self.lang_dir = AbsolutePath.join(self.app_dir, "lang").mkdir() self.db = VideoDatabase(self.db_path.path) row = self.db.query_one("SELECT application_id, lang FROM application") self.application_id = row[0] self.lang = row[1] say.set_language(self.lang) say.set_folder(self.lang_dir)
def rename(self, new_name) -> None: old_db_folder = self.__db_folder old_thumb_folder = self.__thumb_folder old_json_path = self.__json_path old_miniature_path = self.__miniatures_path old_log_path = self.__log_path new_name = new_name.strip() if new_name == self.__db_folder.title: return if functions.has_discarded_characters(new_name): raise exceptions.InvalidDatabaseName(new_name) new_db_folder = AbsolutePath.join(old_db_folder.get_directory(), new_name) if new_db_folder.exists(): raise exceptions.DatabaseAlreadyExists(new_db_folder) FileSystem.rename(old_db_folder.path, new_db_folder.path) assert not old_db_folder.exists() assert new_db_folder.isdir() self.__db_folder = new_db_folder self.__thumb_folder = self.__transfer_db_path(old_thumb_folder, old_db_folder, new_db_folder) self.__json_path = self.__transfer_db_path(old_json_path, old_db_folder, new_db_folder) self.__miniatures_path = self.__transfer_db_path( old_miniature_path, old_db_folder, new_db_folder) self.__log_path = self.__transfer_db_path(old_log_path, old_db_folder, new_db_folder) self.notifier.set_log_path(self.__log_path.path) self.set_path(self.__json_path)
def main(): app = Application() db = app.open_database(AbsolutePath.join(app.dbs_dir, "adult videos")) all_miniatures = db.ensure_miniatures( returns=True) # type: List[Miniature] video_id_to_miniature = {m.video_id: m for m in all_miniatures} videos = db.get_videos("readable", "with_thumbnails") # type: List[Video] prop_name = "<?newsensations>" classifier = {} for video in videos: prop_val = video.properties.get(prop_name, -1) classifier.setdefault(prop_val, []).append(video) assert 0 in classifier assert 1 in classifier negative_videos = classifier[0] positive_videos = classifier[1] video_id_to_class = {video.video_id: False for video in negative_videos} video_id_to_class.update( {video.video_id: True for video in positive_videos}) miniatures = [] classes = [] for video_id, y in video_id_to_class.items(): miniatures.append(video_id_to_miniature[video_id]) classes.append(video_id_to_class[video_id]) theta = train(miniatures, classes) print("Predictions:") for i, (x, y) in enumerate(zip(miniatures, classes)): p = predict(x, theta) b = p >= 0.5 if y else p < 0.5 print("Image", i + 1, "expects", y, "predicts", p, "good?", b)
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
def __init__(self, path: PathType): path = AbsolutePath.ensure(path) folder = path.get_directory() self.__json_path = path self.__prev_path = AbsolutePath.file_path(folder, path.title, "prev.json") self.__next_path = AbsolutePath.file_path(folder, path.title, "next.json") self.__thumb_folder = AbsolutePath.join(folder, f"{path.title}.thumbnails")
def main(): if len(sys.argv) != 2: return path = AbsolutePath(sys.argv[1]).assert_file() assert path.extension.lower() == "webp" image = Image.open(path.path) image.info.pop("background", None) assert image.is_animated output_path = AbsolutePath.join(path.get_directory(), f"{path.title}.gif") image.save(output_path.path, "gif", save_all=True, optimize=False) print(output_path)
def main(): if len(sys.argv) != 2: return folder = AbsolutePath.ensure_directory(sys.argv[1]) for basename in folder.listdir(): try: path = AbsolutePath.join(folder, basename) if not path.isfile(): continue image = Image.open(path.path) if image.is_animated: print(path) except UnidentifiedImageError: pass
def new_database(self, name, folders: Iterable[AbsolutePath]): if functions.has_discarded_characters(name): raise exceptions.InvalidDatabaseName(name) path = AbsolutePath.join(self.dbs_dir, name) if path.title != name: raise exceptions.InvalidDatabaseName(name) if path in self.databases: raise exceptions.DatabaseAlreadyExists(path) if path.exists(): raise exceptions.DatabasePathUnavailable(path) self.databases[path] = Database( path.mkdir(), folders=folders, notifier=self.notifier, lang=self.lang ) return self.databases[path]
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 main(): if len(sys.argv) != 2: print("No path specified.") return folder = AbsolutePath(sys.argv[1]).assert_dir() lines = Counter() for file_name in folder.listdir(): path = AbsolutePath.join(folder, file_name) if path.isfile(): with open(path.path) as file: for line in file: lines.update([line.strip()]) inv = {} for line, count in lines.items(): inv.setdefault(count, []).append(line) for count in sorted(inv, reverse=True): for line in sorted(inv[count]): print(f"{count}\t{line}")
def __init__(self, *, geometry=None): super().__init__() # setup interface self.interface = Interface() # setup page html_path = AbsolutePath.join(package_dir(), "interface", "web", "index.html").assert_file() url = QUrl.fromLocalFile(html_path.path) print("Loading", url) with open(html_path.path) as file: html = file.read() html = html.replace( "<!--headerScript-->", '<script src="qrc:///qtwebchannel/qwebchannel.js"></script>', ) html = html.replace('<script src="onload.js"></script>', '<script src="qt.js"></script>') self.web_page = CustomPage() self.web_page.setHtml(html, url) self.setPage(self.web_page) # setup channel self.channel = QWebChannel() self.channel.registerObject("backend", self.interface) self.page().setWebChannel(self.channel) # setup geometry if geometry: assert isinstance(geometry, (tuple, list)) assert len(geometry) == 4 self.setGeometry(*geometry) # setup player self.player = None if has_vlc: self.player = Player(on_next=self._on_next_random_video) self.interface.player_triggered.connect(self.player.show) if geometry: _, _, width, height = geometry self.player.resize(int(width * 3 / 4), int(height * 3 / 4))
def new_sub_folder(folder: AbsolutePath, suffix: str, sep="."): return AbsolutePath.join(folder, f"{folder.title}{sep}{suffix}")
def current_path(self): return AbsolutePath.join(self.folder, f"{self.current}.txt")
def lang(self) -> DefaultLanguage: return self.languages[ AbsolutePath.join(self.lang_dir, f"{self.config.language}.txt") ]
def open_language_from_name(self, name: str) -> DefaultLanguage: return self.open_language(AbsolutePath.join(self.lang_dir, f"{name}.txt"))
def open_database_from_name(self, name: str) -> Database: return self.open_database(AbsolutePath.join(self.dbs_dir, name))