param.py (5831B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\devtools\script\param.py 2 3 4 def _typecast(cls, value): 5 try: 6 return cls(value) 7 except Exception: 8 if type(cls) == type: 9 raise Error('invalid %s argument: %s' % (cls.__name__, value)) 10 raise 11 12 13 class Error(Exception): 14 __guid__ = 'param.Error' 15 16 17 class ParamObject: 18 19 def __init__(self, line): 20 self.line = line 21 self.argc = -1 22 self.error = 'Param object not initialized' 23 self.__call__ = self.ParseTypes 24 25 def __len__(self): 26 return len(self.argi) 27 28 def __getitem__(self, idx): 29 if type(idx) != int: 30 raise TypeError('Unsupported key for __getitem__') 31 self._compile() 32 if idx >= self.argc: 33 raise IndexError(self.error) 34 else: 35 return self.args[idx] 36 37 def __getslice__(self, left, right): 38 self._compile() 39 if left > self.argc: 40 raise IndexError(self.error) 41 if right < 2147483647: 42 raise ValueError('ParamObjects only support [x:] slicing') 43 try: 44 return self.line[self.argi[left]:] 45 except IndexError: 46 raise IndexError(self.error) 47 48 def _compile(self): 49 if self.argc > -1: 50 return 51 self.args = [] 52 self.argi = [] 53 quoted = False 54 line = self.line 55 length = len(line) 56 self.error = 'param index out of range' 57 idx = 0 58 while idx < length: 59 while line[idx] == ' ': 60 idx += 1 61 62 self.argi.append(idx) 63 if line[idx] == '"': 64 idx += 1 65 left = idx 66 idx = line.find('"', idx) 67 if idx == -1: 68 self.error = 'unterminated quote' 69 break 70 arg = self.line[left:idx] 71 self.args.append(arg) 72 idx += 1 73 if idx >= length: 74 break 75 if line[idx] != ' ': 76 self.error = 'crap behind quote at position %d' % (idx - 1) 77 break 78 else: 79 left = idx 80 idx = line.find(' ', idx) 81 if idx == -1: 82 idx = 2147483647 83 arg = self.line[left:idx] 84 q = arg.find('"') 85 if q > -1: 86 self.error = 'crap before quote at position %d' % (left + q) 87 break 88 self.args.append(arg) 89 90 self.argc = len(self.args) 91 92 def Parse(self, template, offset = 0): 93 ret = [] 94 a = ret.append 95 n = offset 96 qty = 1 97 wasdigit = False 98 optional = False 99 try: 100 tlist = list(template) 101 while tlist: 102 c = tlist.pop(0) 103 while qty: 104 if c == 's': 105 a(self[n]) 106 elif c == 'i': 107 try: 108 a(int(self[n])) 109 except ValueError as e: 110 raise Error(e) 111 112 elif c == 'f': 113 a(float(self[n])) 114 elif c == 'x': 115 self[n] 116 else: 117 if c == '.': 118 return ret 119 if c == '?': 120 optional = 1 121 n -= 1 122 else: 123 if c == 'r': 124 a(self[n:]) 125 return ret 126 if c.isdigit(): 127 if wasdigit: 128 qty = 10 * qty + int(c) 129 else: 130 qty = int(c) 131 wasdigit = True 132 break 133 wasdigit = False 134 qty -= 1 135 n += 1 136 else: 137 qty = 1 138 139 except IndexError: 140 if optional: 141 tlist.append('s') 142 else: 143 raise Error('not enough arguments for template string') 144 145 if optional: 146 while tlist: 147 c = tlist.pop() 148 if c != '?': 149 a(None) 150 151 elif n != self.argc: 152 raise Error('not all arguments converted during template parsing') 153 return ret 154 155 def ParseTypes(self, *args, **kwargs): 156 eol = kwargs.get('eol', False) 157 optional = False 158 positional = 0 159 n = 0 160 ret = [] 161 todo = list(args) 162 while todo: 163 typeClass = todo.pop(0) 164 if typeClass: 165 if isinstance(typeClass, list): 166 todo = typeClass 167 optional = True 168 positional = n 169 continue 170 try: 171 if eol and not todo: 172 value = self[n:] 173 n = self.argc 174 else: 175 value = self[n] 176 n += 1 177 except IndexError as e: 178 if optional: 179 ret.append(None) 180 else: 181 raise Error(e) 182 else: 183 ret.append(_typecast(typeClass, value)) 184 185 else: 186 ret.append(None) 187 188 if positional != self.argc and n != self.argc: 189 raise Error('not all parameters converted during type parsing') 190 return ret 191 192 193 exports = {'param.Error': Error, 194 'param.ParamObject': ParamObject}