From 2f28be09be467c505d4b52d96bc5638def8fbb10 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 27 Dec 2021 22:45:57 -0800 Subject: [PATCH] convert %-formatted strings with args in logging.* calls to f-strings --- activitypub.py | 18 +++++++----------- common.py | 7 +++---- models.py | 6 +++--- redirect.py | 6 +++--- webfinger.py | 2 +- webmention.py | 2 +- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/activitypub.py b/activitypub.py index 687cb79..9a7e343 100644 --- a/activitypub.py +++ b/activitypub.py @@ -49,8 +49,7 @@ def send(activity, inbox_url, user_domain): Returns: requests.Response """ - logging.info('Sending AP request from %s: %s', user_domain, - json_dumps(activity, indent=2)) + logging.info('Sending AP request from {user_domain}: {json_dumps(activity, indent=2)}') # prepare HTTP Signature (required by Mastodon) # https://w3c.github.io/activitypub/#authorization @@ -86,15 +85,12 @@ def actor(domain): if tld in common.TLD_BLOCKLIST: error('', status=404) - mf2 = util.fetch_mf2('http://%s/' % domain, gateway=True, - headers=common.HEADERS) - # logging.info('Parsed mf2 for %s: %s', resp.url, json_dumps(mf2, indent=2)) + mf2 = util.fetch_mf2(f'http://{domain}/', gateway=True, headers=common.HEADERS) hcard = mf2util.representative_hcard(mf2, mf2['url']) - logging.info('Representative h-card: %s', json_dumps(hcard, indent=2)) + logging.info(f'Representative h-card: {json_dumps(hcard, indent=2)}') if not hcard: - error("""\ -Coul find a representative h-card (http://microformats.org/wiki/representative-hcard-parsing) on %s""" % mf2['url']) + error(f"Couldn't find a representative h-card (http://microformats.org/wiki/representative-hcard-parsing) on {mf2['url']}") key = MagicKey.get_or_create(domain) obj = common.postprocess_as2( @@ -106,7 +102,7 @@ Coul find a representative h-card (http://microformats.org/wiki/representative-h 'following': f'{request.host_url}{domain}/following', 'followers': f'{request.host_url}{domain}/followers', }) - logging.info('Returning: %s', json_dumps(obj, indent=2)) + logging.info(f'Returning: {json_dumps(obj, indent=2)}') return (obj, { 'Content-Type': common.CONTENT_TYPE_AS2, @@ -248,10 +244,10 @@ def undo_follow(undo_unwrapped): user_domain = util.domain_from_link(followee) follower_obj = Follower.get_by_id(Follower._id(user_domain, follower)) if follower_obj: - logging.info('Marking %s as inactive' % follower_obj.key) + logging.info(f'Marking {follower_obj.key} as inactive') follower_obj.status = 'inactive' follower_obj.put() else: - logging.warning('No Follower found for %s %s', user_domain, follower) + logging.warning(f'No Follower found for {user_domain} {follower}') # TODO send webmention with 410 of u-follow diff --git a/common.py b/common.py index 32dbdae..1509b83 100644 --- a/common.py +++ b/common.py @@ -78,7 +78,7 @@ def _requests_fn(fn, url, parse_json=False, **kwargs): kwargs.setdefault('headers', {}).update(HEADERS) resp = fn(url, gateway=True, **kwargs) - logging.info('Got %s headers:%s', resp.status_code, resp.headers) + logging.info(f'Got {resp.status_code} headers: {resp.headers}') type = content_type(resp) if (type and type != 'text/html' and (type.startswith('text/') or type.endswith('+json') or type.endswith('/json'))): @@ -192,8 +192,7 @@ def send_webmentions(activity_wrapped, proxy=None, **response_props): errors = [] # stores (code, body) tuples for target in targets: if util.domain_from_link(target) == util.domain_from_link(source): - logging.info('Skipping same-domain webmention from %s to %s', - source, target) + logging.info(f'Skipping same-domain webmention from {source} to {target}') continue response = Response(source=source, target=target, direction='in', @@ -202,7 +201,7 @@ def send_webmentions(activity_wrapped, proxy=None, **response_props): wm_source = (response.proxy_url() if verb in ('follow', 'like', 'share') or proxy else source) - logging.info('Sending webmention from %s to %s', wm_source, target) + logging.info(f'Sending webmention from {wm_source} to {target}') try: endpoint = webmention.discover(target, headers=HEADERS).endpoint diff --git a/models.py b/models.py index d89a4e7..469cd8d 100644 --- a/models.py +++ b/models.py @@ -87,12 +87,12 @@ class Response(StringIdModel): if source and target: assert 'id' not in kwargs kwargs['id'] = self._id(source, target) - logging.info('Response id (source target): %s', kwargs['id']) + logging.info(f"Response id (source target): {kwargs['id']}") super(Response, self).__init__(**kwargs) @classmethod def get_or_create(cls, source=None, target=None, **kwargs): - logging.info('Response source target: %s %s', source, target) + logging.info(f'Response source target: {source} {target}') return cls.get_or_insert(cls._id(source, target), **kwargs) def source(self): @@ -149,6 +149,6 @@ class Follower(StringIdModel): @classmethod def get_or_create(cls, user_domain, follower_id, **kwargs): - logging.info('new Follower for %s %s', user_domain, follower_id) + logging.info(f'new Follower for {user_domain} {follower_id}') return cls.get_or_insert(cls._id(user_domain, follower_id), **kwargs) diff --git a/redirect.py b/redirect.py index 771a144..01992c1 100644 --- a/redirect.py +++ b/redirect.py @@ -62,7 +62,7 @@ def redir(to): return convert_to_as2(to) # redirect - logging.info('redirecting to %s', to) + logging.info(f'redirecting to {to}') return redirect(to, code=301) @@ -74,10 +74,10 @@ def convert_to_as2(url): """ mf2 = util.fetch_mf2(url) entry = mf2util.find_first_entry(mf2, ['h-entry']) - logging.info('Parsed mf2 for %s: %s', mf2['url'], json_dumps(entry, indent=2)) + logging.info(f"Parsed mf2 for {mf2['url']}: {json_dumps(entry, indent=2)}") obj = common.postprocess_as2(as2.from_as1(microformats2.json_to_object(entry))) - logging.info('Returning: %s', json_dumps(obj, indent=2)) + logging.info(f'Returning: {json_dumps(obj, indent=2)}') return obj, { 'Content-Type': common.CONTENT_TYPE_AS2, diff --git a/webfinger.py b/webfinger.py index a4b29cb..858880f 100644 --- a/webfinger.py +++ b/webfinger.py @@ -48,7 +48,7 @@ class User(flask_util.XrdOrJrd): resp = common.requests_get(candidate) parsed = util.parse_html(resp) mf2 = util.parse_mf2(parsed, url=resp.url) - # logging.debug('Parsed mf2 for %s: %s', resp.url, json_dumps(mf2, indent=2)) + # logging.debug(f'Parsed mf2 for {resp.url}: {json_dumps(mf2, indent=2)}') hcard = mf2util.representative_hcard(mf2, resp.url) if hcard: logging.info(f'Representative h-card: {json_dumps(hcard, indent=2)}') diff --git a/webmention.py b/webmention.py index 0abde7c..4929616 100644 --- a/webmention.py +++ b/webmention.py @@ -48,7 +48,7 @@ class Webmention(View): self.source_domain = urllib.parse.urlparse(self.source_url).netloc.split(':')[0] self.source_mf2 = util.parse_mf2(source_resp) - # logging.debug('Parsed mf2 for %s: %s', source_resp.url, json_dumps(self.source_mf2 indent=2)) + # logging.debug(f'Parsed mf2 for {source_resp.url} : {json_dumps(self.source_mf2 indent=2)}') # check for backlink to bridgy fed (for webmention spec and to confirm # source's intent to federate to mastodon)