Changeset 3413 for pykota/trunk/pykota/commandline.py
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/commandline.py
r3411 r3413 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 36 36 loginvalidparam(opt, value, option.default) 37 37 setattr(optionparser.values, option.dest, option.default) 38 else : 39 setattr(optionparser.values, option.dest, value) 40 41 def checkandset_savetoner(option, opt, value, optionparser) : 38 else : 39 setattr(optionparser.values, option.dest, value) 40 41 def checkandset_savetoner(option, opt, value, optionparser) : 42 42 """Checks and sets the save toner value.""" 43 43 if (value < 0.0) or (value > 99.0) : … … 45 45 _("Allowed range is (0..99)")) 46 46 setattr(optionparser.values, option.dest, option.default) 47 else : 48 setattr(optionparser.values, option.dest, value) 49 50 def checkandset_positiveint(option, opt, value, optionparser) : 47 else : 48 setattr(optionparser.values, option.dest, value) 49 50 def checkandset_positiveint(option, opt, value, optionparser) : 51 51 """Checks if an option argument is a positive integer and validates the option if it is the case.""" 52 52 if not (value >= 0) : … … 54 54 _("Value must be positive")) 55 55 setattr(optionparser.values, option.dest, option.default) 56 else : 57 setattr(optionparser.values, option.dest, value) 58 59 def checkandset_positivefloat(option, opt, value, optionparser) : 56 else : 57 setattr(optionparser.values, option.dest, value) 58 59 def checkandset_positivefloat(option, opt, value, optionparser) : 60 60 """Checks if an option argument is a positive float and validates the option if it is the case.""" 61 61 if not (value >= 0.0) : … … 63 63 _("Value must be positive")) 64 64 setattr(optionparser.values, option.dest, option.default) 65 else : 66 setattr(optionparser.values, option.dest, value) 67 68 def checkandset_percent(option, opt, value, optionparser) : 65 else : 66 setattr(optionparser.values, option.dest, value) 67 68 def checkandset_percent(option, opt, value, optionparser) : 69 69 """Checks if an option argument is comprised between 0.0 included and 100.0 not included, and validates the option if it is the case.""" 70 70 if not (0.0 <= value < 100.0) : … … 72 72 _("Value must be comprised between 0.0 included and 100.0 not included")) 73 73 setattr(optionparser.values, option.dest, option.default) 74 else : 75 setattr(optionparser.values, option.dest, value) 76 77 def load_arguments_file(option, opt, value, optionparser) : 74 else : 75 setattr(optionparser.values, option.dest, value) 76 77 def load_arguments_file(option, opt, value, optionparser) : 78 78 """Loads arguments from a file, one per line.""" 79 79 setattr(optionparser.values, option.dest, value) … … 89 89 if argi.startswith('"') and argi.endswith('"') : 90 90 arguments[i] = argi[1:-1] 91 arguments.reverse() 91 arguments.reverse() 92 92 for arg in arguments : 93 optionparser.rargs.insert(0, arg) 93 optionparser.rargs.insert(0, arg) 94 94 95 95 class PyKotaOptionParser(optparse.OptionParser) : … … 97 97 This class to define additional methods, and different help 98 98 formatting, from the traditional OptionParser. 99 """ 99 """ 100 100 def __init__(self, *args, **kwargs) : 101 101 """ … … 110 110 self.remove_version_and_help() 111 111 self.add_generic_options() 112 112 113 113 def format_help(self, formatter=None) : 114 114 """ … … 124 124 result.append(self.format_copyright()) 125 125 return "".join(result) 126 127 # 128 # Below are PyKota specific additions 126 127 # 128 # Below are PyKota specific additions 129 129 # 130 130 def format_filterexpressions(self, formatter=None) : … … 132 132 if formatter is None : 133 133 formatter = self.formatter 134 result = [] 134 result = [] 135 135 if self.filterexpressions : 136 136 result.append(formatter.format_heading(_("filtering expressions"))) … … 139 139 result.append("\n") 140 140 result.append(formatter.format_heading(_("allowed keys for now"))) 141 formatter.indent() 141 formatter.indent() 142 142 for (expression, explanation) in self.filterexpressions : 143 143 result.append(formatter.format_description("%s : %s" % (expression, explanation))) 144 formatter.dedent() 144 formatter.dedent() 145 145 result.append("\n") 146 146 result.append(formatter.format_heading(_("formatting of dates with the 'start' and 'end' filtering keys"))) … … 156 156 result.append(formatter.format_description(_("tomorrow[+-N] : tomorrow more or less N days (e.g. : tomorrow-15)"))) 157 157 result.append(formatter.format_description(_("now[+-N] : now more or less N days (e.g. now-15)"))) 158 formatter.dedent() 158 formatter.dedent() 159 159 result.append("\n") 160 160 result.append(formatter.format_description(_("'now' and 'today' are not exactly the same since 'today' represents the first or last second of the day depending on if it's used in a 'start=' or 'end=' date expression."))) 161 161 result.append("\n") 162 162 return "".join(result) 163 163 164 164 def format_examples(self, formatter=None) : 165 165 """Formats examples our way.""" … … 174 174 result.append(formatter.format_description(self.expand_prog_name(explanation)) + "\n") 175 175 formatter.dedent() 176 return "".join(result) 177 176 return "".join(result) 177 178 178 def format_copyright(self, formatter=None) : 179 179 """Formats copyright message our way.""" 180 180 if formatter is None : 181 181 formatter = self.formatter 182 result = [] 182 result = [] 183 183 result.append(formatter.format_heading(_("licensing terms"))) 184 184 formatter.indent() … … 188 188 for part in version.__gplblurb__.split("\n\n") : 189 189 result.append(formatter.format_description(part) + "\n") 190 formatter.dedent() 191 return "".join(result) 192 193 def add_filterexpression(self, expression, doc) : 190 formatter.dedent() 191 return "".join(result) 192 193 def add_filterexpression(self, expression, doc) : 194 194 """Adds a filtering expression.""" 195 195 self.filterexpressions.append((expression, doc)) 196 197 def add_example(self, command, doc) : 196 197 def add_example(self, command, doc) : 198 198 """Adds an usage example.""" 199 199 self.examples.append(("%prog " + command, doc)) 200 201 def remove_version_and_help(self) : 200 201 def remove_version_and_help(self) : 202 202 """Removes the default definitions for options version and help.""" 203 203 for o in ("-h", "-help", "--help", "-v", "-version", "--version") : 204 204 try : 205 205 self.remove_option(o) 206 except ValueError : 206 except ValueError : 207 207 pass 208 209 def add_generic_options(self) : 208 209 def add_generic_options(self) : 210 210 """Adds options which are common to all PyKota command line tools.""" 211 211 self.add_option("-h", "--help",