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.
81 Throws out messages with any but a small list of acceptable characters.
85 msg = msg.replace('\n', ' ') # Inserted by some tablet keyboards.
86 legal_chars = string.digits + string.ascii_letters +\
87 string.punctuation + ' ' + 'ÄäÖöÜüߧ' + 'éèáàô'
89 if not c in legal_chars:
90 raise ArgError('Command/message contains illegal character(s), '
91 'may only contain ones of: %s' % legal_chars)
92 tokens = self.tokenize(msg)
95 func = self.game.get_command(tokens[0])
97 if hasattr(func, 'argtypes'):
98 argtypes = func.argtypes
101 if len(argtypes) == 0:
103 raise ArgError('Command expects no argument(s).')
106 raise ArgError('Command expects argument(s).')
107 args_candidates = tokens[1:]
108 args = self.argsparse(argtypes, args_candidates)
111 def argsparse(self, signature, args_tokens):
112 tmpl_tokens = signature.split()
113 if len(tmpl_tokens) != len(args_tokens):
114 raise ArgError('Number of arguments (' + str(len(args_tokens)) +
115 ') not expected number (' + str(len(tmpl_tokens)) +
118 string_string = 'string'
119 for i in range(len(tmpl_tokens)):
120 tmpl = tmpl_tokens[i]
122 if tmpl == 'int:nonneg':
123 if not arg.isdigit():
124 raise ArgError('Argument must be non-negative integer.')
126 elif tmpl == 'int:pos':
127 if not arg.isdigit() or int(arg) < 1:
128 raise ArgError('Argument must be positive integer.')
131 if not arg.isdigit() or int(arg) not in (0, 1):
132 raise ArgError('Argument must be 0 or 1.')
133 args += [bool(int(arg))]
138 raise ArgError('Argument must be single character.')
140 elif tmpl == 'yx_tuple:nonneg':
141 args += [self.parse_yx_tuple(arg, 'nonneg')]
142 elif tmpl == 'yx_tuple:pos':
143 args += [self.parse_yx_tuple(arg, 'pos')]
144 elif tmpl == 'yx_tuple':
145 args += [self.parse_yx_tuple(arg, 'all')]
146 elif tmpl == string_string:
148 elif tmpl[:len(string_string) + 1] == string_string + ':':
149 if not hasattr(self.game, 'get_string_options'):
150 raise ArgError('No string option directory.')
151 string_option_type = tmpl[len(string_string) + 1:]
152 options = self.game.get_string_options(string_option_type)
154 raise ArgError('Unknown string option type.')
155 if arg not in options:
156 msg = 'Argument #%s must be one of: %s' % (i + 1, options)
160 raise ArgError('Unknown argument type: %s' % tmpl)