1
# Copyright (c) 2010 Mitch Garnaat http://garnaat.org/
2
# Copyright (c) 2010, Eucalyptus Systems, Inc.
4
# Permission is hereby granted, free of charge, to any person obtaining a
5
# copy of this software and associated documentation files (the
6
# "Software"), to deal in the Software without restriction, including
7
# without limitation the rights to use, copy, modify, merge, publish, dis-
8
# tribute, sublicense, and/or sell copies of the Software, and to permit
9
# persons to whom the Software is furnished to do so, subject to the fol-
12
# The above copyright notice and this permission notice shall be included
13
# in all copies or substantial portions of the Software.
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25
class Converter(object):
28
def convert_string(cls, param, value):
29
# TODO: could do length validation, etc. here
30
if not isinstance(value, basestring):
35
def convert_integer(cls, param, value):
36
# TODO: could do range checking here
40
def convert_boolean(cls, param, value):
42
For command line arguments, just the presence
43
of the option means True so just return True
48
def convert_file(cls, param, value):
49
if os.path.isfile(value):
54
def convert_dir(cls, param, value):
55
if os.path.isdir(value):
60
def convert(cls, param, value):
62
if hasattr(cls, 'convert_'+param.ptype):
63
mthd = getattr(cls, 'convert_'+param.ptype)
65
mthd = cls.convert_string
66
return mthd(param, value)
68
raise ValidationException(param, '')
72
def __init__(self, name=None, ptype='string', optional=True,
73
short_name=None, long_name=None, doc='',
74
metavar=None, cardinality=1, default=None,
75
choices=None, encoder=None, request_param=True):
78
self.optional = optional
79
self.short_name = short_name
80
self.long_name = long_name
82
self.metavar = metavar
83
self.cardinality = cardinality
84
self.default = default
85
self.choices = choices
86
self.encoder = encoder
87
self.request_param = request_param
90
def optparse_long_name(self):
93
ln = '--%s' % self.long_name
97
def synopsis_long_name(self):
100
ln = '--%s' % self.long_name
104
def getopt_long_name(self):
107
ln = '%s' % self.long_name
108
if self.ptype != 'boolean':
113
def optparse_short_name(self):
116
sn = '-%s' % self.short_name
120
def synopsis_short_name(self):
123
sn = '-%s' % self.short_name
127
def getopt_short_name(self):
130
sn = '%s' % self.short_name
131
if self.ptype != 'boolean':
135
def convert(self, value):
137
Convert a string value as received in the command line
138
tools and convert to the appropriate type of value.
139
Raise a ValidationError if the value can't be converted.
142
:param value: The value to convert. This should always
145
return Converter.convert(self, value)