Skip to content

Commit 7d5c0fd

Browse files
committed
ruff & mimetype
1 parent eaa13d3 commit 7d5c0fd

5 files changed

Lines changed: 160 additions & 38 deletions

File tree

gchat/chat_tools.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ async def search_messages(
402402
links_suffix = "".join(f" [linked: {url}]" for url in rich_links)
403403
attachments = msg.get("attachment", [])
404404
att_suffix = "".join(
405-
f" [attachment: {a.get('contentName', 'unnamed')}]" for a in attachments
405+
f" [attachment: {a.get('contentName', 'unnamed')} ({a.get('contentType', 'unknown type')})]"
406+
for a in attachments
406407
)
407408
output.append(
408409
f"- [{create_time}] {sender} in '{space_name}': {text_content}{links_suffix}{att_suffix}"
@@ -513,9 +514,7 @@ async def download_chat_attachment(
513514

514515
# Prefer attachmentDataRef.resourceName for the media endpoint
515516
resource_name = media_resource or att_name
516-
download_url = (
517-
f"https://chat.googleapis.com/v1/media/{resource_name}?alt=media"
518-
)
517+
download_url = f"https://chat.googleapis.com/v1/media/{resource_name}?alt=media"
519518

520519
try:
521520
access_token = service._http.credentials.token
@@ -542,13 +541,15 @@ async def download_chat_attachment(
542541

543542
if is_stateless_mode():
544543
b64_preview = base64.urlsafe_b64encode(file_bytes).decode("utf-8")[:100]
545-
return "\n".join([
546-
f"Attachment downloaded: {filename} ({content_type})",
547-
f"Size: {size_kb:.1f} KB ({size_bytes} bytes)",
548-
"",
549-
"Stateless mode: File storage disabled.",
550-
f"Base64 preview: {b64_preview}...",
551-
])
544+
return "\n".join(
545+
[
546+
f"Attachment downloaded: {filename} ({content_type})",
547+
f"Size: {size_kb:.1f} KB ({size_bytes} bytes)",
548+
"",
549+
"Stateless mode: File storage disabled.",
550+
f"Base64 preview: {b64_preview}...",
551+
]
552+
)
552553

553554
# Save to local disk
554555
from core.attachment_storage import get_attachment_storage, get_attachment_url

gdocs/docs_markdown.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ def _apply_text_style(text: str, style: dict[str, Any]) -> str:
152152
return text
153153

154154

155-
def _is_ordered_list(
156-
lists_meta: dict[str, Any], list_id: str, nesting: int
157-
) -> bool:
155+
def _is_ordered_list(lists_meta: dict[str, Any], list_id: str, nesting: int) -> bool:
158156
"""Check if a list at a given nesting level is ordered."""
159157
list_info = lists_meta.get(list_id, {})
160158
nesting_levels = list_info.get("listProperties", {}).get("nestingLevels", [])
@@ -251,9 +249,7 @@ def format_comments_appendix(comments: list[dict[str, Any]]) -> str:
251249
if anchor:
252250
lines.append(f"> {anchor}")
253251
lines.append("")
254-
lines.append(
255-
f"- **{comment['author']}**: {comment['content']}{resolved_tag}"
256-
)
252+
lines.append(f"- **{comment['author']}**: {comment['content']}{resolved_tag}")
257253
for reply in comment.get("replies", []):
258254
lines.append(f" - **{reply['author']}**: {reply['content']}")
259255
lines.append("")

tests/core/test_comments.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ async def test_read_comments_includes_quoted_text():
5757
if "Author: Bob" in part:
5858
bob_section_started = True
5959
if bob_section_started and "Quoted text:" in part:
60-
pytest.fail("Comment without quotedFileContent should not show 'Quoted text'")
60+
pytest.fail(
61+
"Comment without quotedFileContent should not show 'Quoted text'"
62+
)
6163
if bob_section_started and "Content: General comment" in part:
6264
break
6365

tests/gchat/test_chat_tools.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,7 @@ async def test_download_uses_api_media_endpoint():
291291
async def test_download_falls_back_to_att_name():
292292
"""When attachmentDataRef is missing, should fall back to attachment name."""
293293
fake_bytes = b"fetched content"
294-
att = _make_attachment(
295-
name="spaces/S/messages/M/attachments/A", resource_name=None
296-
)
294+
att = _make_attachment(name="spaces/S/messages/M/attachments/A", resource_name=None)
297295
msg = _make_message(attachments=[att])
298296

299297
service = Mock()

tests/gdocs/test_docs_markdown.py

Lines changed: 142 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
{"sectionBreak": {"sectionStyle": {}}},
2323
{
2424
"paragraph": {
25-
"elements": [{"textRun": {"content": "Hello world\n", "textStyle": {}}}],
25+
"elements": [
26+
{"textRun": {"content": "Hello world\n", "textStyle": {}}}
27+
],
2628
"paragraphStyle": {"namedStyleType": "NORMAL_TEXT"},
2729
}
2830
},
@@ -32,7 +34,12 @@
3234
{"textRun": {"content": "This is ", "textStyle": {}}},
3335
{"textRun": {"content": "bold", "textStyle": {"bold": True}}},
3436
{"textRun": {"content": " and ", "textStyle": {}}},
35-
{"textRun": {"content": "italic", "textStyle": {"italic": True}}},
37+
{
38+
"textRun": {
39+
"content": "italic",
40+
"textStyle": {"italic": True},
41+
}
42+
},
3643
{"textRun": {"content": " text.\n", "textStyle": {}}},
3744
],
3845
"paragraphStyle": {"namedStyleType": "NORMAL_TEXT"},
@@ -55,13 +62,17 @@
5562
},
5663
{
5764
"paragraph": {
58-
"elements": [{"textRun": {"content": "Heading one\n", "textStyle": {}}}],
65+
"elements": [
66+
{"textRun": {"content": "Heading one\n", "textStyle": {}}}
67+
],
5968
"paragraphStyle": {"namedStyleType": "HEADING_1"},
6069
}
6170
},
6271
{
6372
"paragraph": {
64-
"elements": [{"textRun": {"content": "Heading two\n", "textStyle": {}}}],
73+
"elements": [
74+
{"textRun": {"content": "Heading two\n", "textStyle": {}}}
75+
],
6576
"paragraphStyle": {"namedStyleType": "HEADING_2"},
6677
}
6778
},
@@ -81,14 +92,86 @@
8192
"tableRows": [
8293
{
8394
"tableCells": [
84-
{"content": [{"paragraph": {"elements": [{"textRun": {"content": "Name\n", "textStyle": {}}}], "paragraphStyle": {"namedStyleType": "NORMAL_TEXT"}}}]},
85-
{"content": [{"paragraph": {"elements": [{"textRun": {"content": "Age\n", "textStyle": {}}}], "paragraphStyle": {"namedStyleType": "NORMAL_TEXT"}}}]},
95+
{
96+
"content": [
97+
{
98+
"paragraph": {
99+
"elements": [
100+
{
101+
"textRun": {
102+
"content": "Name\n",
103+
"textStyle": {},
104+
}
105+
}
106+
],
107+
"paragraphStyle": {
108+
"namedStyleType": "NORMAL_TEXT"
109+
},
110+
}
111+
}
112+
]
113+
},
114+
{
115+
"content": [
116+
{
117+
"paragraph": {
118+
"elements": [
119+
{
120+
"textRun": {
121+
"content": "Age\n",
122+
"textStyle": {},
123+
}
124+
}
125+
],
126+
"paragraphStyle": {
127+
"namedStyleType": "NORMAL_TEXT"
128+
},
129+
}
130+
}
131+
]
132+
},
86133
]
87134
},
88135
{
89136
"tableCells": [
90-
{"content": [{"paragraph": {"elements": [{"textRun": {"content": "Alice\n", "textStyle": {}}}], "paragraphStyle": {"namedStyleType": "NORMAL_TEXT"}}}]},
91-
{"content": [{"paragraph": {"elements": [{"textRun": {"content": "30\n", "textStyle": {}}}], "paragraphStyle": {"namedStyleType": "NORMAL_TEXT"}}}]},
137+
{
138+
"content": [
139+
{
140+
"paragraph": {
141+
"elements": [
142+
{
143+
"textRun": {
144+
"content": "Alice\n",
145+
"textStyle": {},
146+
}
147+
}
148+
],
149+
"paragraphStyle": {
150+
"namedStyleType": "NORMAL_TEXT"
151+
},
152+
}
153+
}
154+
]
155+
},
156+
{
157+
"content": [
158+
{
159+
"paragraph": {
160+
"elements": [
161+
{
162+
"textRun": {
163+
"content": "30\n",
164+
"textStyle": {},
165+
}
166+
}
167+
],
168+
"paragraphStyle": {
169+
"namedStyleType": "NORMAL_TEXT"
170+
},
171+
}
172+
}
173+
]
174+
},
92175
]
93176
},
94177
],
@@ -114,14 +197,18 @@
114197
{"sectionBreak": {"sectionStyle": {}}},
115198
{
116199
"paragraph": {
117-
"elements": [{"textRun": {"content": "Item one\n", "textStyle": {}}}],
200+
"elements": [
201+
{"textRun": {"content": "Item one\n", "textStyle": {}}}
202+
],
118203
"paragraphStyle": {"namedStyleType": "NORMAL_TEXT"},
119204
"bullet": {"listId": "kix.list001", "nestingLevel": 0},
120205
}
121206
},
122207
{
123208
"paragraph": {
124-
"elements": [{"textRun": {"content": "Item two\n", "textStyle": {}}}],
209+
"elements": [
210+
{"textRun": {"content": "Item two\n", "textStyle": {}}}
211+
],
125212
"paragraphStyle": {"namedStyleType": "NORMAL_TEXT"},
126213
"bullet": {"listId": "kix.list001", "nestingLevel": 0},
127214
}
@@ -196,8 +283,18 @@ class TestParseComments:
196283
def test_filters_resolved(self):
197284
response = {
198285
"comments": [
199-
{"content": "open", "resolved": False, "author": {"displayName": "A"}, "replies": []},
200-
{"content": "closed", "resolved": True, "author": {"displayName": "B"}, "replies": []},
286+
{
287+
"content": "open",
288+
"resolved": False,
289+
"author": {"displayName": "A"},
290+
"replies": [],
291+
},
292+
{
293+
"content": "closed",
294+
"resolved": True,
295+
"author": {"displayName": "B"},
296+
"replies": [],
297+
},
201298
]
202299
}
203300
result = parse_drive_comments(response, include_resolved=False)
@@ -207,8 +304,18 @@ def test_filters_resolved(self):
207304
def test_includes_resolved(self):
208305
response = {
209306
"comments": [
210-
{"content": "open", "resolved": False, "author": {"displayName": "A"}, "replies": []},
211-
{"content": "closed", "resolved": True, "author": {"displayName": "B"}, "replies": []},
307+
{
308+
"content": "open",
309+
"resolved": False,
310+
"author": {"displayName": "A"},
311+
"replies": [],
312+
},
313+
{
314+
"content": "closed",
315+
"resolved": True,
316+
"author": {"displayName": "B"},
317+
"replies": [],
318+
},
212319
]
213320
}
214321
result = parse_drive_comments(response, include_resolved=True)
@@ -237,7 +344,13 @@ class TestInlineComments:
237344
def test_inserts_footnote(self):
238345
md = "Some text here."
239346
comments = [
240-
{"author": "Alice", "content": "Note.", "anchor_text": "text", "replies": [], "resolved": False}
347+
{
348+
"author": "Alice",
349+
"content": "Note.",
350+
"anchor_text": "text",
351+
"replies": [],
352+
"resolved": False,
353+
}
241354
]
242355
result = format_comments_inline(md, comments)
243356
assert "text[^c1]" in result
@@ -246,7 +359,13 @@ def test_inserts_footnote(self):
246359
def test_unmatched_goes_to_appendix(self):
247360
md = "No match."
248361
comments = [
249-
{"author": "Alice", "content": "Note.", "anchor_text": "missing", "replies": [], "resolved": False}
362+
{
363+
"author": "Alice",
364+
"content": "Note.",
365+
"anchor_text": "missing",
366+
"replies": [],
367+
"resolved": False,
368+
}
250369
]
251370
result = format_comments_inline(md, comments)
252371
assert "## Comments" in result
@@ -256,7 +375,13 @@ def test_unmatched_goes_to_appendix(self):
256375
class TestAppendixComments:
257376
def test_structure(self):
258377
comments = [
259-
{"author": "Alice", "content": "Note.", "anchor_text": "some text", "replies": [], "resolved": False}
378+
{
379+
"author": "Alice",
380+
"content": "Note.",
381+
"anchor_text": "some text",
382+
"replies": [],
383+
"resolved": False,
384+
}
260385
]
261386
result = format_comments_appendix(comments)
262387
assert "## Comments" in result

0 commit comments

Comments
 (0)