-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbox_cli.py
More file actions
106 lines (82 loc) · 2.82 KB
/
mbox_cli.py
File metadata and controls
106 lines (82 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from contextlib import contextmanager
from io import StringIO
from typing import List
import rich
from rich.table import Table
import typer
import mbox
from mbox import MBox, Message, MessageState, InvalidIdError
app = typer.Typer(add_completion=False)
@app.command()
def version():
""" Return app version """
print(mbox.__version__)
@app.command()
def path():
""" Return mailbox file path """
with mailbox() as mbox:
print(mbox.path())
@app.command()
def add(frm: str, to: str, subj: str, content: List[str], state: str = typer.Option(None, "-st", "--state")):
""" Add message to mailbox """
content = " ".join(content) if content else None
if not state:
state = MessageState.UNREAD
with mailbox() as mbox:
message = Message(frm, to, subj, content, state)
id = mbox.add_message(message)
print(f"Created message with {id} id")
@app.command()
def delete(id: int):
""" Remove message from mailbox with given id """
with mailbox() as mbox:
try:
mbox.remove_message(id)
print(f"Removed message with {id} id")
except InvalidIdError:
print(f"Error: Invalid message id {id}")
@app.command("list")
def list_messages():
""" List messages in mailbox """
with mailbox() as mbox:
messages = mbox.list_messages()
table = Table(box=rich.box.SIMPLE)
table.add_column("ID")
table.add_column("From")
table.add_column("To")
table.add_column("Subject")
table.add_column("Content")
for message in messages:
table.add_row(message.id, message.frm, message.to, message.subj, message.content)
out = StringIO()
rich.print(table, file=out)
print(out.getvalue())
@contextmanager
def mailbox():
mbox = MBox('.')
yield mbox
mbox.close()
@app.callback(invoke_without_command=True)
def main(ctx: typer.Context):
print(f"Executing command: {ctx.invoked_subcommand} ...")
if ctx.invoked_subcommand is None:
list_messages()
"""
Usage examples:
# 1. Create message:
$ python3 cli.py add 'Anna K' 'Andrew L' 'Additional changes' 'Im writing to inform you that...'
Executing command: add ...
Created message with 0 id
# 2. List existing messages:
$ python3 cli.py list
Executing command: list ...
ID From To Subject Content
────────────────────────────────────────────────────────────────────────────────
0 Andrew L Additional changes Im writng to inform you that... Unread
# 3. Delete message:
$ python3 cli.py delete 0
Executing command: delete ...
Removed message with 0 id
"""
if __name__ == "__main__":
app()