def load(self): '''load songs and playlists from db file''' self.logdebug("Loading library...") # Load songs try: db_objs = utils.load_db(get_config_file("songs.db")) except: self.logexception("Failed to load library") db_objs = [] # Load playlists try: pls_objs = utils.load_db(get_config_file("playlists.db")) except: self.logexception("Failed load playlists") pls_objs = [] if db_objs: for obj in db_objs: try: song_type = obj["song_type"] except KeyError: self.logerror("Song with no type found, %s", obj.get("uri")) if song_type not in self.__song_types: self.logwarn("Song type %s not exist, for registration", song_type) self.register_type(song_type) if self.__force_check: s = Song() s.init_from_dict(obj) s["uri"] = utils.realuri(s.get("uri")) else: s = Song(obj) s.set_type(song_type) if not self.__force_check or not self.__songs.has_key(s.get("uri")): self.add(s) if pls_objs: for pl_obj in pls_objs: name, infos = pl_obj if self.__force_check: infos = map(utils.realuri, infos) self.create_playlist("local", name, infos) if self.__force_check: self.save() self.__dirty = False # fire signal self.__reset_queued_signal() gobject.timeout_add(AUTOSAVE_TIMEOUT, self.interval_async_save) gobject.timeout_add(SIGNAL_DB_QUERY_FIRED * 20, self.__fire_queued_signal) self.logdebug("%s songs loaded in %s types", len(self.__songs), len(self.__song_types)) self.logdebug("Finish loading library") gobject.idle_add(self.__delay_post_load)
def create_song(self, tags, song_type, read_from_file=False): '''Create song''' self.set_dirty() try: uri = tags["uri"] except KeyError: raise MissingUriTag() tags["uri"] = utils.realuri(uri) return self.__create_song(tags, song_type, read_from_file=read_from_file)
def get_song(self, uri): uri = utils.realuri(uri) try: return self.__songs[uri] except KeyError: if not uri: return None if uri.startswith("file://"): return self.get_or_create_song({"uri":uri}, "local", read_from_file=True) else: return self.get_or_create_song({"uri":uri}, "unknown", read_from_file=True)
def reload_song_from_file(self, song): s = Song() s.init_from_dict({"uri":song.get("uri")}) s["uri"] = utils.realuri(s.get("uri")) s.read_from_file() new_tags = {} for key in TAG_KEYS.values() + ["#size", "#mtime", "ctime"]: if s.has_key(key) and not song.has_key(key): new_tags[key] = s.get(key) elif not s.has_key(key) and song.has_key(key): new_tags[key] = song.get(key) elif s.get(key) != song.get(key): new_tags[key] = s.get(key) self.set_property(song, new_tags)
def reload_song_from_file(self, song): s = Song() s.init_from_dict({"uri": song.get("uri")}) s["uri"] = utils.realuri(s.get("uri")) s.read_from_file() new_tags = {} for key in TAG_KEYS.values() + ["#size", "#mtime", "ctime"]: if s.has_key(key) and not song.has_key(key): new_tags[key] = s.get(key) elif not s.has_key(key) and song.has_key(key): new_tags[key] = song.get(key) elif s.get(key) != song.get(key): new_tags[key] = s.get(key) self.set_property(song, new_tags)
def get_song(self, uri): uri = utils.realuri(uri) try: return self.__songs[uri] except KeyError: if not uri: return None if uri.startswith("file://"): return self.get_or_create_song({"uri": uri}, "local", read_from_file=True) else: return self.get_or_create_song({"uri": uri}, "unknown", read_from_file=True)
def get_or_create_song(self, tags, song_type, read_from_file=False): self.set_dirty() try: uri = utils.realuri(tags["uri"]) except KeyError: raise MissingUriTag() try: s = self.__songs[uri] if s.get_type() != song_type: raise KeyError except KeyError: s = self.__create_song(tags, song_type, read_from_file) else: if read_from_file: s.read_from_file() self.set_property(s, tags) return s
def has_uri(self, uri): uri = utils.realuri(uri) return self.__songs.has_key(uri)
def load(self): '''load songs and playlists from db file''' self.logdebug("Loading library...") # Load songs try: db_objs = utils.load_db(get_config_file("songs.db")) except: self.logexception("Failed to load library") db_objs = [] # Load playlists try: pls_objs = utils.load_db(get_config_file("playlists.db")) except: self.logexception("Failed load playlists") pls_objs = [] if db_objs: for obj in db_objs: try: song_type = obj["song_type"] except KeyError: self.logerror("Song with no type found, %s", obj.get("uri")) if song_type not in self.__song_types: self.logwarn("Song type %s not exist, for registration", song_type) self.register_type(song_type) if self.__force_check: s = Song() s.init_from_dict(obj) s["uri"] = utils.realuri(s.get("uri")) else: s = Song(obj) s.set_type(song_type) if not self.__force_check or not self.__songs.has_key( s.get("uri")): self.add(s) if pls_objs: for pl_obj in pls_objs: name, infos = pl_obj if self.__force_check: infos = map(utils.realuri, infos) self.create_playlist("local", name, infos) if self.__force_check: self.save() self.__dirty = False # fire signal self.__reset_queued_signal() gobject.timeout_add(AUTOSAVE_TIMEOUT, self.interval_async_save) gobject.timeout_add(SIGNAL_DB_QUERY_FIRED * 20, self.__fire_queued_signal) self.logdebug("%s songs loaded in %s types", len(self.__songs), len(self.__song_types)) self.logdebug("Finish loading library") gobject.idle_add(self.__delay_post_load)