IMAPClient 4.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
imapclient/interact.py ADDED
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/python
2
+
3
+ # Copyright (c) 2020, Menno Smits
4
+ # Released subject to the New BSD License
5
+ # Please see http://en.wikipedia.org/wiki/BSD_licenses
6
+
7
+ import argparse
8
+ from getpass import getpass
9
+
10
+ from . import imapclient
11
+ from .config import create_client_from_config, get_config_defaults, parse_config_file
12
+
13
+
14
+ def command_line() -> argparse.Namespace:
15
+ parser = argparse.ArgumentParser()
16
+ parser.add_argument(
17
+ "-H", "--host", dest="host", action="store", help="IMAP host connect to"
18
+ )
19
+ parser.add_argument(
20
+ "-u",
21
+ "--username",
22
+ dest="username",
23
+ action="store",
24
+ help="Username to login with",
25
+ )
26
+ parser.add_argument(
27
+ "-p",
28
+ "--password",
29
+ dest="password",
30
+ action="store",
31
+ help="Password to login with",
32
+ )
33
+ parser.add_argument(
34
+ "-P",
35
+ "--port",
36
+ dest="port",
37
+ action="store",
38
+ type=int,
39
+ default=None,
40
+ help="IMAP port to use (default is 993 for TLS, or 143 otherwise)",
41
+ )
42
+
43
+ ssl_group = parser.add_mutually_exclusive_group()
44
+ ssl_group.add_argument(
45
+ "-s",
46
+ "--ssl",
47
+ dest="ssl",
48
+ action="store_true",
49
+ default=None,
50
+ help="Use SSL/TLS connection (default)",
51
+ )
52
+ ssl_group.add_argument(
53
+ "--insecure",
54
+ dest="insecure",
55
+ action="store_true",
56
+ default=False,
57
+ help="Use insecure connection (i.e. without SSL/TLS)",
58
+ )
59
+
60
+ parser.add_argument(
61
+ "-f",
62
+ "--file",
63
+ dest="file",
64
+ action="store",
65
+ default=None,
66
+ help="Config file (same as livetest)",
67
+ )
68
+
69
+ args = parser.parse_args()
70
+
71
+ if args.file:
72
+ if (
73
+ args.host
74
+ or args.username
75
+ or args.password
76
+ or args.port
77
+ or args.ssl
78
+ or args.insecure
79
+ ):
80
+ parser.error("If -f/--file is given no other options can be used")
81
+ # Use the options in the config file
82
+ args = parse_config_file(args.file)
83
+ return args
84
+
85
+ args.ssl = not args.insecure
86
+
87
+ # Scan through arguments, filling in defaults and prompting when
88
+ # a compulsory argument wasn't provided.
89
+ compulsory_args = ("host", "username", "password")
90
+ for name, default_value in get_config_defaults().items():
91
+ value = getattr(args, name, default_value)
92
+ if name in compulsory_args and value is None:
93
+ value = getpass(name + ": ")
94
+ setattr(args, name, value)
95
+
96
+ return args
97
+
98
+
99
+ def main() -> int:
100
+ args = command_line()
101
+ print("Connecting...")
102
+ client = create_client_from_config(args)
103
+ print("Connected.")
104
+ banner = '\nIMAPClient instance is "c"'
105
+
106
+ def ptpython(c: imapclient.IMAPClient) -> None:
107
+ from ptpython.repl import embed # type: ignore[import-not-found]
108
+
109
+ embed(globals(), locals())
110
+
111
+ def ipython_400(c: imapclient.IMAPClient) -> None:
112
+ from IPython.terminal.embed import ( # type: ignore[import-not-found]
113
+ InteractiveShellEmbed,
114
+ )
115
+
116
+ ipshell = InteractiveShellEmbed(banner1=banner)
117
+ ipshell("")
118
+
119
+ def ipython_011(c: imapclient.IMAPClient) -> None:
120
+ from IPython.frontend.terminal.embed import ( # type: ignore[import-not-found]
121
+ InteractiveShellEmbed,
122
+ )
123
+
124
+ ipshell = InteractiveShellEmbed(banner1=banner)
125
+ ipshell("")
126
+
127
+ def ipython_010(c: imapclient.IMAPClient) -> None:
128
+ from IPython.Shell import IPShellEmbed # type: ignore[import-not-found]
129
+
130
+ IPShellEmbed("", banner=banner)()
131
+
132
+ def builtin(c: imapclient.IMAPClient) -> None:
133
+ import code
134
+
135
+ code.interact(banner, local={"c": c})
136
+
137
+ shell_attempts = (
138
+ ptpython,
139
+ ipython_400,
140
+ ipython_011,
141
+ ipython_010,
142
+ builtin,
143
+ )
144
+ for shell in shell_attempts:
145
+ try:
146
+ shell(client)
147
+ except ImportError:
148
+ pass
149
+ else:
150
+ break
151
+ return 0
152
+
153
+
154
+ if __name__ == "__main__":
155
+ main()