-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgmailcli.py
More file actions
executable file
·78 lines (68 loc) · 2.26 KB
/
Copy pathgmailcli.py
File metadata and controls
executable file
·78 lines (68 loc) · 2.26 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
#!/usr/bin/env python3
import argparse
import json
import sys
from argparse import RawTextHelpFormatter
from gmail import gmail, history
from gmail.argparse_utils import checked_file_path, wrap_long, wrap_short
def main() -> None:
parser = argparse.ArgumentParser(
description=wrap_long(
"Interactive command-line interface to access the Gmail API. It"
" allows to issue API calls by concatenating their method names"
" with an underscore. E.g., to call the method"
" 'users.messages.list', you just have to write 'messages_list'"
" followed by the arguments the method takes, e.g.,"
' labelIds=["INBOX"]. The answer from the Gmail server will be'
" printed to the screen, either the API call response or an error."
),
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"-p",
"--profile",
metavar="NAME",
help=wrap_short(
"profile name to store Gmail access token and command history"
f" under '{gmail.get_profile_dir()}'"
),
required=True,
)
parser.add_argument(
"-c",
"--credentials",
metavar="FILE",
help=wrap_short(
"OAuth 2.0 client credentials file for your Google Cloud project"
" (default: ./credentials.json)"
),
default="credentials.json",
type=checked_file_path,
)
# Parse arguments
args = parser.parse_args()
profile_name = args.profile
credentials_file = args.credentials
(creds, err) = gmail.authenticate(profile_name, credentials_file)
if err:
sys.exit(1)
profile_dir = gmail.get_profile_dir(profile_name)
history.init(profile_dir)
print("Type Ctrl-D to quit")
while True:
try:
line = input(">>> ")
(response, err) = gmail.execute(creds, line)
if not err:
print(
json.dumps(
response, indent=2, ensure_ascii=False, sort_keys=True
)
)
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
except EOFError:
print()
break
if __name__ == "__main__":
main()