-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
110 lines (93 loc) · 4.19 KB
/
utils.py
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
107
108
109
110
# utils.py --- Utility classes and functions
# Copyright (C) 2012 Sandro Bottaro, Christian Holzgraefe, Wouter Boomsma
#
# This file is part of Nettuno
#
# Nettuno is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Nettuno is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Nettuno. If not, see <http://www.gnu.org/licenses/>.
import optparse
class SubOptions:
'''Class for defining sub-options used for the command line parser'''
def __init__(self, key_value_dict):
'''Constructor. Initialized using a dictionary of (name, description) pairs'''
self.dict = key_value_dict
def __repr__(self):
'''String representation'''
output = "\n"
for item in self.dict.items():
output += " %s: %s\n" % item
return output
class HelpFormatterInnerOptions(optparse.IndentedHelpFormatter):
'''Overrides default HelpFormatter for optparse command line parser
to provide nicer printing of inner options'''
def format_option(self, option):
# The help for each option consists of two parts:
# * the opt strings and metavars
# eg. ("-x", or "-fFILENAME, --file=FILENAME")
# * the user-supplied help string
# eg. ("turn on expert mode", "read data from FILENAME")
#
# If possible, we write both of these on the same line:
# -x turn on expert mode
#
# But if the opt string list is too long, we put the help
# string on a second line, indented to the same column it would
# start in if it fit on the first line.
# -fFILENAME, --file=FILENAME
# read data from FILENAME
import textwrap
result = []
opts = self.option_strings[option]
opt_width = self.help_position - self.current_indent - 2
if len(opts) > opt_width:
opts = "%*s%s\n" % (self.current_indent, "", opts)
indent_first = self.help_position
else: # start help on same line as opts
opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
indent_first = 0
result.append(opts)
if option.help:
help_text = self.expand_default(option)
help_paragraphs = help_text.splitlines()
first_paragraph = True
for help_paragraph in help_paragraphs:
help_lines = textwrap.wrap(help_paragraph, self.help_width)
if len(help_lines) > 0:
if first_paragraph:
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
else:
result.append("%*s%s\n" % (self.help_position, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
for line in help_lines[1:]])
else:
result.append("\n")
first_paragraph=False
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)
def vararg_callback(option, opt_str, value, parser):
'''A callback for the option parser allowing a variable number of arguments.'''
value = []
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a, but not on -3 or -3.0
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
break
value.append(arg)
del parser.rargs[:len(value)]
parser.values.ensure_value(option.dest, []).append(value)
class CallbackHasMetaVarOption(optparse.Option):
'''Overrides default optparse Option class, giving callbacks a metavar description'''
ALWAYS_TYPED_ACTIONS = optparse.Option.ALWAYS_TYPED_ACTIONS + ('callback',)