From fcd4fe330d4a51e1de3990a2708b249631d0e1be Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 27 Oct 2023 22:01:18 -0700 Subject: [PATCH] ruff: Fix UP031 Use format specifiers instead of percent format. Signed-off-by: Anders Kaseorg --- .../bridge_with_irc/irc_mirror_backend.py | 3 +- .../bridge_with_matrix/matrix_bridge.py | 9 +++-- .../codebase/zulip_codebase_mirror | 8 ++--- zulip/integrations/zephyr/check-mirroring | 3 +- .../zephyr/zephyr_mirror_backend.py | 34 ++++++++----------- zulip/zulip/__init__.py | 6 ++-- zulip/zulip/cli.py | 5 +-- zulip/zulip/send.py | 5 +-- zulip_bots/zulip_bots/bots/giphy/giphy.py | 6 ++-- zulip_bots/zulip_bots/bots/youtube/youtube.py | 3 +- zulip_bots/zulip_bots/run.py | 5 ++- 11 files changed, 37 insertions(+), 50 deletions(-) diff --git a/zulip/integrations/bridge_with_irc/irc_mirror_backend.py b/zulip/integrations/bridge_with_irc/irc_mirror_backend.py index a3d2fd7b..fb9e26b3 100644 --- a/zulip/integrations/bridge_with_irc/irc_mirror_backend.py +++ b/zulip/integrations/bridge_with_irc/irc_mirror_backend.py @@ -51,8 +51,7 @@ class IRCBot(irc.bot.SingleServerIRCBot): subs = [s["name"] for s in resp["subscriptions"]] if self.stream not in subs: print( - "The bot is not yet subscribed to stream '%s'. Please subscribe the bot to the stream first." - % (self.stream,) + f"The bot is not yet subscribed to stream '{self.stream}'. Please subscribe the bot to the stream first." ) exit(1) diff --git a/zulip/integrations/bridge_with_matrix/matrix_bridge.py b/zulip/integrations/bridge_with_matrix/matrix_bridge.py index 6b7e68db..3f950e86 100644 --- a/zulip/integrations/bridge_with_matrix/matrix_bridge.py +++ b/zulip/integrations/bridge_with_matrix/matrix_bridge.py @@ -91,7 +91,7 @@ class MatrixToZulip: return matrix_to_zulip async def _matrix_to_zulip(self, room: nio.MatrixRoom, event: nio.Event) -> None: - logging.debug("_matrix_to_zulip; room %s, event: %s" % (str(room.room_id), str(event))) + logging.debug(f"_matrix_to_zulip; room {str(room.room_id)}, event: {str(event)}") # We do this to identify the messages generated from Zulip -> Matrix # and we make sure we don't forward it again to the Zulip stream. @@ -253,7 +253,7 @@ class ZulipToMatrix: raise Bridge_FatalMatrixException(str(result)) def _zulip_to_matrix(self, msg: Dict[str, Any]) -> None: - logging.debug("_zulip_to_matrix; msg: %s" % (str(msg),)) + logging.debug(f"_zulip_to_matrix; msg: {str(msg)}") room_id: Optional[str] = self.get_matrix_room_for_zulip_message(msg) if room_id is None: @@ -485,8 +485,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: if section.startswith("additional_bridge"): if section_keys != bridge_key_set: raise Bridge_ConfigException( - "Please ensure the bridge configuration section %s contain the following keys: %s." - % (section, str(bridge_key_set)) + f"Please ensure the bridge configuration section {section} contain the following keys: {str(bridge_key_set)}." ) zulip_target = (section_config["stream"], section_config["topic"]) @@ -519,7 +518,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: for key in zulip_bridge_key_set: first_bridge[key] = section_config[key] else: - logging.warning("Unknown section %s" % (section,)) + logging.warning(f"Unknown section {section}") # Add the "first_bridge" to the bridges. zulip_target = (first_bridge["stream"], first_bridge["topic"]) diff --git a/zulip/integrations/codebase/zulip_codebase_mirror b/zulip/integrations/codebase/zulip_codebase_mirror index 771282f7..ead29da1 100755 --- a/zulip/integrations/codebase/zulip_codebase_mirror +++ b/zulip/integrations/codebase/zulip_codebase_mirror @@ -147,10 +147,7 @@ def handle_event(event: Dict[str, Any]) -> None: if assignee is None: assignee = "no one" subject = f"#{num}: {name}" - content = ( - """%s created a new ticket [#%s](%s) priority **%s** assigned to %s:\n\n~~~ quote\n %s""" - % (actor_name, num, url, priority, assignee, name) - ) + content = f"""{actor_name} created a new ticket [#{num}]({url}) priority **{priority}** assigned to {assignee}:\n\n~~~ quote\n {name}""" elif event_type == "ticketing_note": stream = config.ZULIP_TICKETS_STREAM_NAME @@ -225,8 +222,7 @@ def handle_event(event: Dict[str, Any]) -> None: ) end_ref_url = make_url(f"projects/{project_link}/repositories/{repo_link}/commit/{end_ref}") between_url = make_url( - "projects/%s/repositories/%s/compare/%s...%s" - % (project_link, repo_link, start_ref, end_ref) + f"projects/{project_link}/repositories/{repo_link}/compare/{start_ref}...{end_ref}" ) subject = f"Deployment to {environment}" diff --git a/zulip/integrations/zephyr/check-mirroring b/zulip/integrations/zephyr/check-mirroring index d5f9dff5..63e715b9 100755 --- a/zulip/integrations/zephyr/check-mirroring +++ b/zulip/integrations/zephyr/check-mirroring @@ -250,8 +250,7 @@ for key, (stream, test) in zhkeys.items(): server_failure_again = send_zephyr(zwrite_args, str(new_key)) if server_failure_again: logging.error( - "Zephyr server failure twice in a row on keys %s and %s! Aborting." - % (key, new_key) + f"Zephyr server failure twice in a row on keys {key} and {new_key}! Aborting." ) print_status_and_exit(1) else: diff --git a/zulip/integrations/zephyr/zephyr_mirror_backend.py b/zulip/integrations/zephyr/zephyr_mirror_backend.py index 79b12d03..a0f09dde 100755 --- a/zulip/integrations/zephyr/zephyr_mirror_backend.py +++ b/zulip/integrations/zephyr/zephyr_mirror_backend.py @@ -669,8 +669,9 @@ def zephyr_to_zulip(options: optparse.Values) -> None: if "instance" in zeph: zeph["subject"] = zeph["instance"] logger.info( - "sending saved message to %s from %s..." - % (zeph.get("stream", zeph.get("recipient")), zeph["sender"]) + "sending saved message to {} from {}...".format( + zeph.get("stream", zeph.get("recipient")), zeph["sender"] + ) ) send_zulip(zulip_client, zeph) except Exception: @@ -831,7 +832,7 @@ Feedback button or at support@zulip.com.""" if result is None: send_error_zulip( zulip_client, - """%s + f"""{support_heading} Your Zulip-Zephyr mirror bot was unable to forward that last message \ from Zulip to Zephyr because you were sending to a zcrypted Zephyr \ @@ -839,8 +840,7 @@ class and your mirroring bot does not have access to the relevant \ key (perhaps because your AFS tokens expired). That means that while \ Zulip users (like you) received it, Zephyr users did not. -%s""" - % (support_heading, support_closing), +{support_closing}""", ) return @@ -858,15 +858,14 @@ Zulip users (like you) received it, Zephyr users did not. elif code == 0: send_error_zulip( zulip_client, - """%s + f"""{support_heading} Your last message was successfully mirrored to zephyr, but zwrite \ returned the following warning: -%s +{stderr} -%s""" - % (support_heading, stderr, support_closing), +{support_closing}""", ) return elif code != 0 and ( @@ -881,7 +880,7 @@ returned the following warning: return send_error_zulip( zulip_client, - """%s + f"""{support_heading} Your last message was forwarded from Zulip to Zephyr unauthenticated, \ because your Kerberos tickets have expired. It was sent successfully, \ @@ -889,8 +888,7 @@ but please renew your Kerberos tickets in the screen session where you \ are running the Zulip-Zephyr mirroring bot, so we can send \ authenticated Zephyr messages for you again. -%s""" - % (support_heading, support_closing), +{support_closing}""", ) return @@ -899,16 +897,15 @@ authenticated Zephyr messages for you again. # but regardless, we should just notify the user. send_error_zulip( zulip_client, - """%s + f"""{support_heading} Your Zulip-Zephyr mirror bot was unable to forward that last message \ from Zulip to Zephyr. That means that while Zulip users (like you) \ received it, Zephyr users did not. The error message from zwrite was: -%s +{stderr} -%s""" - % (support_heading, stderr, support_closing), +{support_closing}""", ) return @@ -1279,11 +1276,10 @@ if __name__ == "__main__": "\n" + "\n".join( textwrap.wrap( - """\ + f"""\ Could not find API key file. -You need to either place your api key file at %s, +You need to either place your api key file at {options.api_key_file}, or specify the --api-key-file option.""" - % (options.api_key_file,) ) ) ) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 212a7d4c..6f8a7c3f 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -496,8 +496,7 @@ class Client: if client_cert is None: if client_cert_key is not None: raise ConfigNotFoundError( - "client cert key '%s' specified, but no client cert public part provided" - % (client_cert_key,) + f"client cert key '{client_cert_key}' specified, but no client cert public part provided" ) else: # we have a client cert if not os.path.isfile(client_cert): @@ -609,8 +608,7 @@ class Client: if self.verbose: if not query_state["had_error_retry"]: sys.stdout.write( - "zulip API(%s): connection error%s -- retrying." - % ( + "zulip API({}): connection error{} -- retrying.".format( url.split(API_VERSTRING, 2)[0], error_string, ) diff --git a/zulip/zulip/cli.py b/zulip/zulip/cli.py index a1ec761a..632d84cf 100755 --- a/zulip/zulip/cli.py +++ b/zulip/zulip/cli.py @@ -84,8 +84,9 @@ def send_message(recipients: List[str], stream: str, subject: str, message: str) if message_data["type"] == "stream": log.info( - 'Sending message to stream "%s", subject "%s"... ' - % (message_data["to"], message_data["subject"]) + 'Sending message to stream "{}", subject "{}"... '.format( + message_data["to"], message_data["subject"] + ) ) else: log.info("Sending message to %s... " % message_data["to"]) diff --git a/zulip/zulip/send.py b/zulip/zulip/send.py index fc580483..d7fe85a7 100755 --- a/zulip/zulip/send.py +++ b/zulip/zulip/send.py @@ -18,8 +18,9 @@ def do_send_message(client: zulip.Client, message_data: Dict[str, Any]) -> bool: if message_data["type"] == "stream": log.info( - 'Sending message to stream "%s", subject "%s"... ' - % (message_data["to"], message_data["subject"]) + 'Sending message to stream "{}", subject "{}"... '.format( + message_data["to"], message_data["subject"] + ) ) else: log.info("Sending message to {}... ".format(message_data["to"])) diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.py b/zulip_bots/zulip_bots/bots/giphy/giphy.py index a0a5a421..3087072e 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.py @@ -96,10 +96,10 @@ def get_bot_giphy_response( "let's try again later! :grin:" ) except GiphyNoResultException: - return 'Sorry, I don\'t have a GIF for "%s"! ' ":astonished:" % (keyword,) + return f'Sorry, I don\'t have a GIF for "{keyword}"! ' ":astonished:" return ( - "[Click to enlarge](%s)" - "[](/static/images/interactive-bot/giphy/powered-by-giphy.png)" % (gif_url,) + f"[Click to enlarge]({gif_url})" + "[](/static/images/interactive-bot/giphy/powered-by-giphy.png)" ) diff --git a/zulip_bots/zulip_bots/bots/youtube/youtube.py b/zulip_bots/zulip_bots/bots/youtube/youtube.py index 646fbf76..b65cda7c 100644 --- a/zulip_bots/zulip_bots/bots/youtube/youtube.py +++ b/zulip_bots/zulip_bots/bots/youtube/youtube.py @@ -123,8 +123,7 @@ def get_bot_response( elif len(video_list) == 1: return ( reply - + "\n%s - [Watch now](https://www.youtube.com/watch?v=%s)" - % (video_list[0][0], video_list[0][1]) + + f"\n{video_list[0][0]} - [Watch now](https://www.youtube.com/watch?v={video_list[0][1]})" ).strip() for title, id in video_list: diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index e2692231..d5856321 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -103,12 +103,11 @@ def exit_gracefully_if_bot_config_file_does_not_exist(bot_config_file: Optional[ if not os.path.exists(bot_config_file): print( - """ - ERROR: %s does not exist. + f""" + ERROR: {bot_config_file} does not exist. You probably just specified the wrong file location here. """ - % (bot_config_file,) ) sys.exit(1)