srcanamdw/codescanner/pyinstaller/optik/option.py
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
"""optik.option
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
Defines the Option class and some standard value-checking functions.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
"""
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
# Copyright (c) 2001-2004 Gregory P. Ward.  All rights reserved.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
# See the README.txt distributed with Optik for licensing terms.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
import sys
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
import types, string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
from optik.errors import OptionError, OptionValueError, gettext
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
_ = gettext
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
__revision__ = "$Id: option.py,v 1.1 2009/02/05 23:03:30 stechong Exp $"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
__all__ = ['Option']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
# Do the right thing with boolean values for all known Python versions.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
    True, False
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
except NameError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
    (True, False) = (1, 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
# For Python 1.5, just ignore unicode (try it as str)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
    unicode
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
except NameError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
    unicode=str
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
    types.UnicodeType
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
except AttributeError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
    types.UnicodeType = types.StringType
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
_idmax = 2L * sys.maxint + 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
def _repr(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
    return "<%s at 0x%x: %s>" % (self.__class__.__name__,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
                                 id(self) & _idmax,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
                                 self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
def _parse_num(val, type):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
    if string.lower(val[:2]) == "0x":   # hexadecimal
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
        radix = 16
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
    elif string.lower(val[:2]) == "0b": # binary
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
        radix = 2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
        val = val[2:] or "0"            # have to remove "0b" prefix
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
    elif val[:1] == "0":                # octal
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
        radix = 8
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
    else:                               # decimal
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
        radix = 10
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
    try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
        return type(val, radix)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
    except TypeError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
        # In Python pre-2.0, int() and long() did not support the radix
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
        # argument. We catch the type error (not to be confused with ValueError,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
        # which is a real parsing failure), and try again with string.atol.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
        return type(string.atol(val, radix))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
def _parse_int(val):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
    return _parse_num(val, int)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
def _parse_long(val):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
    return _parse_num(val, long)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
_builtin_cvt = { "int" : (_parse_int, _("integer")),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
                 "long" : (_parse_long, _("long integer")),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
                 "float" : (float, _("floating-point")),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
                 "complex" : (complex, _("complex")) }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
def check_builtin(option, opt, value):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
    (cvt, what) = _builtin_cvt[option.type]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
    try:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
        return cvt(value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
    except ValueError:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
        raise OptionValueError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
            _("option %s: invalid %s value: %s") % (opt, what, repr(value)))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
def check_choice(option, opt, value):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
    if value in option.choices:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
        return value
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
    else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
        choices = string.join(map(repr, option.choices), ", ")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
        raise OptionValueError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
            _("option %s: invalid choice: %s (choose from %s)")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
            % (opt, repr(value), choices))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
# Not supplying a default is different from a default of None,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
# so we need an explicit "not supplied" value.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
NO_DEFAULT = ("NO", "DEFAULT")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
class Option:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
    """
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
    Instance attributes:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
      _short_opts : [string]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
      _long_opts : [string]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
      action : string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
      type : string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
      dest : string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
      default : any
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
      nargs : int
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
      const : any
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
      choices : [string]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
      callback : function
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
      callback_args : (any*)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
      callback_kwargs : { string : any }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
      help : string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
      metavar : string
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
    """
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
    # The list of instance attributes that may be set through
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
    # keyword args to the constructor.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
    ATTRS = ['action',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
             'type',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
             'dest',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
             'default',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
             'nargs',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
             'const',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
             'choices',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
             'callback',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
             'callback_args',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
             'callback_kwargs',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
             'help',
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
             'metavar']
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
    # The set of actions allowed by option parsers.  Explicitly listed
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
    # here so the constructor can validate its arguments.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
    ACTIONS = ("store",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
               "store_const",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
               "store_true",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
               "store_false",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
               "append",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
               "append_const",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
               "count",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
               "callback",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
               "help",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
               "version")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
    # The set of actions that involve storing a value somewhere;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
    # also listed just for constructor argument validation.  (If
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
    # the action is one of these, there must be a destination.)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
    STORE_ACTIONS = ("store",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
                     "store_const",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
                     "store_true",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
                     "store_false",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
                     "append",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
                     "append_const",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
                     "count")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
    # The set of actions for which it makes sense to supply a value
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
    # type, ie. which may consume an argument from the command line.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
    TYPED_ACTIONS = ("store",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
                     "append",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   156
                     "callback")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   157
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   158
    # The set of actions which *require* a value type, ie. that
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   159
    # always consume an argument from the command line.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   160
    ALWAYS_TYPED_ACTIONS = ("store",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   161
                            "append")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   162
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   163
    # The set of actions which take a 'const' attribute.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   164
    CONST_ACTIONS = ("store_const",
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   165
                     "append_const")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   166
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   167
    # The set of known types for option parsers.  Again, listed here for
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   168
    # constructor argument validation.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   169
    TYPES = ("string", "int", "long", "float", "complex", "choice")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   170
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   171
    # Dictionary of argument checking functions, which convert and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   172
    # validate option arguments according to the option type.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   173
    #
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   174
    # Signature of checking functions is:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   175
    #   check(option : Option, opt : string, value : string) -> any
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   176
    # where
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   177
    #   option is the Option instance calling the checker
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   178
    #   opt is the actual option seen on the command-line
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   179
    #     (eg. "-a", "--file")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   180
    #   value is the option argument seen on the command-line
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   181
    #
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   182
    # The return value should be in the appropriate Python type
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   183
    # for option.type -- eg. an integer if option.type == "int".
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   184
    #
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   185
    # If no checker is defined for a type, arguments will be
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   186
    # unchecked and remain strings.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   187
    TYPE_CHECKER = { "int"    : check_builtin,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   188
                     "long"   : check_builtin,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   189
                     "float"  : check_builtin,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   190
                     "complex": check_builtin,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   191
                     "choice" : check_choice,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   192
                   }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   193
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   194
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   195
    # CHECK_METHODS is a list of unbound method objects; they are called
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   196
    # by the constructor, in order, after all attributes are
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   197
    # initialized.  The list is created and filled in later, after all
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   198
    # the methods are actually defined.  (I just put it here because I
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   199
    # like to define and document all class attributes in the same
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   200
    # place.)  Subclasses that add another _check_*() method should
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   201
    # define their own CHECK_METHODS list that adds their check method
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   202
    # to those from this class.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   203
    CHECK_METHODS = None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   204
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   205
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   206
    # -- Constructor/initialization methods ----------------------------
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   207
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   208
    def __init__(self, *opts, **attrs):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   209
        # Set _short_opts, _long_opts attrs from 'opts' tuple.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   210
        # Have to be set now, in case no option strings are supplied.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   211
        self._short_opts = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   212
        self._long_opts = []
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   213
        opts = self._check_opt_strings(opts)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   214
        self._set_opt_strings(opts)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   215
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   216
        # Set all other attrs (action, type, etc.) from 'attrs' dict
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   217
        self._set_attrs(attrs)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   218
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   219
        # Check all the attributes we just set.  There are lots of
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   220
        # complicated interdependencies, but luckily they can be farmed
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   221
        # out to the _check_*() methods listed in CHECK_METHODS -- which
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   222
        # could be handy for subclasses!  The one thing these all share
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   223
        # is that they raise OptionError if they discover a problem.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   224
        for checker in self.CHECK_METHODS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   225
            checker(self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   226
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   227
    def _check_opt_strings(self, opts):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   228
        # Filter out None because early versions of Optik had exactly
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   229
        # one short option and one long option, either of which
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   230
        # could be None.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   231
        opts = filter(None, opts)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   232
        if not opts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   233
            raise TypeError("at least one option string must be supplied")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   234
        return opts
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   235
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   236
    def _set_opt_strings(self, opts):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   237
        for opt in opts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   238
            if len(opt) < 2:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   239
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   240
                    "invalid option string %s: "
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   241
                    "must be at least two characters long" % repr(opt), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   242
            elif len(opt) == 2:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   243
                if not (opt[0] == "-" and opt[1] != "-"):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   244
                    raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   245
                        "invalid short option string %s: "
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   246
                        "must be of the form -x, (x any non-dash char)" % repr(opt),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   247
                        self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   248
                self._short_opts.append(opt)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   249
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   250
                if not (opt[0:2] == "--" and opt[2] != "-"):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   251
                    raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   252
                        "invalid long option string %s: "
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   253
                        "must start with --, followed by non-dash" % repr(opt),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   254
                        self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   255
                self._long_opts.append(opt)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   256
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   257
    def _set_attrs(self, attrs):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   258
        for attr in self.ATTRS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   259
            if attrs.has_key(attr):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   260
                setattr(self, attr, attrs[attr])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   261
                del attrs[attr]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   262
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   263
                if attr == 'default':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   264
                    setattr(self, attr, NO_DEFAULT)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   265
                else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   266
                    setattr(self, attr, None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   267
        if attrs:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   268
            raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   269
                "invalid keyword arguments: %s" % string.join(attrs.keys(), ", "),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   270
                self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   271
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   272
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   273
    # -- Constructor validation methods --------------------------------
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   274
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   275
    def _check_action(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   276
        if self.action is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   277
            self.action = "store"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   278
        elif self.action not in self.ACTIONS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   279
            raise OptionError("invalid action: %s" % repr(self.action), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   280
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   281
    def _check_type(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   282
        if self.type is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   283
            if self.action in self.ALWAYS_TYPED_ACTIONS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   284
                if self.choices is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   285
                    # The "choices" attribute implies "choice" type.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   286
                    self.type = "choice"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   287
                else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   288
                    # No type given?  "string" is the most sensible default.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   289
                    self.type = "string"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   290
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   291
            # Allow type objects as an alternative to their names.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   292
            if hasattr(self.type, "__name__"):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   293
                self.type = self.type.__name__
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   294
            if self.type == "str":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   295
                self.type = "string"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   296
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   297
            if self.type not in self.TYPES:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   298
                raise OptionError("invalid option type: %s" % repr(self.type), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   299
            if self.action not in self.TYPED_ACTIONS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   300
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   301
                    "must not supply a type for action %s" % repr(self.action), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   302
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   303
    def _check_choice(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   304
        if self.type == "choice":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   305
            if self.choices is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   306
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   307
                    "must supply a list of choices for type 'choice'", self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   308
            elif type(self.choices) not in (types.TupleType, types.ListType):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   309
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   310
                    "choices must be a list of strings ('%s' supplied)"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   311
                    % string.split(str(type(self.choices)), "'")[1], self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   312
        elif self.choices is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   313
            raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   314
                "must not supply choices for type %s" % repr(self.type), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   315
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   316
    def _check_dest(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   317
        # No destination given, and we need one for this action.  The
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   318
        # self.type check is for callbacks that take a value.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   319
        takes_value = (self.action in self.STORE_ACTIONS or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   320
                       self.type is not None)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   321
        if self.dest is None and takes_value:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   322
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   323
            # Glean a destination from the first long option string,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   324
            # or from the first short option string if no long options.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   325
            if self._long_opts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   326
                # eg. "--foo-bar" -> "foo_bar"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   327
                self.dest = string.replace(self._long_opts[0][2:], '-', '_')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   328
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   329
                self.dest = self._short_opts[0][1]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   330
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   331
    def _check_const(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   332
        if self.action not in self.CONST_ACTIONS and self.const is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   333
            raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   334
                "'const' must not be supplied for action %s" % repr(self.action),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   335
                self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   336
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   337
    def _check_nargs(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   338
        if self.action in self.TYPED_ACTIONS:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   339
            if self.nargs is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   340
                self.nargs = 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   341
        elif self.nargs is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   342
            raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   343
                "'nargs' must not be supplied for action %s" % repr(self.action),
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   344
                self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   345
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   346
    def _check_callback(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   347
        if self.action == "callback":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   348
            if not callable(self.callback):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   349
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   350
                    "callback not callable: %s" % repr(self.callback), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   351
            if (self.callback_args is not None and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   352
                type(self.callback_args) is not types.TupleType):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   353
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   354
                    "callback_args, if supplied, must be a tuple: not %s"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   355
                    % repr(self.callback_args), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   356
            if (self.callback_kwargs is not None and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   357
                type(self.callback_kwargs) is not types.DictType):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   358
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   359
                    "callback_kwargs, if supplied, must be a dict: not %s"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   360
                    % repr(self.callback_kwargs), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   361
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   362
            if self.callback is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   363
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   364
                    "callback supplied (%s) for non-callback option"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   365
                    % repr(self.callback), self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   366
            if self.callback_args is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   367
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   368
                    "callback_args supplied for non-callback option", self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   369
            if self.callback_kwargs is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   370
                raise OptionError(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   371
                    "callback_kwargs supplied for non-callback option", self)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   372
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   373
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   374
    CHECK_METHODS = [_check_action,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   375
                     _check_type,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   376
                     _check_choice,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   377
                     _check_dest,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   378
                     _check_const,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   379
                     _check_nargs,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   380
                     _check_callback]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   381
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   382
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   383
    # -- Miscellaneous methods -----------------------------------------
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   384
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   385
    def __str__(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   386
        return string.join(self._short_opts + self._long_opts, "/")
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   387
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   388
    __repr__ = _repr
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   389
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   390
    def takes_value(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   391
        return self.type is not None
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   392
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   393
    def get_opt_string(self):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   394
        if self._long_opts:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   395
            return self._long_opts[0]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   396
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   397
            return self._short_opts[0]
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   398
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   399
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   400
    # -- Processing methods --------------------------------------------
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   401
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   402
    def check_value(self, opt, value):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   403
        checker = self.TYPE_CHECKER.get(self.type)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   404
        if checker is None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   405
            return value
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   406
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   407
            return checker(self, opt, value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   408
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   409
    def convert_value(self, opt, value):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   410
        if value is not None:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   411
            if self.nargs == 1:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   412
                return self.check_value(opt, value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   413
            else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   414
                return tuple(map(lambda v,self=self,opt=opt: self.check_value(opt, v), value))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   415
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   416
    def process(self, opt, value, values, parser):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   417
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   418
        # First, convert the value(s) to the right type.  Howl if any
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   419
        # value(s) are bogus.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   420
        value = self.convert_value(opt, value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   421
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   422
        # And then take whatever action is expected of us.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   423
        # This is a separate method to make life easier for
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   424
        # subclasses to add new actions.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   425
        return self.take_action(
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   426
            self.action, self.dest, opt, value, values, parser)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   427
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   428
    def take_action(self, action, dest, opt, value, values, parser):
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   429
        if action == "store":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   430
            setattr(values, dest, value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   431
        elif action == "store_const":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   432
            setattr(values, dest, self.const)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   433
        elif action == "store_true":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   434
            setattr(values, dest, True)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   435
        elif action == "store_false":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   436
            setattr(values, dest, False)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   437
        elif action == "append":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   438
            values.ensure_value(dest, []).append(value)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   439
        elif action == "append_const":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   440
            values.ensure_value(dest, []).append(self.const)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   441
        elif action == "count":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   442
            setattr(values, dest, values.ensure_value(dest, 0) + 1)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   443
        elif action == "callback":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   444
            args = self.callback_args or ()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   445
            kwargs = self.callback_kwargs or {}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   446
            apply(self.callback, (self, opt, value, parser)+args, kwargs)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   447
        elif action == "help":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   448
            parser.print_help()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   449
            parser.exit()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   450
        elif action == "version":
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   451
            parser.print_version()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   452
            parser.exit()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   453
        else:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   454
            raise RuntimeError, "unknown action %s" % repr(self.action)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   455
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   456
        return 1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   457
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   458
# class Option