ruff: Fix UP032 Use f-string instead of format
call.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
a49add3d02
commit
ddccf0eda3
24 changed files with 88 additions and 187 deletions
|
@ -55,11 +55,11 @@ def pack(options: argparse.Namespace) -> None:
|
||||||
zip_file.write(options.config, "zuliprc")
|
zip_file.write(options.config, "zuliprc")
|
||||||
# Pack the config file for the botfarm.
|
# Pack the config file for the botfarm.
|
||||||
bot_config = textwrap.dedent(
|
bot_config = textwrap.dedent(
|
||||||
"""\
|
f"""\
|
||||||
[deploy]
|
[deploy]
|
||||||
bot={}
|
bot={options.main}
|
||||||
zuliprc=zuliprc
|
zuliprc=zuliprc
|
||||||
""".format(options.main)
|
"""
|
||||||
)
|
)
|
||||||
zip_file.writestr("config.ini", bot_config)
|
zip_file.writestr("config.ini", bot_config)
|
||||||
zip_file.close()
|
zip_file.close()
|
||||||
|
|
|
@ -58,12 +58,10 @@ the Python version this command is executed with."""
|
||||||
return_code = subprocess.call([options.python_interpreter, "-m", "venv", venv_dir])
|
return_code = subprocess.call([options.python_interpreter, "-m", "venv", venv_dir])
|
||||||
except OSError:
|
except OSError:
|
||||||
print(
|
print(
|
||||||
"{red}Installation with venv failed. Probable errors are: "
|
f"{red}Installation with venv failed. Probable errors are: "
|
||||||
"You are on Ubuntu and you haven't installed python3-venv,"
|
"You are on Ubuntu and you haven't installed python3-venv,"
|
||||||
"or you are running an unsupported python version"
|
"or you are running an unsupported python version"
|
||||||
"or python is not installed properly{end_format}".format(
|
f"or python is not installed properly{end_format}"
|
||||||
red=red, end_format=end_format
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -47,7 +47,7 @@ if __name__ == "__main__":
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
print(
|
print(
|
||||||
"You have unsatisfied dependencies. Install all missing dependencies with "
|
"You have unsatisfied dependencies. Install all missing dependencies with "
|
||||||
"{} --provision".format(sys.argv[0])
|
f"{sys.argv[0]} --provision"
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
@ -118,11 +118,7 @@ class MatrixBridgeScriptTests(TestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output_lines,
|
output_lines,
|
||||||
[
|
[f"Wrote sample configuration to '{path}' using zuliprc file '{zuliprc_path}'"],
|
||||||
"Wrote sample configuration to '{}' using zuliprc file '{}'".format(
|
|
||||||
path, zuliprc_path
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(path) as sample_file:
|
with open(path) as sample_file:
|
||||||
|
@ -143,11 +139,7 @@ class MatrixBridgeScriptTests(TestCase):
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
output_lines,
|
output_lines,
|
||||||
[
|
[f"Could not write sample config: Zuliprc file '{zuliprc_path}' does not exist."],
|
||||||
"Could not write sample config: Zuliprc file '{}' does not exist.".format(
|
|
||||||
zuliprc_path
|
|
||||||
)
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_parse_multiple_bridges(self) -> None:
|
def test_parse_multiple_bridges(self) -> None:
|
||||||
|
|
|
@ -173,10 +173,7 @@ def handle_event(event: Dict[str, Any]) -> None:
|
||||||
|
|
||||||
if "status_id" in changes:
|
if "status_id" in changes:
|
||||||
status_change = changes.get("status_id")
|
status_change = changes.get("status_id")
|
||||||
content += "Status changed from **{}** to **{}**\n\n".format(
|
content += f"Status changed from **{status_change[0]}** to **{status_change[1]}**\n\n"
|
||||||
status_change[0],
|
|
||||||
status_change[1],
|
|
||||||
)
|
|
||||||
elif event_type == "ticketing_milestone":
|
elif event_type == "ticketing_milestone":
|
||||||
stream = config.ZULIP_TICKETS_STREAM_NAME
|
stream = config.ZULIP_TICKETS_STREAM_NAME
|
||||||
|
|
||||||
|
@ -199,12 +196,7 @@ def handle_event(event: Dict[str, Any]) -> None:
|
||||||
url = make_url(f"projects/{project_link}/repositories/{repo_link}/commit/{commit}")
|
url = make_url(f"projects/{project_link}/repositories/{repo_link}/commit/{commit}")
|
||||||
|
|
||||||
subject = f"{actor_name} commented on {commit}"
|
subject = f"{actor_name} commented on {commit}"
|
||||||
content = "{} commented on [{}]({}):\n\n~~~ quote\n{}".format(
|
content = f"{actor_name} commented on [{commit}]({url}):\n\n~~~ quote\n{comment}"
|
||||||
actor_name,
|
|
||||||
commit,
|
|
||||||
url,
|
|
||||||
comment,
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
# Otherwise, this is a Discussion item, and handle it
|
# Otherwise, this is a Discussion item, and handle it
|
||||||
subj = raw_props.get("subject")
|
subj = raw_props.get("subject")
|
||||||
|
|
|
@ -30,18 +30,12 @@ def format_summary_line(
|
||||||
|
|
||||||
if web_url:
|
if web_url:
|
||||||
shortlog_base_url = web_url.rstrip("/") + "/shortlog/"
|
shortlog_base_url = web_url.rstrip("/") + "/shortlog/"
|
||||||
summary_url = "{shortlog}{tip}?revcount={revcount}".format(
|
summary_url = f"{shortlog_base_url}{tip - 1}?revcount={revcount}"
|
||||||
shortlog=shortlog_base_url, tip=tip - 1, revcount=revcount
|
formatted_commit_count = f"[{revcount} commit{plural}]({summary_url})"
|
||||||
)
|
|
||||||
formatted_commit_count = "[{revcount} commit{s}]({url})".format(
|
|
||||||
revcount=revcount, s=plural, url=summary_url
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
formatted_commit_count = f"{revcount} commit{plural}"
|
formatted_commit_count = f"{revcount} commit{plural}"
|
||||||
|
|
||||||
return "**{user}** pushed {commits} to **{branch}** (`{tip}:{node}`):\n\n".format(
|
return f"**{user}** pushed {formatted_commit_count} to **{branch}** (`{tip}:{node[:12]}`):\n\n"
|
||||||
user=user, commits=formatted_commit_count, branch=branch, tip=tip, node=node[:12]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def format_commit_lines(web_url: str, repo: repo, base: int, tip: int) -> str:
|
def format_commit_lines(web_url: str, repo: repo, base: int, tip: int) -> str:
|
||||||
|
|
|
@ -77,7 +77,7 @@ if hasattr(config, "P4_WEB"):
|
||||||
|
|
||||||
if p4web is not None:
|
if p4web is not None:
|
||||||
# linkify the change number
|
# linkify the change number
|
||||||
change = "[{change}]({p4web}/{change}?ac=10)".format(p4web=p4web, change=change)
|
change = f"[{change}]({p4web}/{change}?ac=10)"
|
||||||
|
|
||||||
message = """**{user}** committed revision @{change} to `{path}`.
|
message = """**{user}** committed revision @{change} to `{path}`.
|
||||||
|
|
||||||
|
|
|
@ -177,12 +177,7 @@ def send_zulip(entry: Any, feed_name: str) -> Dict[str, Any]:
|
||||||
if opts.unwrap:
|
if opts.unwrap:
|
||||||
body = unwrap_text(body)
|
body = unwrap_text(body)
|
||||||
|
|
||||||
content = "**[{}]({})**\n{}\n{}".format(
|
content = f"**[{entry.title}]({entry.link})**\n{strip_tags(body)}\n{entry.link}"
|
||||||
entry.title,
|
|
||||||
entry.link,
|
|
||||||
strip_tags(body),
|
|
||||||
entry.link,
|
|
||||||
)
|
|
||||||
|
|
||||||
if opts.math:
|
if opts.math:
|
||||||
content = content.replace("$", "$$")
|
content = content.replace("$", "$$")
|
||||||
|
|
|
@ -793,9 +793,7 @@ Feedback button or at support@zulip.com."""
|
||||||
# Forward messages sent to '(instance "WHITESPACE")' back to the
|
# Forward messages sent to '(instance "WHITESPACE")' back to the
|
||||||
# appropriate WHITESPACE instance for bidirectional mirroring
|
# appropriate WHITESPACE instance for bidirectional mirroring
|
||||||
instance = match_whitespace_instance.group(1)
|
instance = match_whitespace_instance.group(1)
|
||||||
elif instance == f"instance {zephyr_class}" or instance == "test instance {}".format(
|
elif instance == f"instance {zephyr_class}" or instance == f"test instance {zephyr_class}":
|
||||||
zephyr_class,
|
|
||||||
):
|
|
||||||
# Forward messages to e.g. -c -i white-magic back from the
|
# Forward messages to e.g. -c -i white-magic back from the
|
||||||
# place we forward them to
|
# place we forward them to
|
||||||
if instance.startswith("test"):
|
if instance.startswith("test"):
|
||||||
|
|
|
@ -41,9 +41,7 @@ Zulip API configuration:
|
||||||
expanded_test_path = os.path.abspath(os.path.expanduser(test_path))
|
expanded_test_path = os.path.abspath(os.path.expanduser(test_path))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(cm.exception),
|
str(cm.exception),
|
||||||
"api_key or email not specified and " "file {} does not exist".format(
|
"api_key or email not specified and " f"file {expanded_test_path} does not exist",
|
||||||
expanded_test_path
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -417,8 +417,8 @@ class Client:
|
||||||
if insecure is None:
|
if insecure is None:
|
||||||
raise ZulipError(
|
raise ZulipError(
|
||||||
"The ZULIP_ALLOW_INSECURE environment "
|
"The ZULIP_ALLOW_INSECURE environment "
|
||||||
"variable is set to '{}', it must be "
|
f"variable is set to '{insecure_setting}', it must be "
|
||||||
"'true' or 'false'".format(insecure_setting)
|
"'true' or 'false'"
|
||||||
)
|
)
|
||||||
if config_file is None:
|
if config_file is None:
|
||||||
config_file = get_default_config_filename()
|
config_file = get_default_config_filename()
|
||||||
|
@ -448,10 +448,8 @@ class Client:
|
||||||
|
|
||||||
if insecure is None:
|
if insecure is None:
|
||||||
raise ZulipError(
|
raise ZulipError(
|
||||||
"insecure is set to '{}', it must be "
|
f"insecure is set to '{insecure_setting}', it must be "
|
||||||
"'true' or 'false' if it is used in {}".format(
|
f"'true' or 'false' if it is used in {config_file}"
|
||||||
insecure_setting, config_file
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
elif None in (api_key, email):
|
elif None in (api_key, email):
|
||||||
|
@ -561,11 +559,7 @@ class Client:
|
||||||
elif vendor == "Darwin":
|
elif vendor == "Darwin":
|
||||||
vendor_version = platform.mac_ver()[0]
|
vendor_version = platform.mac_ver()[0]
|
||||||
|
|
||||||
return "{client_name} ({vendor}; {vendor_version})".format(
|
return f"{self.client_name} ({vendor}; {vendor_version})"
|
||||||
client_name=self.client_name,
|
|
||||||
vendor=vendor,
|
|
||||||
vendor_version=vendor_version,
|
|
||||||
)
|
|
||||||
|
|
||||||
def do_api_query(
|
def do_api_query(
|
||||||
self,
|
self,
|
||||||
|
|
|
@ -26,9 +26,7 @@ def get_beeminder_response(message_content: str, config_info: Dict[str, str]) ->
|
||||||
if message_content == "" or message_content == "help":
|
if message_content == "" or message_content == "help":
|
||||||
return help_message
|
return help_message
|
||||||
|
|
||||||
url = "https://www.beeminder.com/api/v1/users/{}/goals/{}/datapoints.json".format(
|
url = f"https://www.beeminder.com/api/v1/users/{username}/goals/{goalname}/datapoints.json"
|
||||||
username, goalname
|
|
||||||
)
|
|
||||||
message_pieces = message_content.split(",")
|
message_pieces = message_content.split(",")
|
||||||
for i in range(len(message_pieces)):
|
for i in range(len(message_pieces)):
|
||||||
message_pieces[i] = message_pieces[i].strip()
|
message_pieces[i] = message_pieces[i].strip()
|
||||||
|
@ -66,13 +64,11 @@ at syntax by: @mention-botname help"
|
||||||
if r.status_code == 401: # Handles case of invalid key and missing key
|
if r.status_code == 401: # Handles case of invalid key and missing key
|
||||||
return "Error. Check your key!"
|
return "Error. Check your key!"
|
||||||
else:
|
else:
|
||||||
return "Error occured : {}".format(
|
return f"Error occured : {r.status_code}" # Occures in case of unprocessable entity
|
||||||
r.status_code
|
|
||||||
) # Occures in case of unprocessable entity
|
|
||||||
else:
|
else:
|
||||||
datapoint_link = f"https://www.beeminder.com/{username}/{goalname}"
|
datapoint_link = f"https://www.beeminder.com/{username}/{goalname}"
|
||||||
return "[Datapoint]({}) created.".format(
|
return (
|
||||||
datapoint_link
|
f"[Datapoint]({datapoint_link}) created."
|
||||||
) # Handles the case of successful datapoint creation
|
) # Handles the case of successful datapoint creation
|
||||||
except ConnectionError as e:
|
except ConnectionError as e:
|
||||||
logging.exception(str(e))
|
logging.exception(str(e))
|
||||||
|
|
|
@ -124,9 +124,7 @@ def get_bot_converter_response(message: Dict[str, str], bot_handler: BotHandler)
|
||||||
number_res = round_to(number_res, 7)
|
number_res = round_to(number_res, 7)
|
||||||
|
|
||||||
results.append(
|
results.append(
|
||||||
"{} {} = {} {}".format(
|
f"{number} {words[convert_index + 2]} = {number_res} {words[convert_index + 3]}"
|
||||||
number, words[convert_index + 2], number_res, words[convert_index + 3]
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -27,7 +27,7 @@ class GithubHandler:
|
||||||
"To reference an issue or pull request usename mention the bot then "
|
"To reference an issue or pull request usename mention the bot then "
|
||||||
"anytime in the message type its id, for example:\n"
|
"anytime in the message type its id, for example:\n"
|
||||||
"@**Github detail** #3212 zulip#3212 zulip/zulip#3212\n"
|
"@**Github detail** #3212 zulip#3212 zulip/zulip#3212\n"
|
||||||
"The default owner is {} and the default repo is {}.".format(self.owner, self.repo)
|
f"The default owner is {self.owner} and the default repo is {self.repo}."
|
||||||
)
|
)
|
||||||
|
|
||||||
def format_message(self, details: Dict[str, Any]) -> str:
|
def format_message(self, details: Dict[str, Any]) -> str:
|
||||||
|
@ -44,10 +44,8 @@ class GithubHandler:
|
||||||
message_string = (
|
message_string = (
|
||||||
f"**[{owner}/{repo}#{number}]",
|
f"**[{owner}/{repo}#{number}]",
|
||||||
f"({link}) - {title}**\n",
|
f"({link}) - {title}**\n",
|
||||||
"Created by **[{author}](https://github.com/{author})**\n".format(author=author),
|
f"Created by **[{author}](https://github.com/{author})**\n",
|
||||||
"Status - **{status}**\n```quote\n{description}\n```".format(
|
f"Status - **{status}**\n```quote\n{description}\n```",
|
||||||
status=status, description=description
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return "".join(message_string)
|
return "".join(message_string)
|
||||||
|
|
||||||
|
@ -100,9 +98,7 @@ class GithubHandler:
|
||||||
bot_messages.append(self.format_message(details))
|
bot_messages.append(self.format_message(details))
|
||||||
else:
|
else:
|
||||||
bot_messages.append(
|
bot_messages.append(
|
||||||
"Failed to find issue/pr: {owner}/{repo}#{id}".format(
|
f"Failed to find issue/pr: {owner}/{repo}#{issue_pr.group(3)}"
|
||||||
owner=owner, repo=repo, id=issue_pr.group(3)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
bot_messages.append("Failed to detect owner and repository name.")
|
bot_messages.append("Failed to detect owner and repository name.")
|
||||||
|
|
|
@ -124,26 +124,15 @@ def format_incident_for_widget(ticket_id: str, incident: Dict[str, Any]) -> str:
|
||||||
|
|
||||||
|
|
||||||
def format_incident_for_markdown(ticket_id: str, incident: Dict[str, Any]) -> str:
|
def format_incident_for_markdown(ticket_id: str, incident: Dict[str, Any]) -> str:
|
||||||
answer_list = "\n".join(
|
answer_list = "\n".join(f"* **{code}** {ANSWERS[code]}" for code in "1234")
|
||||||
"* **{code}** {answer}".format(
|
|
||||||
code=code,
|
|
||||||
answer=ANSWERS[code],
|
|
||||||
)
|
|
||||||
for code in "1234"
|
|
||||||
)
|
|
||||||
how_to_respond = f"""**reply**: answer {ticket_id} <code>"""
|
how_to_respond = f"""**reply**: answer {ticket_id} <code>"""
|
||||||
|
|
||||||
content = """
|
content = f"""
|
||||||
Incident: {incident}
|
Incident: {incident}
|
||||||
Q: {question}
|
Q: {QUESTION}
|
||||||
|
|
||||||
{answer_list}
|
{answer_list}
|
||||||
{how_to_respond}""".format(
|
{how_to_respond}"""
|
||||||
question=QUESTION,
|
|
||||||
answer_list=answer_list,
|
|
||||||
how_to_respond=how_to_respond,
|
|
||||||
incident=incident,
|
|
||||||
)
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -242,24 +242,14 @@ class JiraHandler:
|
||||||
response = "Oh no! Jira raised an error:\n > " + ", ".join(errors)
|
response = "Oh no! Jira raised an error:\n > " + ", ".join(errors)
|
||||||
else:
|
else:
|
||||||
response = (
|
response = (
|
||||||
"**Issue *[{}]({})*: {}**\n\n"
|
f"**Issue *[{key}]({url})*: {summary}**\n\n"
|
||||||
" - Type: *{}*\n"
|
f" - Type: *{type_name}*\n"
|
||||||
" - Description:\n"
|
" - Description:\n"
|
||||||
" > {}\n"
|
f" > {description}\n"
|
||||||
" - Creator: *{}*\n"
|
f" - Creator: *{creator_name}*\n"
|
||||||
" - Project: *{}*\n"
|
f" - Project: *{project_name}*\n"
|
||||||
" - Priority: *{}*\n"
|
f" - Priority: *{priority_name}*\n"
|
||||||
" - Status: *{}*\n"
|
f" - Status: *{status_name}*\n"
|
||||||
).format(
|
|
||||||
key,
|
|
||||||
url,
|
|
||||||
summary,
|
|
||||||
type_name,
|
|
||||||
description,
|
|
||||||
creator_name,
|
|
||||||
project_name,
|
|
||||||
priority_name,
|
|
||||||
status_name,
|
|
||||||
)
|
)
|
||||||
elif create_match:
|
elif create_match:
|
||||||
jira_response = requests.post(
|
jira_response = requests.post(
|
||||||
|
|
|
@ -284,8 +284,8 @@ def create_room(topic_name, merels_storage):
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
return (
|
return (
|
||||||
"Failed: Cannot create an already existing game in {}. "
|
f"Failed: Cannot create an already existing game in {topic_name}. "
|
||||||
"Please finish the game first.".format(topic_name)
|
"Please finish the game first."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -309,9 +309,9 @@ def display_game(topic_name, merels_storage):
|
||||||
take = "No"
|
take = "No"
|
||||||
|
|
||||||
response += interface.graph_grid(data.grid()) + "\n"
|
response += interface.graph_grid(data.grid()) + "\n"
|
||||||
response += """Phase {}. Take mode: {}.
|
response += f"""Phase {data.get_phase()}. Take mode: {take}.
|
||||||
X taken: {}, O taken: {}.
|
X taken: {data.x_taken}, O taken: {data.o_taken}.
|
||||||
""".format(data.get_phase(), take, data.x_taken, data.o_taken)
|
"""
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -362,9 +362,7 @@ def move_man(topic_name, p1, p2, merels_storage):
|
||||||
data.hill_uid,
|
data.hill_uid,
|
||||||
data.take_mode,
|
data.take_mode,
|
||||||
)
|
)
|
||||||
return "Moved a man from ({}, {}) -> ({}, {}) for {}.".format(
|
return f"Moved a man from ({p1[0]}, {p1[1]}) -> ({p2[0]}, {p2[1]}) for {data.turn}."
|
||||||
p1[0], p1[1], p2[0], p2[1], data.turn
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
raise BadMoveException("Failed: That's not a legal move. Please try again.")
|
raise BadMoveException("Failed: That's not a legal move. Please try again.")
|
||||||
|
|
||||||
|
|
|
@ -189,24 +189,14 @@ def format_quiz_for_widget(quiz_id: str, quiz: Dict[str, Any]) -> str:
|
||||||
def format_quiz_for_markdown(quiz_id: str, quiz: Dict[str, Any]) -> str:
|
def format_quiz_for_markdown(quiz_id: str, quiz: Dict[str, Any]) -> str:
|
||||||
question = quiz["question"]
|
question = quiz["question"]
|
||||||
answers = quiz["answers"]
|
answers = quiz["answers"]
|
||||||
answer_list = "\n".join(
|
answer_list = "\n".join(f"* **{letter}** {answers[letter]}" for letter in "ABCD")
|
||||||
"* **{letter}** {answer}".format(
|
|
||||||
letter=letter,
|
|
||||||
answer=answers[letter],
|
|
||||||
)
|
|
||||||
for letter in "ABCD"
|
|
||||||
)
|
|
||||||
how_to_respond = f"""**reply**: answer {quiz_id} <letter>"""
|
how_to_respond = f"""**reply**: answer {quiz_id} <letter>"""
|
||||||
|
|
||||||
content = """
|
content = f"""
|
||||||
Q: {question}
|
Q: {question}
|
||||||
|
|
||||||
{answer_list}
|
{answer_list}
|
||||||
{how_to_respond}""".format(
|
{how_to_respond}"""
|
||||||
question=question,
|
|
||||||
answer_list=answer_list,
|
|
||||||
how_to_respond=how_to_respond,
|
|
||||||
)
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -62,11 +62,11 @@ def get_xkcd_bot_response(message: Dict[str, str], quoted_name: str) -> str:
|
||||||
|
|
||||||
commands_help = (
|
commands_help = (
|
||||||
"%s"
|
"%s"
|
||||||
"\n* `{0} help` to show this help message."
|
f"\n* `{quoted_name} help` to show this help message."
|
||||||
"\n* `{0} latest` to fetch the latest comic strip from xkcd."
|
f"\n* `{quoted_name} latest` to fetch the latest comic strip from xkcd."
|
||||||
"\n* `{0} random` to fetch a random comic strip from xkcd."
|
f"\n* `{quoted_name} random` to fetch a random comic strip from xkcd."
|
||||||
"\n* `{0} <comic id>` to fetch a comic strip based on `<comic id>` "
|
f"\n* `{quoted_name} <comic id>` to fetch a comic strip based on `<comic id>` "
|
||||||
"e.g `{0} 1234`.".format(quoted_name)
|
f"e.g `{quoted_name} 1234`."
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -80,12 +80,12 @@ class GameAdapter:
|
||||||
self.put_user_cache()
|
self.put_user_cache()
|
||||||
|
|
||||||
def help_message(self) -> str:
|
def help_message(self) -> str:
|
||||||
return """** {} Bot Help:**
|
return f"""** {self.game_name} Bot Help:**
|
||||||
*Preface all commands with @**{}***
|
*Preface all commands with @**{self.get_bot_username()}***
|
||||||
* To start a game in a stream (*recommended*), type
|
* To start a game in a stream (*recommended*), type
|
||||||
`start game`
|
`start game`
|
||||||
* To start a game against another player, type
|
* To start a game against another player, type
|
||||||
`start game with @<player-name>`{}
|
`start game with @<player-name>`{self.play_with_computer_help()}
|
||||||
* To play game with the current number of players, type
|
* To play game with the current number of players, type
|
||||||
`play game`
|
`play game`
|
||||||
* To quit a game at any time, type
|
* To quit a game at any time, type
|
||||||
|
@ -100,23 +100,18 @@ class GameAdapter:
|
||||||
`cancel game`
|
`cancel game`
|
||||||
* To see rules of this game, type
|
* To see rules of this game, type
|
||||||
`rules`
|
`rules`
|
||||||
{}""".format(
|
{self.move_help_message}"""
|
||||||
self.game_name,
|
|
||||||
self.get_bot_username(),
|
|
||||||
self.play_with_computer_help(),
|
|
||||||
self.move_help_message,
|
|
||||||
)
|
|
||||||
|
|
||||||
def help_message_single_player(self) -> str:
|
def help_message_single_player(self) -> str:
|
||||||
return """** {} Bot Help:**
|
return f"""** {self.game_name} Bot Help:**
|
||||||
*Preface all commands with @**{}***
|
*Preface all commands with @**{self.get_bot_username()}***
|
||||||
* To start a game in a stream, type
|
* To start a game in a stream, type
|
||||||
`start game`
|
`start game`
|
||||||
* To quit a game at any time, type
|
* To quit a game at any time, type
|
||||||
`quit`
|
`quit`
|
||||||
* To see rules of this game, type
|
* To see rules of this game, type
|
||||||
`rules`
|
`rules`
|
||||||
{}""".format(self.game_name, self.get_bot_username(), self.move_help_message)
|
{self.move_help_message}"""
|
||||||
|
|
||||||
def get_commands(self) -> Dict[str, str]:
|
def get_commands(self) -> Dict[str, str]:
|
||||||
action = self.help_message_single_player()
|
action = self.help_message_single_player()
|
||||||
|
@ -359,9 +354,7 @@ class GameAdapter:
|
||||||
if len(users) + 1 < self.min_players:
|
if len(users) + 1 < self.min_players:
|
||||||
self.send_reply(
|
self.send_reply(
|
||||||
message,
|
message,
|
||||||
"You must have at least {} players to play.\nGame cancelled.".format(
|
f"You must have at least {self.min_players} players to play.\nGame cancelled.",
|
||||||
self.min_players
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if len(users) + 1 > self.max_players:
|
if len(users) + 1 > self.max_players:
|
||||||
|
@ -546,11 +539,9 @@ class GameAdapter:
|
||||||
self.instances[game_id].start()
|
self.instances[game_id].start()
|
||||||
|
|
||||||
def get_formatted_game_object(self, game_id: str) -> str:
|
def get_formatted_game_object(self, game_id: str) -> str:
|
||||||
object = """> **Game `{}`**
|
object = f"""> **Game `{game_id}`**
|
||||||
> {}
|
> {self.game_name}
|
||||||
> {}/{} players""".format(
|
> {self.get_number_of_players(game_id)}/{self.max_players} players"""
|
||||||
game_id, self.game_name, self.get_number_of_players(game_id), self.max_players
|
|
||||||
)
|
|
||||||
if game_id in self.instances.keys():
|
if game_id in self.instances.keys():
|
||||||
instance = self.instances[game_id]
|
instance = self.instances[game_id]
|
||||||
if not self.is_single_player:
|
if not self.is_single_player:
|
||||||
|
@ -631,18 +622,16 @@ class GameAdapter:
|
||||||
return
|
return
|
||||||
self.send_reply(
|
self.send_reply(
|
||||||
message,
|
message,
|
||||||
"Join your game using the link below!\n\n{}".format(
|
f"Join your game using the link below!\n\n{self.get_formatted_game_object(game_id)}",
|
||||||
self.get_formatted_game_object(game_id)
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
if game["subject"] != message["subject"] or game["stream"] != message["display_recipient"]:
|
if game["subject"] != message["subject"] or game["stream"] != message["display_recipient"]:
|
||||||
if game_id not in self.pending_subject_changes:
|
if game_id not in self.pending_subject_changes:
|
||||||
self.send_reply(
|
self.send_reply(
|
||||||
message,
|
message,
|
||||||
"Your current game is not in this subject. \n\
|
f"Your current game is not in this subject. \n\
|
||||||
To move subjects, send your message again, otherwise join the game using the link below.\n\n\
|
To move subjects, send your message again, otherwise join the game using the link below.\n\n\
|
||||||
{}".format(self.get_formatted_game_object(game_id)),
|
{self.get_formatted_game_object(game_id)}",
|
||||||
)
|
)
|
||||||
self.pending_subject_changes.append(game_id)
|
self.pending_subject_changes.append(game_id)
|
||||||
return
|
return
|
||||||
|
@ -950,9 +939,7 @@ class GameInstance:
|
||||||
if not is_computer:
|
if not is_computer:
|
||||||
self.current_messages.append(
|
self.current_messages.append(
|
||||||
self.gameAdapter.gameMessageHandler.alert_move_message(
|
self.gameAdapter.gameMessageHandler.alert_move_message(
|
||||||
"**{}**".format(
|
f"**{self.gameAdapter.get_username_by_email(self.players[self.turn])}**",
|
||||||
self.gameAdapter.get_username_by_email(self.players[self.turn])
|
|
||||||
),
|
|
||||||
content,
|
content,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -973,9 +960,7 @@ class GameInstance:
|
||||||
if not is_computer:
|
if not is_computer:
|
||||||
self.current_messages.append(
|
self.current_messages.append(
|
||||||
self.gameAdapter.gameMessageHandler.alert_move_message(
|
self.gameAdapter.gameMessageHandler.alert_move_message(
|
||||||
"**{}**".format(
|
f"**{self.gameAdapter.get_username_by_email(self.players[self.turn])}**",
|
||||||
self.gameAdapter.get_username_by_email(self.players[self.turn])
|
|
||||||
),
|
|
||||||
content,
|
content,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -222,21 +222,21 @@ class ExternalBotHandler:
|
||||||
user_profile = client.get_profile()
|
user_profile = client.get_profile()
|
||||||
except ZulipError as e:
|
except ZulipError as e:
|
||||||
print(
|
print(
|
||||||
"""
|
f"""
|
||||||
ERROR: {}
|
ERROR: {e}
|
||||||
|
|
||||||
Have you not started the server?
|
Have you not started the server?
|
||||||
Or did you mis-specify the URL?
|
Or did you mis-specify the URL?
|
||||||
""".format(e)
|
"""
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if user_profile.get("result") == "error":
|
if user_profile.get("result") == "error":
|
||||||
msg = user_profile.get("msg", "unknown")
|
msg = user_profile.get("msg", "unknown")
|
||||||
print(
|
print(
|
||||||
|
f"""
|
||||||
|
ERROR: {msg}
|
||||||
"""
|
"""
|
||||||
ERROR: {}
|
|
||||||
""".format(msg)
|
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -323,18 +323,18 @@ class ExternalBotHandler:
|
||||||
|
|
||||||
if bot_name not in self.bot_config_file:
|
if bot_name not in self.bot_config_file:
|
||||||
print(
|
print(
|
||||||
"""
|
f"""
|
||||||
WARNING!
|
WARNING!
|
||||||
|
|
||||||
{} does not adhere to the
|
{self.bot_config_file} does not adhere to the
|
||||||
file naming convention, and it could be a
|
file naming convention, and it could be a
|
||||||
sign that you passed in the
|
sign that you passed in the
|
||||||
wrong third-party configuration file.
|
wrong third-party configuration file.
|
||||||
|
|
||||||
The suggested name is {}.conf
|
The suggested name is {bot_name}.conf
|
||||||
|
|
||||||
We will proceed anyway.
|
We will proceed anyway.
|
||||||
""".format(self.bot_config_file, bot_name)
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# We expect the caller to pass in None if the user does
|
# We expect the caller to pass in None if the user does
|
||||||
|
@ -368,8 +368,8 @@ class ExternalBotHandler:
|
||||||
return open(abs_filepath)
|
return open(abs_filepath)
|
||||||
else:
|
else:
|
||||||
raise PermissionError(
|
raise PermissionError(
|
||||||
'Cannot open file "{}". Bots may only access '
|
f'Cannot open file "{abs_filepath}". Bots may only access '
|
||||||
"files in their local directory.".format(abs_filepath)
|
"files in their local directory."
|
||||||
)
|
)
|
||||||
|
|
||||||
def quit(self, message: str = "") -> None:
|
def quit(self, message: str = "") -> None:
|
||||||
|
|
|
@ -19,7 +19,7 @@ def get_bot_message_handler(bot_name: str) -> Any:
|
||||||
# handler class. Eventually, we want bot's handler classes to
|
# handler class. Eventually, we want bot's handler classes to
|
||||||
# inherit from a common prototype specifying the handle_message
|
# inherit from a common prototype specifying the handle_message
|
||||||
# function.
|
# function.
|
||||||
lib_module: Any = import_module("zulip_bots.bots.{bot}.{bot}".format(bot=bot_name))
|
lib_module: Any = import_module(f"zulip_bots.bots.{bot_name}.{bot_name}")
|
||||||
return lib_module.handler_class()
|
return lib_module.handler_class()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -273,8 +273,8 @@ class BotServerTests(BotServerTestCase):
|
||||||
# load invalid file path
|
# load invalid file path
|
||||||
with self.assertRaisesRegex(
|
with self.assertRaisesRegex(
|
||||||
SystemExit,
|
SystemExit,
|
||||||
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
f'Error: Bot "{root_dir}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
||||||
"Please make sure you have set up the botserverrc file correctly.".format(root_dir),
|
"Please make sure you have set up the botserverrc file correctly.",
|
||||||
):
|
):
|
||||||
path = Path(
|
path = Path(
|
||||||
root_dir, "zulip_bots/zulip_bots/bots/{bot}.py".format(bot="helloworld")
|
root_dir, "zulip_bots/zulip_bots/bots/{bot}.py".format(bot="helloworld")
|
||||||
|
|
|
@ -90,9 +90,7 @@ def read_config_file(
|
||||||
bot_section = parser.sections()[0]
|
bot_section = parser.sections()[0]
|
||||||
bots_config[bot_name] = read_config_section(parser, bot_section)
|
bots_config[bot_name] = read_config_section(parser, bot_section)
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"First bot name in the config list was changed from '{}' to '{}'".format(
|
f"First bot name in the config list was changed from '{bot_section}' to '{bot_name}'"
|
||||||
bot_section, bot_name
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
ignored_sections = parser.sections()[1:]
|
ignored_sections = parser.sections()[1:]
|
||||||
|
|
||||||
|
@ -118,7 +116,7 @@ def load_lib_modules(available_bots: List[str]) -> Dict[str, ModuleType]:
|
||||||
if bot.endswith(".py") and os.path.isfile(bot):
|
if bot.endswith(".py") and os.path.isfile(bot):
|
||||||
lib_module = import_module_from_source(bot, "custom_bot_module")
|
lib_module = import_module_from_source(bot, "custom_bot_module")
|
||||||
else:
|
else:
|
||||||
module_name = "zulip_bots.bots.{bot}.{bot}".format(bot=bot)
|
module_name = f"zulip_bots.bots.{bot}.{bot}"
|
||||||
lib_module = import_module(module_name)
|
lib_module = import_module(module_name)
|
||||||
bots_lib_module[bot] = lib_module
|
bots_lib_module[bot] = lib_module
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
Loading…
Add table
Reference in a new issue