1 from plomrogue.errors import ArgError
2 from plomrogue.mapping import YX
7 def __init__(self, game=None):
9 self.string_options = {}
11 def tokenize(self, msg):
12 """Parse msg string into tokens.
14 Separates by ' ' and '\n', but allows whitespace in tokens quoted by
15 '"', and allows escaping within quoted tokens by a prefixed backslash.
34 elif c in {' ', '\n'}:
44 def parse_yx_tuple(self, yx_string, range_=None):
45 """Parse yx_string as yx_tuple, return result.
47 The range_ argument may be 'nonneg' (non-negative, including
48 0) or 'pos' (positive, excluding 0) or 'all'.
52 def get_axis_position_from_argument(axis, token):
53 if token[:2] != axis + ':':
54 raise ArgError('invalid YX tuple formatting')
56 if n_string.strip() != n_string:
57 raise ArgError('invalid YX tuple formatting')
61 raise ArgError('non-int value for ' + axis + ' position')
64 if n < 1 and range == 'pos':
65 raise ArgError('Arg for ' + axis + ' position < 1.')
66 elif n < 0 and range_ == 'nonneg':
67 raise ArgError('Arg for ' + axis + ' position < 0.')
70 tokens = yx_string.split(',')
72 raise ArgError('Wrong number of yx-tuple arguments.')
73 y = get_axis_position_from_argument('Y', tokens[0])
74 x = get_axis_position_from_argument('X', tokens[1])
77 def parse(self, msg, replace_newline=True):
78 """Parse msg as call to function, return function with args tuple.
80 Respects function signature defined in function's .argtypes attribute.
82 Refuses messages with any but a small list of acceptable characters.
87 msg = msg.replace('\n', ' ') # Inserted by some tablet keyboards.
88 legal_chars = string.digits + string.ascii_letters +\
89 string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô' + '–…'
91 if not c in legal_chars:
92 raise ArgError('Command/message contains illegal character(s), '
93 'may only contain ones of: %s' % legal_chars)
94 tokens = self.tokenize(msg)
97 func = self.game.get_command(tokens[0])
99 if hasattr(func, 'argtypes'):
100 argtypes = func.argtypes
103 if len(argtypes) == 0:
105 raise ArgError('Command expects no argument(s).')
108 raise ArgError('Command expects argument(s).')
109 args_candidates = tokens[1:]
110 args = self.argsparse(argtypes, args_candidates)
113 def argsparse(self, signature, args_tokens):
114 tmpl_tokens = signature.split()
115 if len(tmpl_tokens) != len(args_tokens):
116 raise ArgError('Number of arguments (' + str(len(args_tokens)) +
117 ') not expected number (' + str(len(tmpl_tokens)) +
120 string_string = 'string'
121 for i in range(len(tmpl_tokens)):
122 tmpl = tmpl_tokens[i]
124 if tmpl == 'int:nonneg':
125 if not arg.isdigit():
126 raise ArgError('Argument must be non-negative integer.')
128 elif tmpl == 'int:pos':
129 if not arg.isdigit() or int(arg) < 1:
130 raise ArgError('Argument must be positive integer.')
136 raise ArgError('Argument must be integer.')
138 if not arg.isdigit() or int(arg) not in (0, 1):
139 raise ArgError('Argument must be 0 or 1.')
140 args += [bool(int(arg))]
145 raise ArgError('Argument must be single character.')
147 elif tmpl == 'yx_tuple:nonneg':
148 args += [self.parse_yx_tuple(arg, 'nonneg')]
149 elif tmpl == 'yx_tuple:pos':
150 args += [self.parse_yx_tuple(arg, 'pos')]
151 elif tmpl == 'yx_tuple':
152 args += [self.parse_yx_tuple(arg, 'all')]
153 elif tmpl == string_string:
155 elif tmpl[:len(string_string) + 1] == string_string + ':':
156 string_option_type = tmpl[len(string_string) + 1:]
157 if not string_option_type in self.string_options.keys():
158 raise ArgError('Unknown string option type: %s' % string_option_type)
159 options = self.string_options[string_option_type]
160 if arg not in options:
161 msg = 'Argument #%s must be one of: %s' % (i + 1, options)
165 raise ArgError('Unknown argument type: %s' % tmpl)