1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """ This package contains a specialized OptionParser for use with ProcImap.
22
23 All CLI tools developed from the ProcImap package should uniformly
24 provide an option '-p' / '--profile' to read a MailboxFactory profile
25
26 Optionally, the filename for the profile is read from the
27 PROC_IMAP_PROFILE environment variable.
28
29 This allows the user to keep account information (passwords) in one
30 place for a variety of tools.
31 """
32
33 from ProcImap.Utils.MailboxFactory import MailboxFactory
34
35 from optparse import OptionParser, IndentedHelpFormatter
36 import os
37
38
40 """ A special OptionParser for ProcImap. This takes the exact same
41 arguments as optparse.OptionParser.
42
43 However, there is a predefined option '-p' or '--profile'. If
44 not given explicitely, this option takes the value of the
45 environment variable PROC_IMAP_PROFILE. If the option is not
46 given, neither explicitely nor implicitely through the envirnoment
47 variable, ProcImapOptParser will shoot down the program it's
48 running in with an error message.
49
50 You can use ProcImapOptParser in this manner:
51
52 >>> opt = ProcImapOptParser()
53 >>> (options, args) = opt.parse_args(args=sys.argv)
54
55 if sys.argv included e.g. '-p mailboxes.cfg', options.profile will
56 NOT be the string 'mailboxes.cfg', but instead will be an instance
57 of MailboxFactory, generated from the file mailboxes.cfg.
58
59 ProcImapOptParser also has a modified default formatter that
60 allows explicit linebreaks (for paragraphs) in help-generating
61 arguments like 'epilogue'.
62 """
64 """ See documentation of optparse.OptionParser for arguments. """
65 class MyIndentedHelpFormatter(IndentedHelpFormatter):
66 """ Slightly modified formatter for help output:
67 allow paragraphs
68 """
69 def format_paragraphs(self, text):
70 """ wrap text per paragraph """
71 result = ""
72 for paragraph in text.split("\n"):
73 result += self._format_text(paragraph) + "\n"
74 return result
75 def format_description(self, description):
76 """ format description, honoring paragraphs """
77 if description:
78 return self.format_paragraphs(description) + "\n"
79 else:
80 return ""
81 def format_epilog(self, epilog):
82 """ format epilog, honoring paragraphs """
83 if epilog:
84 return "\n" + self.format_paragraphs(epilog) + "\n"
85 else:
86 return ""
87 OptionParser.__init__(self, **args)
88 self.add_option("-p", "--profile", dest="profile",
89 help="Use FILE as the MailboxFactory config file "
90 "defining all the mailboxes for this program.",
91 metavar="FILE")
92 if not args.has_key('formatter'):
93 self.formatter = MyIndentedHelpFormatter()
94 if os.environ.has_key('PROC_IMAP_PROFILE'):
95 self.set_defaults(profile=os.environ['PROC_IMAP_PROFILE'])
96
98 """
99 parse_args(args : [string] = sys.argv[1:],
100 values : Values = None)
101 -> (values : Values, args : [string])
102
103 Parse the command-line options found in 'args' (default:
104 sys.argv[1:]). Any errors result in a call to 'error()', which
105 by default prints the usage message to stderr and calls
106 sys.exit() with an error message. On success returns a pair
107 (values, args) where 'values' is an Values instance (with all
108 your option values) and 'args' is the list of arguments left
109 over after parsing options.
110 """
111 result = OptionParser.parse_args(self, args, values)
112 if self.values.profile is None:
113 self.exit(status=1, msg="You did not provide a profile, and the "
114 "PROC_IMAP_PROFILE environment variable is not set.\n")
115 else:
116 try:
117 self.values.profile = MailboxFactory(self.values.profile)
118 except:
119 raise
120 return result
121