]> jfr.im git - irc/rizon/acid.git/blob - pyva/pyva/src/main/python/internets/erepparser.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / internets / erepparser.py
1 from copy import copy
2 from decimal import Decimal, InvalidOperation
3 from optparse import *
4
5 def check_positive_decimal(option, opt, value):
6 try:
7 d = Decimal(value)
8 except InvalidOperation:
9 raise OptionValueError('option %s: invalid decimal value: %s' % (opt, value))
10
11 if d >= 0 and d <= 1000000:
12 return d
13 else:
14 raise OptionValueError('option %s: expected value (0 <= x <= 1000000), got %s instead' % (opt, d))
15
16 def check_positive_integer(option, opt, value):
17 try:
18 i = int(value)
19 except ValueError:
20 raise OptionValueError('option %s: invalid integer value: %s' % (opt, value))
21
22 if i >= 0:
23 return i
24 else:
25 raise OptionValueError('option %s: expected positive value, got %d instead' % (opt, i))
26
27 class ErepublikParserOption(Option):
28 TYPES = Option.TYPES + ('+decimal', '+integer', 'rank', 'quality', 'wellness', 'happiness', 'industry', 'domain')
29 TYPE_CHECKER = copy(Option.TYPE_CHECKER)
30 TYPE_CHECKER['+decimal'] = check_positive_decimal
31 TYPE_CHECKER['+integer'] = check_positive_integer
32
33 class ErepublikParserError(Exception):
34 def __init__(self, message):
35 self.msg = message
36
37 def __str__(self):
38 return str(self.msg)
39
40 class ErepublikParser(OptionParser):
41 def error(self, error):
42 raise ErepublikParserError(error)