|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | 3 | import datetime |
| 4 | +from collections.abc import Iterable |
4 | 5 |
|
5 | 6 | import click |
6 | 7 | from sqlalchemy.orm import Query |
|
13 | 14 | from inbox.models.session import global_session_scope, session_scope |
14 | 15 |
|
15 | 16 |
|
16 | | -def fetch_message_ids( |
| 17 | +def yield_account_id_and_message_ids( |
17 | 18 | *, |
18 | | - account_id: int | None, |
| 19 | + only_account_id: int | None, |
19 | 20 | date_start: datetime.date | None, |
20 | 21 | date_end: datetime.date | None, |
21 | 22 | only_inbox: bool, |
22 | | -) -> list[int]: |
23 | | - query = Query([Message.id]) |
24 | | - if account_id: |
25 | | - query = query.filter(Message.namespace.has(Namespace.account_id == account_id)) |
26 | | - if only_inbox: |
27 | | - inbox_folder = ImapUid.folder.has(Folder._canonical_name == "INBOX") |
28 | | - query = query.filter(Message.imapuids.any(inbox_folder)) |
29 | | - if date_start: |
30 | | - query = query.filter(Message.created_at >= date_start) |
31 | | - if date_end: |
32 | | - query = query.filter(Message.created_at < date_end) |
| 23 | +) -> Iterable[int, list[int]]: |
| 24 | + namespace_query = Query([Namespace.account_id, Namespace.id]) |
| 25 | + if only_account_id: |
| 26 | + namespace_query = namespace_query.filter( |
| 27 | + Namespace.account_id == only_account_id |
| 28 | + ) |
33 | 29 |
|
34 | 30 | with global_session_scope() as session: |
35 | | - message_ids = [message_id for message_id, in query.with_session(session)] |
| 31 | + account_id_to_namespace_id = { |
| 32 | + account_id: namespace_id |
| 33 | + for account_id, namespace_id in namespace_query.with_session(session) |
| 34 | + } |
36 | 35 |
|
37 | | - return message_ids |
| 36 | + for account_id, namespace_id in account_id_to_namespace_id.items(): |
| 37 | + query = Query([Message.id]).filter(Message.namespace_id == namespace_id) |
| 38 | + |
| 39 | + if only_inbox: |
| 40 | + inbox_folder = ImapUid.folder.has(Folder._canonical_name == "INBOX") |
| 41 | + query = query.filter(Message.imapuids.any(inbox_folder)) |
| 42 | + if date_start: |
| 43 | + query = query.filter(Message.created_at >= date_start) |
| 44 | + if date_end: |
| 45 | + query = query.filter(Message.created_at < date_end) |
| 46 | + |
| 47 | + with global_session_scope() as session: |
| 48 | + message_ids = [message_id for message_id, in query.with_session(session)] |
| 49 | + |
| 50 | + yield account_id, message_ids |
38 | 51 |
|
39 | 52 |
|
40 | 53 | @click.command() |
41 | 54 | @click.option("--date-start", type=click.DateTime(formats=["%Y-%m-%d"]), default=None) |
42 | 55 | @click.option("--date-end", type=click.DateTime(formats=["%Y-%m-%d"]), default=None) |
43 | | -@click.option("--account-id", type=int, default=None) |
| 56 | +@click.option("--only-account-id", type=int, default=None) |
44 | 57 | @click.option("--only-inbox", is_flag=True, default=False) |
| 58 | +@click.option("--dry-run/--no-dry-run", default=True) |
45 | 59 | def main( |
46 | | - account_id: int | None, |
| 60 | + only_account_id: int | None, |
47 | 61 | only_inbox: bool, |
48 | 62 | date_start: datetime.date | None, |
49 | 63 | date_end: datetime.date | None, |
| 64 | + dry_run: bool, |
50 | 65 | ) -> None: |
51 | | - message_ids = fetch_message_ids( |
52 | | - account_id=account_id, |
| 66 | + print( |
| 67 | + f"Settings: {only_account_id=},{only_inbox=},{date_start=},{date_end=},{dry_run=}" |
| 68 | + ) |
| 69 | + |
| 70 | + def session_factory(): |
| 71 | + return global_session_scope() if dry_run else session_scope(None) |
| 72 | + |
| 73 | + for account_id, message_ids in yield_account_id_and_message_ids( |
| 74 | + only_account_id=only_account_id, |
53 | 75 | date_start=date_start, |
54 | 76 | date_end=date_end, |
55 | 77 | only_inbox=only_inbox, |
56 | | - ) |
| 78 | + ): |
| 79 | + print(f"{account_id=},{len(message_ids)=}") |
57 | 80 |
|
58 | | - print(f"Found {len(message_ids)}") |
59 | | - |
60 | | - for message_id in message_ids: |
61 | | - with session_scope(None) as session: |
62 | | - message = session.query(Message).get(message_id) |
63 | | - old_categories = set( |
64 | | - category.display_name for category in message.categories |
65 | | - ) |
66 | | - update_message_metadata(session, message.account, message, message.is_draft) |
67 | | - new_categories = set( |
68 | | - category.display_name for category in message.categories |
69 | | - ) |
70 | | - if old_categories != new_categories: |
71 | | - print( |
72 | | - f"Message {message_id} categories changed from {old_categories} to {new_categories}" |
| 81 | + for message_id in message_ids: |
| 82 | + with session_factory() as session: |
| 83 | + message = session.query(Message).get(message_id) |
| 84 | + old_categories = set( |
| 85 | + category.display_name for category in message.categories |
| 86 | + ) |
| 87 | + update_message_metadata( |
| 88 | + session, message.account, message, message.is_draft |
| 89 | + ) |
| 90 | + new_categories = set( |
| 91 | + category.display_name for category in message.categories |
73 | 92 | ) |
| 93 | + if old_categories != new_categories: |
| 94 | + print(f"\t{message_id=},{old_categories=} to {new_categories}=") |
74 | 95 |
|
75 | 96 |
|
76 | 97 | if __name__ == "__main__": |
|
0 commit comments