]>
Commit | Line | Data |
---|---|---|
685e346e A |
1 | from api import utils |
2 | from copy import copy | |
3 | from decimal import Decimal, InvalidOperation | |
4 | from optparse import * | |
5 | ||
6 | def check_positive_decimal(option, opt, value): | |
7 | try: | |
8 | d = Decimal(value) | |
9 | except InvalidOperation: | |
10 | raise OptionValueError('option %s: invalid decimal value: %s' % (opt, value)) | |
11 | ||
12 | if d >= 0 and d <= 1000000: | |
13 | return d | |
14 | else: | |
15 | raise OptionValueError('option %s: expected value (0 <= x <= 1000000), got %s instead' % (opt, d)) | |
16 | ||
17 | def check_positive_integer(option, opt, value): | |
18 | try: | |
19 | i = int(value) | |
20 | except ValueError: | |
21 | raise OptionValueError('option %s: invalid integer value: %s' % (opt, value)) | |
22 | ||
23 | if i >= 0: | |
24 | return i | |
25 | else: | |
26 | raise OptionValueError('option %s: expected positive value, got %d instead' % (opt, i)) | |
27 | ||
28 | def check_rank(option, opt, value): | |
29 | try: | |
30 | return utils.get_rank(value) | |
31 | except: | |
32 | raise OptionValueError('option %s: invalid rank: %s' % (opt, value)) | |
33 | ||
34 | def check_quality(option, opt, value): | |
35 | try: | |
36 | return utils.get_quality(value) | |
37 | except: | |
38 | raise OptionValueError('option %s: invalid quality: %s' % (opt, value)) | |
39 | ||
40 | def check_industry(option, opt, value): | |
41 | try: | |
42 | return utils.get_industry(value) | |
43 | except: | |
44 | raise OptionValueError('option %s: invalid industry: %s' % (opt, value)) | |
45 | ||
46 | def check_domain(option, opt, value): | |
47 | try: | |
48 | return utils.get_domain(value) | |
49 | except: | |
50 | raise OptionValueError('option %s: invalid domain: %s' % (opt, value)) | |
51 | ||
52 | class ErepublikParserOption(Option): | |
53 | TYPES = Option.TYPES + ('+decimal', '+integer', 'rank', 'quality', 'wellness', 'happiness', 'industry', 'domain') | |
54 | TYPE_CHECKER = copy(Option.TYPE_CHECKER) | |
55 | TYPE_CHECKER['+decimal'] = check_positive_decimal | |
56 | TYPE_CHECKER['+integer'] = check_positive_integer | |
57 | TYPE_CHECKER['rank'] = check_rank | |
58 | TYPE_CHECKER['quality'] = check_quality | |
59 | TYPE_CHECKER['wellness'] = check_positive_decimal | |
60 | TYPE_CHECKER['happiness'] = check_positive_decimal | |
61 | TYPE_CHECKER['industry'] = check_industry | |
62 | TYPE_CHECKER['domain'] = check_domain | |
63 | ||
64 | class ErepublikParserError(Exception): | |
65 | def __init__(self, message): | |
66 | self.msg = message | |
67 | ||
68 | def __str__(self): | |
69 | return str(self.msg) | |
70 | ||
71 | class ErepublikParser(OptionParser): | |
72 | def error(self, error): | |
73 | raise ErepublikParserError(error) |