def save(self): self.logger.debug("Saving to %s", self.path) misc.create_dir(os.path.dirname(self.path)) try: with open(self.path, 'w', encoding="utf8") as f: f.write(repr(self._cache)) except IOError as e: self.logger.info("Unable to save: %s", e)
def artwork_save_cache(self): misc.create_dir('~/.config/sonata/') filename = os.path.expanduser("~/.config/sonata/art_cache") try: with open(filename, 'w', encoding="utf8") as f: f.write(repr(self.cache)) except IOError: pass
def get_lyrics_response(self, artist_then, title_then, song_dir, lyrics=None, error=None): if lyrics and not error: filename = self.target_lyrics_filename(artist_then, title_then, song_dir) # Save lyrics to file: misc.create_dir('~/.lyrics/') try: with open(filename, 'w', encoding="utf-8") as f: lyrics = misc.unescape_html(lyrics) f.write(lyrics) except IOError: pass self._show_lyrics(artist_then, title_then, lyrics, error)
def get_lyrics_response(self, artist_then, title_then, song_dir, lyrics=None, error=None): if lyrics and not error: filename = target_lyrics_filename(self.config, artist_then, title_then, song_dir) # Save lyrics to file: misc.create_dir('~/.lyrics/') self.logger.info("Saving lyrics to: %s", filename) try: with open(filename, 'w', encoding="utf-8") as f: lyrics = misc.unescape_html(lyrics) f.write(lyrics) except IOError as e: self.logger.warning("Can't save lyrics to %s: %s", filename, e) self.communicate(artist_then, title_then, lyrics, error)
def settings_load_real(self): """Load configuration from file""" conf = ConfigParser() misc.create_dir(os.path.dirname(self.CONFIG_PATH)) conf.read(self.CONFIG_PATH) # Load all the "simple" options, as described in self._options, and set # them as instance attribute. for section, attributes in self._options.items(): for attribute, (opt_key, type, default) in attributes.items(): if conf.has_option(section, opt_key): try: value = getattr(conf, 'get' + type)(section, opt_key) except Exception as e: # BBB: we need to expect some errors since Sonata uses # to write None values for "int"-type settings, which # fail to be loaded when using getint(). The new code # should write better values each time. Consider # removing this try/except clause when configuration # files are "clean". value = default # This should be safe in all cases faulty_value = conf.get(section, opt_key) logger.warning( "Can't load %r from section %r (as %s). Value is %r", opt_key, section, type if type else "str", faulty_value) else: value = default setattr(self, attribute, value) # Load all the attributes which have several values and are indexed. for section, (index_name, attributes) in self._indexed_options.items(): if not conf.has_option(section, index_name): num = 0 else: num = conf.getint(section, index_name) for attribute, (key, type, default) in attributes.items(): if num == 0: setattr(self, attribute, default) else: setattr(self, attribute, []) for i in range(num): opt_key = "%s[%d]" % (key, i) value = getattr(conf, 'get' + type)(section, opt_key) getattr(self, attribute).append(value) # Finally, load attributes related to the library. This is a bit weird # so we use an helper function to make it easier: def lib_get(name): # Helper function to load attributes related to the library. value = None if conf.has_option('library', name): value = conf.get('library', name) if value == LIB_NODATA: value = None return value if conf.has_section('library'): album = lib_get('lib_album') artist = lib_get('lib_artist') genre = lib_get('lib_genre') year = lib_get('lib_year') path = lib_get('lib_path') self.wd = SongRecord(album, artist, genre, year, path) # Finally, patch some values: self.musicdir = [misc.sanitize_musicdir(v) for v in self.musicdir] # Ensure we have a valid profile number: self.profile_num = max(0, min(self.profile_num, len(self.profile_names) - 1)) # Specifying remote artwork first is too confusing and probably # rarely used, so we're removing this option and defaulting users # back to the default 'local, then remote' option. # Backward compatibility if self.covers_pref > consts.ART_LOCAL_REMOTE: self.covers_pref = consts.ART_LOCAL_REMOTE
def settings_load_real(self): """Load configuration from file""" conf = ConfigParser() misc.create_dir(os.path.dirname(self.CONFIG_PATH)) conf.read(self.CONFIG_PATH) # Load all the "simple" options, as described in self._options, and set # them as instance attribute. for section, attributes in self._options.items(): for attribute, (opt_key, type, default) in attributes.items(): if conf.has_option(section, opt_key): try: value = getattr(conf, 'get' + type)(section, opt_key) except Exception as e: # BBB: we need to expect some errors since Sonata uses # to write None values for "int"-type settings, which # fail to be loaded when using getint(). The new code # should write better values each time. Consider # removing this try/except clause when configuration # files are "clean". value = default # This should be safe in all cases faulty_value = conf.get(section, opt_key) logger.warning( "Can't load %r from section %r (as %s). Value is %r", opt_key, section, type if type else "str", faulty_value) else: value = default setattr(self, attribute, value) # Load all the attributes which have several values and are indexed. for section, (index_name, attributes) in self._indexed_options.items(): if not conf.has_option(section, index_name): num = 0 else: num = conf.getint(section, index_name) for attribute, (key, type, default) in attributes.items(): if num == 0: setattr(self, attribute, default) else: setattr(self, attribute, []) for i in range(num): opt_key = "%s[%d]" % (key, i) value = getattr(conf, 'get' + type)(section, opt_key) getattr(self, attribute).append(value) # Finally, load attributes related to the library. This is a bit weird # so we use an helper function to make it easier: def lib_get(name): # Helper function to load attributes related to the library. value = None if conf.has_option('library', name): value = conf.get('library', name) if value == LIB_NODATA: value = None return value if conf.has_section('library'): album = lib_get('lib_album') artist = lib_get('lib_artist') genre = lib_get('lib_genre') year = lib_get('lib_year') path = lib_get('lib_path') self.wd = SongRecord(album, artist, genre, year, path) # Finally, patch some values: self.musicdir = [misc.sanitize_musicdir(v) for v in self.musicdir] # Ensure we have a valid profile number: self.profile_num = max( 0, min(self.profile_num, len(self.profile_names) - 1)) # Specifying remote artwork first is too confusing and probably # rarely used, so we're removing this option and defaulting users # back to the default 'local, then remote' option. # Backward compatibility if self.covers_pref > consts.ART_LOCAL_REMOTE: self.covers_pref = consts.ART_LOCAL_REMOTE