Skip to content

Commit fee06ff

Browse files
Switch from black to yapf + postprocessing (#1110)
* Format with black * Switch from black to yapf + postprocessing * Reformat samples * Reformat FCM sample
1 parent da5f60c commit fee06ff

File tree

20 files changed

+341
-528
lines changed

20 files changed

+341
-528
lines changed

.github/workflows/test_python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ jobs:
4545
run: pip install -r requirements.txt
4646
- name: Lint
4747
working-directory: ./Python
48-
run: black . --exclude=venv --check --diff
48+
run: python pyfmt.py --check_only --exclude "**/venv/**/*.py" **/*.py

Python/alerts-to-discord/functions/main.py

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,16 @@
1616

1717
# [START v2import]
1818
from firebase_functions import params
19-
from firebase_functions.alerts import (
20-
app_distribution_fn,
21-
crashlytics_fn,
22-
performance_fn,
23-
)
24-
19+
from firebase_functions.alerts import app_distribution_fn, crashlytics_fn, performance_fn
2520
# [END v2import]
2621

2722
import requests
2823

2924
DISCORD_WEBHOOK_URL = params.SecretParam("DISCORD_WEBHOOK_URL")
3025

3126

32-
def post_message_to_discord(
33-
bot_name: str, message_body: str, webhook_url: str
34-
) -> requests.Response:
27+
def post_message_to_discord(bot_name: str, message_body: str,
28+
webhook_url: str) -> requests.Response:
3529
"""Posts a message to Discord with Discord's Webhook API.
3630
3731
Params:
@@ -42,28 +36,24 @@ def post_message_to_discord(
4236
raise EnvironmentError(
4337
"No webhook URL found. Set the Discord Webhook URL before deploying. "
4438
"Learn more about Discord webhooks here: "
45-
"https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks"
46-
)
39+
"https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks")
4740

4841
return requests.post(
4942
url=webhook_url,
5043
json={
5144
# Here's what the Discord API supports in the payload:
5245
# https://discord.com/developers/docs/resources/webhook#execute-webhook-jsonform-params
5346
"username": bot_name,
54-
"content": message_body,
55-
},
56-
)
47+
"content": message_body
48+
})
5749

5850

5951
# [START v2Alerts]
6052
# [START v2CrashlyticsAlertTrigger]
6153
@crashlytics_fn.on_new_fatal_issue_published(secrets=["DISCORD_WEBHOOK_URL"])
62-
def post_fatal_issue_to_discord(
63-
event: crashlytics_fn.CrashlyticsNewFatalIssueEvent,
64-
) -> None:
54+
def post_fatal_issue_to_discord(event: crashlytics_fn.CrashlyticsNewFatalIssueEvent) -> None:
6555
"""Publishes a message to Discord whenever a new Crashlytics fatal issue occurs."""
66-
# [END v2CrashlyticsAlertTrigger]
56+
# [END v2CrashlyticsAlertTrigger]
6757
# [START v2CrashlyticsEventPayload]
6858
# Construct a helpful message to send to Discord.
6959
app_id = event.app_id
@@ -81,33 +71,22 @@ def post_fatal_issue_to_discord(
8171

8272
try:
8373
# [START v2SendToDiscord]
84-
response = post_message_to_discord(
85-
"Crashlytics Bot", message, DISCORD_WEBHOOK_URL.value
86-
)
74+
response = post_message_to_discord("Crashlytics Bot", message, DISCORD_WEBHOOK_URL.value)
8775
if response.ok:
88-
print(
89-
f"Posted fatal Crashlytics alert {issue.id} for {app_id} to Discord."
90-
)
76+
print(f"Posted fatal Crashlytics alert {issue.id} for {app_id} to Discord.")
9177
pprint.pp(event.data.payload)
9278
else:
9379
response.raise_for_status()
9480
# [END v2SendToDiscord]
9581
except (EnvironmentError, requests.HTTPError) as error:
96-
print(
97-
f"Unable to post fatal Crashlytics alert {issue.id} for {app_id} to Discord.",
98-
error,
99-
)
82+
print(f"Unable to post fatal Crashlytics alert {issue.id} for {app_id} to Discord.", error)
10083

10184

10285
# [START v2AppDistributionAlertTrigger]
103-
@app_distribution_fn.on_new_tester_ios_device_published(
104-
secrets=["DISCORD_WEBHOOK_URL"]
105-
)
106-
def post_new_udid_to_discord(
107-
event: app_distribution_fn.NewTesterDeviceEvent,
108-
) -> None:
86+
@app_distribution_fn.on_new_tester_ios_device_published(secrets=["DISCORD_WEBHOOK_URL"])
87+
def post_new_udid_to_discord(event: app_distribution_fn.NewTesterDeviceEvent) -> None:
10988
"""Publishes a message to Discord whenever someone registers a new iOS test device."""
110-
# [END v2AppDistributionAlertTrigger]
89+
# [END v2AppDistributionAlertTrigger]
11190
# [START v2AppDistributionEventPayload]
11291
# Construct a helpful message to send to Discord.
11392
app_id = event.app_id
@@ -121,31 +100,24 @@ def post_new_udid_to_discord(
121100

122101
try:
123102
# [START v2SendNewTesterIosDeviceToDiscord]
124-
response = post_message_to_discord(
125-
"App Distro Bot", message, DISCORD_WEBHOOK_URL.value
126-
)
103+
response = post_message_to_discord("App Distro Bot", message, DISCORD_WEBHOOK_URL.value)
127104
if response.ok:
128-
print(
129-
f"Posted iOS device registration alert for {app_dist.tester_email} to Discord."
130-
)
105+
print(f"Posted iOS device registration alert for {app_dist.tester_email} to Discord.")
131106
pprint.pp(event.data.payload)
132107
else:
133108
response.raise_for_status()
134109
# [END v2SendNewTesterIosDeviceToDiscord]
135110
except (EnvironmentError, requests.HTTPError) as error:
136111
print(
137112
f"Unable to post iOS device registration alert for {app_dist.tester_email} to Discord.",
138-
error,
139-
)
113+
error)
140114

141115

142116
# [START v2PerformanceAlertTrigger]
143117
@performance_fn.on_threshold_alert_published(secrets=["DISCORD_WEBHOOK_URL"])
144-
def post_performance_alert_to_discord(
145-
event: performance_fn.PerformanceThresholdAlertEvent,
146-
) -> None:
118+
def post_performance_alert_to_discord(event: performance_fn.PerformanceThresholdAlertEvent) -> None:
147119
"""Publishes a message to Discord whenever a performance threshold alert is fired."""
148-
# [END v2PerformanceAlertTrigger]
120+
# [END v2PerformanceAlertTrigger]
149121
# [START v2PerformanceEventPayload]
150122
# Construct a helpful message to send to Discord.
151123
app_id = event.app_id
@@ -167,22 +139,14 @@ def post_performance_alert_to_discord(
167139

168140
try:
169141
# [START v2SendPerformanceAlertToDiscord]
170-
response = post_message_to_discord(
171-
"App Performance Bot", message, DISCORD_WEBHOOK_URL.value
172-
)
142+
response = post_message_to_discord("App Performance Bot", message,
143+
DISCORD_WEBHOOK_URL.value)
173144
if response.ok:
174-
print(
175-
f"Posted Firebase Performance alert {perf.event_name} to Discord."
176-
)
145+
print(f"Posted Firebase Performance alert {perf.event_name} to Discord.")
177146
pprint.pp(event.data.payload)
178147
else:
179148
response.raise_for_status()
180149
# [END v2SendPerformanceAlertToDiscord]
181150
except (EnvironmentError, requests.HTTPError) as error:
182-
print(
183-
f"Unable to post Firebase Performance alert {perf.event_name} to Discord.",
184-
error,
185-
)
186-
187-
151+
print(f"Unable to post Firebase Performance alert {perf.event_name} to Discord.", error)
188152
# [END v2Alerts]

Python/delete-unused-accounts-cron/functions/main.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ def accountcleanup(event: scheduler_fn.ScheduledEvent) -> None:
3636
user_page: auth.ListUsersPage | None = auth.list_users()
3737
while user_page is not None:
3838
inactive_uids = [
39-
user.uid
40-
for user in user_page.users
41-
if is_inactive(user, timedelta(days=30))
39+
user.uid for user in user_page.users if is_inactive(user, timedelta(days=30))
4240
]
4341
auth.delete_users(inactive_uids)
4442
user_page = user_page.get_next_page()
45-
46-
4743
# [END accountcleanup]
4844

4945

@@ -59,6 +55,4 @@ def is_inactive(user: auth.UserRecord, inactive_limit: timedelta) -> bool:
5955
last_seen = datetime.fromtimestamp(last_seen_timestamp)
6056
inactive_time = datetime.now() - last_seen
6157
return inactive_time >= inactive_limit
62-
63-
6458
# [END all]

Python/fcm-notifications/functions/main.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
2525
print(f"User {follower_uid} is now following user {followed_uid}")
2626
tokens_ref = db.reference(f"users/{followed_uid}/notificationTokens")
2727
notification_tokens = tokens_ref.get()
28-
if (
29-
not isinstance(notification_tokens, dict)
30-
or len(notification_tokens) < 1
31-
):
28+
if (not isinstance(notification_tokens, dict) or len(notification_tokens) < 1):
3229
print("There are no tokens to send notifications to.")
3330
return
34-
print(
35-
f"There are {len(notification_tokens)} tokens to send notifications to."
36-
)
31+
print(f"There are {len(notification_tokens)} tokens to send notifications to.")
3732

3833
follower: auth.UserRecord = auth.get_user(follower_uid)
3934
notification = messaging.Notification(
@@ -44,8 +39,7 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
4439

4540
# Send notifications to all tokens.
4641
msgs = [
47-
messaging.Message(token=token, notification=notification)
48-
for token in notification_tokens
42+
messaging.Message(token=token, notification=notification) for token in notification_tokens
4943
]
5044
batch_response: messaging.BatchResponse = messaging.send_each(msgs)
5145
if batch_response.failure_count < 1:
@@ -54,7 +48,7 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
5448
# Clean up the tokens that are not registered any more.
5549
for response in batch_response.responses:
5650
if response.exception.code in (
57-
"messaging/invalid-registration-token",
58-
"messaging/registration-token-not-registered",
51+
"messaging/invalid-registration-token",
52+
"messaging/registration-token-not-registered",
5953
):
6054
tokens_ref.child(response.message_id).delete()

Python/http-flask/functions/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,4 @@ def add_widget():
4646
def httpsflaskexample(req: https_fn.Request) -> https_fn.Response:
4747
with app.request_context(req.environ):
4848
return app.full_dispatch_request()
49-
50-
5149
# [END httpflaskexample]

0 commit comments

Comments
 (0)