From: kaliko Date: Sat, 2 Oct 2021 13:23:04 +0000 (+0200) Subject: Mainly use literal for list/dict and f-strings when possible X-Git-Tag: 0.18.0~14 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=a260ebea93f23d72aa6e0178744b0f64c469b7ba;p=mpd-sima.git Mainly use literal for list/dict and f-strings when possible --- diff --git a/sima/core.py b/sima/core.py index 50c9ff9..badc7b2 100644 --- a/sima/core.py +++ b/sima/core.py @@ -44,8 +44,8 @@ class Sima(Daemon): self.sdb = SimaDB(db_path=conf.get('sima', 'db_file')) PlayerClient.database = self.sdb self.log = getLogger('sima') - self._plugins = list() - self._core_plugins = list() + self._plugins = [] + self._core_plugins = [] self.player = PlayerClient(conf) # MPD client self.short_history = deque(maxlen=60) self.changed = None @@ -102,7 +102,7 @@ class Sima(Daemon): return False def queue(self): - to_add = list() + to_add = [] for plugin in self.plugins: self.log.debug('callback_need_track: %s', plugin) pl_candidates = getattr(plugin, 'callback_need_track')() diff --git a/sima/launch.py b/sima/launch.py index 7117f09..784d237 100644 --- a/sima/launch.py +++ b/sima/launch.py @@ -58,12 +58,12 @@ def load_plugins(sima, source): # TODO: Sanity check for "sima.config.get('sima', source)" ? for plugin in sima.config.get('sima', source).split(','): plugin = plugin.strip(' \n') - module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower()) + module = f'sima.plugins.{source}.{plugin.lower()}' try: mod_obj = sima_import(module, fromlist=[plugin]) except ImportError as err: - logger.error('Failed to load "{}" plugin\'s module: '.format(plugin) + - '{0} ({1})'.format(module, err)) + logger.error(f'Failed to load "{plugin}" plugin\'s module: ' + + f'{module} ({err})') sima.shutdown() sys.exit(1) try: diff --git a/sima/lib/http.py b/sima/lib/http.py index a87398b..5014516 100644 --- a/sima/lib/http.py +++ b/sima/lib/http.py @@ -58,7 +58,7 @@ class CacheController: """Normalize the URL to create a safe key for the cache""" (scheme, authority, path, query, _) = parse_uri(uri) if not scheme or not authority: - raise Exception("Only absolute URIs are allowed. uri = %s" % uri) + raise Exception(f'Only absolute URIs are allowed. uri = {uri}') authority = authority.lower() scheme = scheme.lower() if not path: @@ -294,8 +294,7 @@ class HttpClient: try: return self.fetch_ws(req) except Timeout as err: - raise WSTimeout('Failed to reach server within {0}s'.format( - SOCKET_TIMEOUT)) from err + raise WSTimeout(f'Failed to reach server within {SOCKET_TIMEOUT}s') from err except HTTPConnectionError as err: raise WSError(err) from err @@ -308,7 +307,7 @@ class HttpClient: self.stats.update(etag=self.stats.get('etag')+1) resp = self.controller.update_cached_response(prepreq, resp) elif resp.status_code != 200: - raise WSHTTPError('{0.status_code}: {0.reason}'.format(resp)) + raise WSHTTPError(f'{resp.status_code}: {resp.reason}') self.controller.cache_response(resp.request, resp) return resp diff --git a/sima/lib/logger.py b/sima/lib/logger.py index 75a8f65..c257926 100644 --- a/sima/lib/logger.py +++ b/sima/lib/logger.py @@ -78,7 +78,7 @@ def set_logger(level='info', logfile=None): if filehdl: logger.handlers = [] # Add timestamp for file handler - log_format = '{0} {1}'.format('{asctime}', log_format) + log_format = f'{{asctime}} {log_format}' formatter = logging.Formatter(log_format, DATE_FMT, '{') # create file handler fileh = logging.FileHandler(logfile) diff --git a/sima/lib/meta.py b/sima/lib/meta.py index af3e19c..ac5948a 100644 --- a/sima/lib/meta.py +++ b/sima/lib/meta.py @@ -147,7 +147,7 @@ class Meta: if callable(other.__str__) and other.__str__() != self.name: self.__aliases |= {other.__str__()} else: - raise MetaException('No __str__ method found in {!r}'.format(other)) + raise MetaException(f'No __str__ method found in {other!r}') @property def name(self): diff --git a/sima/lib/plugin.py b/sima/lib/plugin.py index 2cc284f..86b7557 100644 --- a/sima/lib/plugin.py +++ b/sima/lib/plugin.py @@ -41,7 +41,7 @@ class Plugin: def info(cls): """self documenting class method """ - doc = 'Undocumented plugin! Fill "{}" docstring'.format(cls.__name__) + doc = f'Undocumented plugin! Fill "{cls.__name__}" docstring' if cls.__doc__: doc = cls.__doc__.strip(' \n').splitlines()[0] return {'name': cls.__name__, @@ -166,7 +166,7 @@ class AdvancedPlugin(Plugin): :param Artist artist: Artist to fetch an album for :param bool unplayed: Fetch only unplayed album """ - self.log.info('Searching an album for "%s"...' % artist) + self.log.info('Searching an album for "%s"...', artist) albums = self.player.search_albums(artist) if not albums: return None @@ -176,7 +176,7 @@ class AdvancedPlugin(Plugin): albums_not_in_hist = [a for a in albums if a.name not in albums_hist] # Get to next artist if there are no unplayed albums if not albums_not_in_hist: - self.log.info('No unplayed album found for "%s"' % artist) + self.log.info('No unplayed album found for "%s"', artist) if unplayed: return None random.shuffle(albums_not_in_hist) diff --git a/sima/lib/simadb.py b/sima/lib/simadb.py index 7d92c6c..dddf4eb 100644 --- a/sima/lib/simadb.py +++ b/sima/lib/simadb.py @@ -373,7 +373,7 @@ class SimaDB: :param sima.lib.track.Track track: track to use :param bool add: add non existing track to database""" if not track.file: - raise SimaDBError('Got a track with no file attribute: %r' % track) + raise SimaDBError(f'Got a track with no file attribute: {track}') if with_connection: connection = with_connection else: @@ -489,7 +489,7 @@ class SimaDB: LEFT OUTER JOIN albumartists ON tracks.albumartist = albumartists.id WHERE history.last_play > ? AND albums.name NOT NULL AND artists.name NOT NULL ORDER BY history.last_play DESC""", (date.isoformat(' '),)) - hist = list() + hist = [] for row in rows: vals = dict(row) if needle: # Here use artist instead of albumartist @@ -530,7 +530,7 @@ class SimaDB: WHERE history.last_play > ? AND artists.name NOT NULL ORDER BY history.last_play DESC""", (date.isoformat(' '),)) last = deque(maxlen=1) - hist = list() + hist = [] for row in rows: artist = Artist(**row) if last and last[0] == artist: # remove consecutive dupes @@ -567,7 +567,7 @@ class SimaDB: WHERE history.last_play > ? AND genres.name NOT NULL ORDER BY history.last_play DESC """, (date.isoformat(' '),)) - genres = list() + genres = [] for row in rows: genres.append(row) if len({g[0] for g in genres}) >= limit: @@ -612,7 +612,7 @@ class SimaDB: else: rows = connection.execute(sql+'ORDER BY history.last_play DESC', (date.isoformat(' '),)) - hist = list() + hist = [] for row in rows: hist.append(Track(**row)) connection.close() diff --git a/sima/lib/simafm.py b/sima/lib/simafm.py index bdfd02d..5103ace 100644 --- a/sima/lib/simafm.py +++ b/sima/lib/simafm.py @@ -61,7 +61,7 @@ class SimaFM: code = ans.get('error') mess = ans.get('message') if code == 6: - raise WSNotFound('{0}: "{1}"'.format(mess, self.artist)) + raise WSNotFound(f'{mess}: "{self.artist}"') raise WSError(mess) return True @@ -76,10 +76,10 @@ class SimaFM: payload = payloads.get(method) payload.update(api_key=LFM.get('apikey'), format='json') if not isinstance(artist, Artist): - raise TypeError('"{0!r}" not an Artist object'.format(artist)) + raise TypeError(f'"{artist!r}" not an Artist object') self.artist = artist if artist.mbid: - payload.update(mbid='{0}'.format(artist.mbid)) + payload.update(mbid=f'{artist.mbid}') else: payload.update(artist=artist.name, autocorrect=1) diff --git a/sima/lib/simastr.py b/sima/lib/simastr.py index f3e77ee..ec82d91 100644 --- a/sima/lib/simastr.py +++ b/sima/lib/simastr.py @@ -81,7 +81,7 @@ class SimaStr(str): """ diafilter = True leven_ratio = 0.82 - regexp_dict = dict() + regexp_dict = {} # Leading patterns: The Le Les # case-insensitive matching for this RE diff --git a/sima/lib/track.py b/sima/lib/track.py index a9003da..2df220c 100644 --- a/sima/lib/track.py +++ b/sima/lib/track.py @@ -54,7 +54,7 @@ class Track: 'musicbrainz_artistid', 'musicbrainz_albumartistid'] # Which tags have been collapsed? - self.collapsed_tags = list() + self.collapsed_tags = [] # Needed for multiple tags which returns a list instead of a string self._collapse_tags() diff --git a/sima/lib/webserv.py b/sima/lib/webserv.py index 10ed2b9..18414e7 100644 --- a/sima/lib/webserv.py +++ b/sima/lib/webserv.py @@ -81,8 +81,8 @@ class WebService(AdvancedPlugin): self.log.info('%s: Flushing cache!', name) else: self.log.info('%s: Initialising cache!', name) - self._cache = {'asearch': dict(), - 'tsearch': dict()} + self._cache = {'asearch': {}, + 'tsearch': {}} def _cleanup_cache(self): """Avoid bloated cache @@ -101,7 +101,7 @@ class WebService(AdvancedPlugin): dynamic = self.plugin_conf.getint('max_art') if dynamic <= 0: dynamic = 100 - results = list() + results = [] similarities.reverse() while (len(results) < dynamic and similarities): art_pop = similarities.pop() @@ -156,8 +156,8 @@ class WebService(AdvancedPlugin): history = self.player.queue + history history = deque(history) last_trk = history.popleft() # remove - extra_arts = list() - ret_extra = list() + extra_arts = [] + ret_extra = [] depth = 0 while depth < self.plugin_conf.getint('depth'): if not history: @@ -250,7 +250,7 @@ class WebService(AdvancedPlugin): def find_album(self, artists): """Find albums to queue. """ - to_add = list() + to_add = [] nb_album_add = 0 target_album_to_add = self.plugin_conf.getint('album_to_add') for artist in artists: @@ -277,7 +277,7 @@ class WebService(AdvancedPlugin): """ find top tracks for artists in artists list. """ - to_add = list() + to_add = [] nbtracks_target = self.plugin_conf.getint('track_to_add') for artist in artists: if len(to_add) == nbtracks_target: @@ -285,7 +285,7 @@ class WebService(AdvancedPlugin): self.log.info('Looking for a top track for %s', artist) titles = deque() try: - titles = [t for t in self.ws.get_toptrack(artist)] + titles = list(self.ws.get_toptrack(artist)) except WSError as err: self.log.warning('%s: %s', self.ws.name, err) continue @@ -353,7 +353,7 @@ class WebService(AdvancedPlugin): self.log.debug(repr(self.player.current)) return None candidates = self.queue_mode() - msg = ' '.join(['{0}: {1:>3d}'.format(k, v) for + msg = ' '.join([f'{k}: {v:>3d}' for k, v in sorted(self.ws.stats.items())]) self.log.debug('http stats: ' + msg) if not candidates: diff --git a/sima/mpdclient.py b/sima/mpdclient.py index c2daae3..e2f0e2c 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -148,28 +148,25 @@ class MPD(MPDClient): super().connect(host, port) # Catch socket errors except OSError as err: - raise PlayerError('Could not connect to "%s:%s": %s' % - (host, port, err.strerror)) from err + raise PlayerError(f'Could not connect to "{host}:{port}": {err.strerror}' + ) from err # Catch all other possible errors # ConnectionError and ProtocolError are always fatal. Others may not # be, but we don't know how to handle them here, so treat them as if # they are instead of ignoring them. except MPDError as err: - raise PlayerError('Could not connect to "%s:%s": %s' % - (host, port, err)) from err + raise PlayerError(f'Could not connect to "{host}:{port}": {err}') from err if password: try: self.password(password) except (MPDError, OSError) as err: - raise PlayerError("Could not connect to '%s': %s" % (host, err)) from err + raise PlayerError(f"Could not connect to '{host}': {err}") from err # Controls we have sufficient rights available_cmd = self.commands() for cmd in MPD.needed_cmds: if cmd not in available_cmd: self.disconnect() - raise PlayerError('Could connect to "%s", ' - 'but command "%s" not available' % - (host, cmd)) + raise PlayerError(f'Could connect to "{host}", but command "{cmd}" not available') self.tagtypes('clear') for tag in MPD.needed_tags: self.tagtypes('enable', tag) @@ -294,7 +291,7 @@ class MPD(MPDClient): plm = {'repeat': None, 'single': None, 'random': None, 'consume': None, } for key, val in self.status().items(): - if key in plm.keys(): + if key in plm: plm.update({key: bool(int(val))}) return plm diff --git a/sima/plugins/core/uniq.py b/sima/plugins/core/uniq.py index 1ad50f7..a46d7fc 100644 --- a/sima/plugins/core/uniq.py +++ b/sima/plugins/core/uniq.py @@ -70,7 +70,7 @@ class Uniq(Plugin): self.log.warning(' '.join(channels)) def sub_chan(self): - self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid()) + self.chan = f'mpd_sima:{getfqdn()}.{getpid()}' self.log.debug('Registering as %s', self.chan) try: self.player.subscribe(self.chan) diff --git a/sima/plugins/internal/genre.py b/sima/plugins/internal/genre.py index 713ee35..1202f89 100644 --- a/sima/plugins/internal/genre.py +++ b/sima/plugins/internal/genre.py @@ -106,7 +106,7 @@ class Genre(AdvancedPlugin): if not trk: continue if queue_mode == 'track': - self.log.info('Genre plugin chose: {}'.format(trk)) + self.log.info('Genre plugin chose: %s', trk) candidates.append(trk) if len(candidates) == target: break diff --git a/sima/plugins/internal/tags.py b/sima/plugins/internal/tags.py index c891b77..32021a1 100644 --- a/sima/plugins/internal/tags.py +++ b/sima/plugins/internal/tags.py @@ -143,7 +143,7 @@ class Tags(AdvancedPlugin): if not trk: continue if queue_mode == 'track': - self.log.info('Tags plugin chose: {}'.format(trk)) + self.log.info('Tags plugin chose: %s', trk) candidates.append(trk) if len(candidates) == target: break diff --git a/sima/utils/configtest.py b/sima/utils/configtest.py index ef91241..a726d17 100644 --- a/sima/utils/configtest.py +++ b/sima/utils/configtest.py @@ -24,7 +24,7 @@ def tags_config_test(cli, config): res = cli.find(filt, 'window', (0, 300)) except PlayerError as err: cli.disconnect() - print('filter error: %s' % err, file=sys.stderr) + print(f'filter error: {err}', file=sys.stderr) sys.exit(1) artists = list({trk.albumartist for trk in res if trk.albumartist}) if not artists: diff --git a/sima/utils/filelock.py b/sima/utils/filelock.py index 81f9d3c..b5c51b6 100644 --- a/sima/utils/filelock.py +++ b/sima/utils/filelock.py @@ -47,7 +47,7 @@ class FileLock: self.filedsc = None self.is_locked = False dirname = os.path.dirname(file_name) - self.lockfile = os.path.join(dirname, '{0}.lock'.format(file_name)) + self.lockfile = os.path.join(dirname, 'f{file_name}.lock') self.file_name = file_name self.timeout = timeout self.delay = delay diff --git a/sima/utils/startopt.py b/sima/utils/startopt.py index f3499d1..bfee3f1 100644 --- a/sima/utils/startopt.py +++ b/sima/utils/startopt.py @@ -124,7 +124,7 @@ class StartOpt: def __init__(self, script_info,): self.parser = None self.info = dict(script_info) - self.options = dict() + self.options = {} self.main() def declare_opts(self): @@ -143,14 +143,14 @@ class StartOpt: opt_names = opt.pop('sw') self.parser.add_argument(*opt_names, **opt) # Add sub commands - sp = self.parser.add_subparsers( + spa = self.parser.add_subparsers( title=f'{self.info["prog"]} commands as positional arguments', description=f"""Use them after optionnal arguments.\n"{self.info["prog"]} command -h" for more info.""", metavar='', dest='command') for cmd in CMDS: helpmsg = cmd.pop('help') cmd, args = cmd.popitem() - _ = sp.add_parser(cmd, description=helpmsg, help=helpmsg) + _ = spa.add_parser(cmd, description=helpmsg, help=helpmsg) for arg in args: name = arg.pop('name', None) if name: diff --git a/sima/utils/utils.py b/sima/utils/utils.py index cc81f61..c148892 100644 --- a/sima/utils/utils.py +++ b/sima/utils/utils.py @@ -131,16 +131,16 @@ class Wfile(FileAction): """ def checks(self): if isdir(self._file): - self.parser.error('need a file not a directory: {}'.format(self._file)) + self.parser.error(f'need a file not a directory: {self._file}') if not exists(self._dir): - self.parser.error('directory does not exist: {0}'.format(self._dir)) + self.parser.error(f'directory does not exist: {self._dir}') if not exists(self._file): # Is parent directory writable then if not access(self._dir, W_OK): - self.parser.error('no write access to "{0}"'.format(self._dir)) + self.parser.error(f'no write access to "{self._dir}"') else: if not access(self._file, W_OK): - self.parser.error('no write access to "{0}"'.format(self._file)) + self.parser.error(f'no write access to "{self._file}"') class Rfile(FileAction): @@ -149,11 +149,11 @@ class Rfile(FileAction): """ def checks(self): if not exists(self._file): - self.parser.error('file does not exist: {0}'.format(self._file)) + self.parser.error(f'file does not exist: {self._file}') if not isfile(self._file): - self.parser.error('not a file: {0}'.format(self._file)) + self.parser.error(f'not a file: {self._file}') if not access(self._file, R_OK): - self.parser.error('no read access to "{0}"'.format(self._file)) + self.parser.error(f'no read access to "{self._file}"') class Wdir(FileAction): @@ -162,11 +162,11 @@ class Wdir(FileAction): """ def checks(self): if not exists(self._file): - self.parser.error('directory does not exist: {0}'.format(self._file)) + self.parser.error(f'directory does not exist: {self._file}') if not isdir(self._file): - self.parser.error('not a directory: {0}'.format(self._file)) + self.parser.error(f'not a directory: {self._file}') if not access(self._file, W_OK): - self.parser.error('no write access to "{0}"'.format(self._file)) + self.parser.error(f'no write access to "{self._file}"') class Throttle: