Mercurial > repos > iuc > table_compute
comparison scripts/safety.py @ 1:dddadbbac949 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/table_compute commit 6820ec9431a22576f3716c40feeb27f0b8cf5e83"
author | iuc |
---|---|
date | Fri, 30 Aug 2019 05:28:18 -0400 |
parents | 1b0f96ed73f2 |
children | 02c3e335a695 |
comparison
equal
deleted
inserted
replaced
0:1b0f96ed73f2 | 1:dddadbbac949 |
---|---|
9 | 9 |
10 __allowed_tokens = ( | 10 __allowed_tokens = ( |
11 '(', ')', 'if', 'else', 'or', 'and', 'not', 'in', | 11 '(', ')', 'if', 'else', 'or', 'and', 'not', 'in', |
12 '+', '-', '*', '/', '%', ',', '!=', '==', '>', '>=', '<', '<=', | 12 '+', '-', '*', '/', '%', ',', '!=', '==', '>', '>=', '<', '<=', |
13 'min', 'max', 'sum', | 13 'min', 'max', 'sum', |
14 'str', 'int', 'float' | |
14 ) | 15 ) |
15 __allowed_ref_types = { | 16 __allowed_ref_types = { |
16 'pd.DataFrame': { | 17 'pd.DataFrame': { |
17 'abs', 'add', 'agg', 'aggregate', 'align', 'all', 'any', 'append', | 18 'abs', 'add', 'agg', 'aggregate', 'align', 'all', 'any', 'append', |
18 'apply', 'applymap', 'as_matrix', 'asfreq', 'at', 'axes', 'bool', | 19 'apply', 'applymap', 'as_matrix', 'asfreq', 'at', 'axes', 'bool', |
161 and their methods | 162 and their methods |
162 """ | 163 """ |
163 | 164 |
164 safe = True | 165 safe = True |
165 # examples of user-expressions | 166 # examples of user-expressions |
166 # '-math.log(1 - elem/4096) * 4096 if elem != bn else elem - 0.5' | 167 # '-math.log(1 - elem/4096) * 4096 if elem != 1 else elem - 0.5' |
167 # 'vec.median() + vec.sum()' | 168 # 'vec.median() + vec.sum()' |
168 | 169 |
169 # 1. Break expressions into tokens | 170 # 1. Break expressions into tokens |
170 # e.g., | 171 # e.g., |
171 # [ | 172 # [ |
172 # '-', 'math.log', '(', '1', '-', 'elem', '/', '4096', ')', '*', | 173 # '-', 'math.log', '(', '1', '-', 'elem', '/', '4096', ')', '*', |
173 # '4096', 'if', 'elem', '!=', 'bn', 'else', 'elem', '-', '0.5' | 174 # '4096', 'if', 'elem', '!=', '1', 'else', 'elem', '-', '0.5' |
174 # ] | 175 # ] |
175 # or | 176 # or |
176 # ['vec.median', '(', ')', '+', 'vec.sum', '(', ')'] | 177 # ['vec.median', '(', ')', '+', 'vec.sum', '(', ')'] |
177 tokens = [ | 178 tokens = [ |
178 e for e in re.split( | 179 e for e in re.split( |
179 r'([a-zA-Z0-9_.]+|[^a-zA-Z0-9_.() ]+|[()])', self.expr | 180 r'("[a-zA-Z%0-9_.]+"|[a-zA-Z0-9_.]+|[^a-zA-Z0-9_.() ]+|[()])', self.expr |
180 ) if e.strip() | 181 ) if e.strip() |
181 ] | 182 ] |
182 | 183 |
183 # 2. Subtract allowed standard tokens | 184 # 2. Subtract allowed standard tokens |
184 rem = [e for e in tokens if e not in self.__allowed_tokens] | 185 rem = [e for e in tokens if e not in self.__allowed_tokens] |
185 | |
186 # 3. Subtract allowed qualified objects from allowed modules | 186 # 3. Subtract allowed qualified objects from allowed modules |
187 # and whitelisted references and their attributes | 187 # and whitelisted references and their attributes |
188 rem2 = [] | 188 rem2 = [] |
189 for e in rem: | 189 for e in rem: |
190 parts = e.split('.') | 190 parts = e.split('.') |
192 if parts[0] in self.these: | 192 if parts[0] in self.these: |
193 continue | 193 continue |
194 if len(parts) == 2: | 194 if len(parts) == 2: |
195 if parts[0] in self.these: | 195 if parts[0] in self.these: |
196 parts[0] = '_this' | 196 parts[0] = '_this' |
197 elif parts[0] == "": | |
198 # e.g. '.T' gives ['','.T'] | |
199 # Here we assume that the blank part[0] refers to the | |
200 # self.ref_type (e.g. "pd.DataFrame"), and that | |
201 # the second part is a function of that type. | |
202 if parts[1] in self.allowed_qualified['_this']: | |
203 continue | |
204 | |
197 if parts[0] in self.allowed_qualified: | 205 if parts[0] in self.allowed_qualified: |
198 if parts[1] in self.allowed_qualified[parts[0]]: | 206 if parts[1] in self.allowed_qualified[parts[0]]: |
199 continue | 207 continue |
208 | |
200 rem2.append(e) | 209 rem2.append(e) |
201 | 210 |
202 # 4. Assert that rest are real numbers | 211 # Debug |
212 # for x in (tokens, rem, rem2):print(x) | |
213 | |
214 # 4. Assert that rest are real numbers or strings | |
203 e = '' | 215 e = '' |
204 for e in rem2: | 216 for e in rem2: |
205 try: | 217 try: |
206 _ = float(e) | 218 _ = float(e) |
207 except ValueError: | 219 except ValueError: |
208 safe = False | 220 # e.g. '"TEXT"' is okay. |
209 break | 221 if not(e[0] == '"' and e[-1] == '"'): |
222 safe = False | |
223 break | |
210 | 224 |
211 return safe, e | 225 return safe, e |