1 from plomrogue.errors import ArgError
2 from plomrogue.mapping import YX
7 def __init__(self, game=None):
10 def tokenize(self, msg):
11 """Parse msg string into tokens.
13 Separates by ' ' and '\n', but allows whitespace in tokens quoted by
14 '"', and allows escaping within quoted tokens by a prefixed backslash.
33 elif c in {' ', '\n'}:
43 def parse_yx_tuple(self, yx_string, range_=None):
44 """Parse yx_string as yx_tuple, return result.
46 The range_ argument may be 'nonneg' (non-negative, including
47 0) or 'pos' (positive, excluding 0) or 'all'.
51 def get_axis_position_from_argument(axis, token):
52 if token[:2] != axis + ':':
53 raise ArgError('invalid YX tuple formatting')
55 if n_string.strip() != n_string:
56 raise ArgError('invalid YX tuple formatting')
60 raise ArgError('non-int value for ' + axis + ' position')
63 if n < 1 and range == 'pos':
64 raise ArgError('Arg for ' + axis + ' position < 1.')
65 elif n < 0 and range_ == 'nonneg':
66 raise ArgError('Arg for ' + axis + ' position < 0.')
69 tokens = yx_string.split(',')
71 raise ArgError('Wrong number of yx-tuple arguments.')
72 y = get_axis_position_from_argument('Y', tokens[0])
73 x = get_axis_position_from_argument('X', tokens[1])
77 """Parse msg as call to function, return function with args tuple.
79 Respects function signature defined in function's .argtypes attribute.
83 legal_chars = string.digits + string.ascii_letters +\
84 string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô'
86 if not c in legal_chars:
87 raise ArgError('Command/message contains illegal character(s), '
88 'may only contain ones of: %s' % legal_chars)
89 tokens = self.tokenize(msg)
92 func = self.game.get_command(tokens[0])
94 if hasattr(func, 'argtypes'):
95 argtypes = func.argtypes
98 if len(argtypes) == 0:
100 raise ArgError('Command expects no argument(s).')
103 raise ArgError('Command expects argument(s).')
104 args_candidates = tokens[1:]
105 args = self.argsparse(argtypes, args_candidates)
108 def argsparse(self, signature, args_tokens):
109 tmpl_tokens = signature.split()
110 if len(tmpl_tokens) != len(args_tokens):
111 raise ArgError('Number of arguments (' + str(len(args_tokens)) +
112 ') not expected number (' + str(len(tmpl_tokens)) +
115 string_string = 'string'
116 for i in range(len(tmpl_tokens)):
117 tmpl = tmpl_tokens[i]
119 if tmpl == 'int:nonneg':
120 if not arg.isdigit():
121 raise ArgError('Argument must be non-negative integer.')
123 elif tmpl == 'int:pos':
124 if not arg.isdigit() or int(arg) < 1:
125 raise ArgError('Argument must be positive integer.')
128 if not arg.isdigit() or int(arg) not in (0, 1):
129 raise ArgError('Argument must be 0 or 1.')
130 args += [bool(int(arg))]
135 raise ArgError('Argument must be single character.')
137 elif tmpl == 'yx_tuple:nonneg':
138 args += [self.parse_yx_tuple(arg, 'nonneg')]
139 elif tmpl == 'yx_tuple:pos':
140 args += [self.parse_yx_tuple(arg, 'pos')]
141 elif tmpl == 'yx_tuple':
142 args += [self.parse_yx_tuple(arg, 'all')]
143 elif tmpl == string_string:
145 elif tmpl[:len(string_string) + 1] == string_string + ':':
146 if not hasattr(self.game, 'get_string_options'):
147 raise ArgError('No string option directory.')
148 string_option_type = tmpl[len(string_string) + 1:]
149 options = self.game.get_string_options(string_option_type)
151 raise ArgError('Unknown string option type.')
152 if arg not in options:
153 msg = 'Argument #%s must be one of: %s' % (i + 1, options)
157 raise ArgError('Unknown argument type: %s' % tmpl)