summaryrefslogtreecommitdiff
path: root/canonicalize/namesets.py
blob: 5523f44e82fede9a565eebbf23c3d640cfdf5a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import ast, collections

supportedLibraries = [ "string", "math", "random", "__future__", "copy" ]

builtInTypes = [ bool, bytes, complex, dict, float, int, list, 
                 set, str, tuple, type ]

staticTypeCastBuiltInFunctions = {
    "bool" : { (object,) : bool }, 
    "bytes" : bytes,
    "complex" : { (str, int) : complex, (str, float) : complex, (int, int) : complex,
                  (int, float) : complex, (float, int) : complex, (float, float) : complex },
    "dict" : { (collections.Iterable,) : dict }, 
    "enumerate" : { (collections.Iterable,) : enumerate },
    "float" : { (str,) : float, (int,) : float, (float,) : float },
    "frozenset" : frozenset, 
    "int" : { (str,) : int, (float,) : int, (int,) : int, (str, int) : int },
    "list" : { (collections.Iterable,) : list },
    "memoryview" : None, #TODO
    "object" : { () : object }, 
    "property" : property, #TODO
    "reversed" : { (str,) : reversed, (list,) : reversed },
    "set" : { () : None, (collections.Iterable,) : None }, #TODO
    "slice" : { (int,) : slice, (int, int) : slice, (int, int, int) : slice },
    "str" : { (object,) : str }, 
    "tuple" : { () : tuple, (collections.Iterable,) : tuple },
    "type" : { (object,) : type },
    }

mutatingTypeCastBuiltInFunctions = {
    "bytearray" : { () : list },
    "classmethod" : None,
    "file" : None,
    "staticmethod" : None, #TOOD
    "super" : None
    }

builtInNames = [ "None", "True", "False", "NotImplemented", "Ellipsis" ]

staticBuiltInFunctions = {    
    "abs" : { (int,) : int, (float,) : float }, 
    "all" : { (collections.Iterable,) : bool }, 
    "any" : { (collections.Iterable,) : bool }, 
    "bin" : { (int,) : str }, 
    "callable" : { (object,) : bool },
    "chr" : { (int,) : str }, 
    "cmp" : { (object, object) : int },
    "coerce" : tuple, #TODO
    "compile" : { (str, str, str) : ast, (ast, str, str) : ast },
    "dir" : { () : list },
    "divmod" : { (int, int) : tuple, (int, float) : tuple,
                 (float, int) : tuple, (float, float) : tuple },
    "filter" : { (type(lambda x : x), collections.Iterable) : list },
    "getattr" : None, 
    "globals" : dict, 
    "hasattr" : bool, #TODO
    "hash" : int, 
    "hex" : str, 
    "id" : int, #TODO
    "isinstance" : { (None, None) : bool }, 
    "issubclass" : bool, #TODO
    "iter" : { (collections.Iterable,) : None, (None, object) : None },
    "len" : { (str,) : int, (tuple,) : int, (list,) : int, (dict,) : int },
    "locals" : dict, 
    "map" : { (None, collections.Iterable) : list }, #TODO
    "max" : { (collections.Iterable,) : None },
    "min" : { (collections.Iterable,) : None },
    "oct" : { (int,) : str },
    "ord" : { (str,) : int },
    "pow" : { (int, int) : int, (int, float) : float,
              (float, int) : float, (float, float) : float },
    "print" : None, 
    "range" : { (int,) : list, (int, int) : list, (int, int, int) : list }, 
    "repr" : {(object,) : str },
    "round" : { (int,) : float, (float,) : float, (int, int) : float, (float, int) : float },
    "sorted" : { (collections.Iterable,) : list },
    "sum" : { (collections.Iterable,) : None },
    "vars" : dict, #TODO
    "zip" : { () : list, (collections.Iterable,) : list}
    }

mutatingBuiltInFunctions = {
    "__import__" : None,
    "apply" : None,
    "delattr" : { (object, str) : None }, 
    "eval" : { (str,) : None },
    "execfile" : None,
    "format" : None, 
    "input" : { () : None, (object,) : None }, 
    "intern" : str, #TOOD
    "next" : { () : None, (None,) : None },
    "open" : None,
    "raw_input" : { () : str, (object,) : str },
    "reduce" : None, 
    "reload" : None, 
    "setattr" : None
    }

builtInSafeFunctions = [
    "abs", "all", "any", "bin", "bool", "cmp", "len",
    "list", "max", "min", "pow", "repr", "round", "slice", "str", "type"
    ]


exceptionClasses = [
    "ArithmeticError", 
    "AssertionError", 
    "AttributeError", 
    "BaseException", 
    "BufferError", 
    "BytesWarning", 
    "DeprecationWarning", 
    "EOFError", 
    "EnvironmentError", 
    "Exception", 
    "FloatingPointError", 
    "FutureWarning", 
    "GeneratorExit", 
    "IOError", 
    "ImportError", 
    "ImportWarning",
    "IndentationError", 
    "IndexError", 
    "KeyError", 
    "KeyboardInterrupt", 
    "LookupError", 
    "MemoryError", 
    "NameError",
    "NotImplementedError", 
    "OSError", 
    "OverflowError", 
    "PendingDeprecationWarning", 
    "ReferenceError", 
    "RuntimeError", 
    "RuntimeWarning", 
    "StandardError",
    "StopIteration", 
    "SyntaxError", 
    "SyntaxWarning", 
    "SystemError", 
    "SystemExit", 
    "TabError",
    "TypeError", 
    "UnboundLocalError", 
    "UnicodeDecodeError", 
    "UnicodeEncodeError", 
    "UnicodeError", 
    "UnicodeTranslateError", 
    "UnicodeWarning", 
    "UserWarning", 
    "ValueError", 
    "Warning", 
    "ZeroDivisionError", 

    "WindowsError", "BlockingIOError", "ChildProcessError", 
    "ConnectionError", "BrokenPipeError", "ConnectionAbortedError", 
    "ConnectionRefusedError", "ConnectionResetError", "FileExistsError", 
    "FileNotFoundError", "InterruptedError", "IsADirectoryError", "NotADirectoryError", 
    "PermissionError", "ProcessLookupError", "TimeoutError", 
    "ResourceWarning", "RecursionError", "StopAsyncIteration" ]

builtInFunctions = dict(list(staticBuiltInFunctions.items()) + \
    list(mutatingBuiltInFunctions.items()) + \
    list(staticTypeCastBuiltInFunctions.items()) + \
    list(mutatingTypeCastBuiltInFunctions.items()))

# All string functions do not mutate the caller, they return copies instead
builtInStringFunctions = {
    "capitalize" : { () : str }, 
    "center" : { (int,) : str, (int, str) : str }, 
    "count" : { (str,) : int, (str, int) : int, (str, int, int) : int },
    "decode" : { () : str }, 
    "encode" : { () : str }, 
    "endswith" : { (str,) : bool },
    "expandtabs" : { () : str, (int,) : str },
    "find" : { (str,) : int, (str, int) : int, (str, int, int) : int },
    "format" : { (list, list) : str },
    "index" : { (str,) : int, (str,int) : int, (str,int,int) : int },
    "isalnum" : { () : bool }, 
    "isalpha" : { () : bool },
    "isdecimal" : { () : bool }, 
    "isdigit" : { () : bool }, 
    "islower" : { () : bool },
    "isnumeric" : { () : bool }, 
    "isspace" : { () : bool }, 
    "istitle" : { () : bool },
    "isupper" : { () : bool }, 
    "join" : { (collections.Iterable,) : str, (collections.Iterable,str) : str },
    "ljust" : { (int,) : str },
    "lower" : { () : str }, 
    "lstrip" : { () : str, (str,) : str },
    "partition" : { (str,) : tuple }, 
    "replace" : { (str, str) : str, (str, str, int) : str },
    "rfind" : { (str,) : int, (str,int) : int, (str,int,int) : int }, 
    "rindex" : { (str,) : int }, 
    "rjust" : { (int,) : str },
    "rpartition" : { (str,) : tuple }, 
    "rsplit" : { () : list }, 
    "rstrip" : { () : str },
    "split" : { () : list, (str,) : list, (str, int) : list }, 
    "splitlines" : { () : list },
    "startswith" : { (str,) : bool },
    "strip" : { () : str, (str,) : str }, 
    "swapcase" : { () : str }, 
    "title" : { () : str },
    "translate" : { (str,) : str }, 
    "upper" : { () : str },
    "zfill" : { (int,) : str }
    }

safeStringFunctions = [ 
    "capitalize", "center", "count", "endswith", "expandtabs", "find",
    "isalnum", "isalpha", "isdigit", "islower", "isspace", "istitle",
    "isupper", "join", "ljust", "lower", "lstrip", "partition", "replace",
    "rfind", "rjust", "rpartition", "rsplit", "rstrip", "split", "splitlines",
    "startswith", "strip", "swapcase", "title", "translate", "upper", "zfill",
    "isdecimal", "isnumeric"]

mutatingListFunctions = {
    "append" : { (object,) : None },
    "extend" : { (list,) : None }, 
    "insert" : { (int, object) : None },
    "remove" : { (object,) : None },
    "pop" : { () : None, (int,) : None },
    "sort" : { () : None },
    "reverse" : { () : None }
    }

staticListFunctions = {
    "index" : { (object,) : int, (object,int) : int, (object,int,int) : int }, 
    "count" : { (object,) : int, (object,int) : int, (object,int,int) : int }
    }

safeListFunctions = [ "append", "extend", "insert", "count", "sort", "reverse"]

builtInListFunctions = dict(list(mutatingListFunctions.items()) + list(staticListFunctions.items()))

staticDictFunctions = { 
    "get" : { (object,) : object, (object, object) : object }, 
    "items" : { () : list }
    }

builtInDictFunctions = staticDictFunctions

mathFunctions = {
    "ceil" : { (int,) : float, (float,) : float }, 
    "copysign" : { (int, int) : float, (int, float) : float,
                   (float, int) : float, (float, float) : float },
    "fabs" : { (int,) : float, (float,) : float }, 
    "factorial" : { (int,) : int, (float,) : int },
    "floor" : { (int,) : float, (float,) : float },
    "fmod" : { (int, int) : float, (int, float) : float, 
               (float, int) : float, (float, float) : float },
    "frexp" : int, 
    "fsum" : int, #TODO
    "isinf" : { (int,) : bool, (float,) : bool }, 
    "isnan" : { (int,) : bool, (float,) : bool },
    "ldexp" : int, 
    "modf" : tuple, 
    "trunc" : None, #TODO
    "exp" : { (int,) : float, (float,) : float }, 
    "expm1" : { (int,) : float, (float,) : float },
    "log" : { (int,) : float, (float,) : float, 
              (int,int) : float, (int,float) : float, 
              (float, int) : float, (float, float) : float },
    "log1p" : { (int,) : float, (float,) : float },
    "log10" : { (int,) : float, (float,) : float },
    "pow" : { (int, int) : float, (int, float) : float, 
              (float, int) : float, (float, float) : float },
    "sqrt" : { (int,) : float, (float,) : float }, 
    "acos" : { (int,) : float, (float,) : float },
    "asin" : { (int,) : float, (float,) : float }, 
    "atan" : { (int,) : float, (float,) : float },
    "atan2" : { (int,) : float, (float,) : float }, 
    "cos" : { (int,) : float, (float,) : float },
    "hypot" : { (int, int) : float, (int, float) : float, 
                (float, int) : float, (float, float) : float },
    "sin" : { (int,) : float, (float,) : float }, 
    "tan" : { (int,) : float, (float,) : float },
    "degrees" : { (int,) : float, (float,) : float }, 
    "radians" : { (int,) : float, (float,) : float },
    "acosh" : int, 
    "asinh" : int, 
    "atanh" : int, 
    "cosh" : int, 
    "sinh" : int, 
    "tanh" : int,#TODO
    "erf" : int, 
    "erfc" : int, 
    "gamma" : int, 
    "lgamma" : int #TODO
    }

safeMathFunctions = [ 
    "ceil", "copysign", "fabs", "floor", "fmod", "isinf", 
    "isnan", "exp", "expm1", "cos", "hypot", "sin", "tan", 
    "degrees", "radians" ]

randomFunctions = { 
    "seed" : { () : None, (collections.Hashable,) : None }, 
    "getstate" : { () : object },
    "setstate" : { (object,) : None }, 
    "jumpahead" : { (int,) : None }, 
    "getrandbits" : { (int,) : int },
    "randrange" : { (int,) : int, (int, int) : int, (int, int, int) : int },
    "randint" : { (int, int) : int }, 
    "choice" : { (collections.Iterable,) : object },
    "shuffle" : { (collections.Iterable,) : None, 
                  (collections.Iterable, type(lambda x : x)) : None },
    "sample" : { (collections.Iterable, int) : list }, 
    "random" : { () : float },
    "uniform" : { (float, float) : float }
    }

futureFunctions = {
    "nested_scopes" : None,
    "generators" : None,
    "division" : None,
    "absolute_import" : None,
    "with_statement" : None,
    "print_function" : None,
    "unicode_literals" : None
    }

copyFunctions = {
    "copy" : None,
    "deepcopy" : None
}

timeFunctions = { 
    "clock" : { () : float }, 
    "time" : { () : float }
    }

errorFunctions = { 
    "AssertionError" : { (str,) : object }
    }

allStaticFunctions = dict(list(staticBuiltInFunctions.items()) + list(staticTypeCastBuiltInFunctions.items()) + \
                            list(builtInStringFunctions.items()) + list(staticListFunctions.items()) + \
                            list(staticDictFunctions.items()) + list(mathFunctions.items()))

allMutatingFunctions = dict(list(mutatingBuiltInFunctions.items()) + list(mutatingTypeCastBuiltInFunctions.items()) + \
                            list(mutatingListFunctions.items()) + list(randomFunctions.items()) + list(timeFunctions.items()))

allPythonFunctions = dict(list(allStaticFunctions.items()) + list(allMutatingFunctions.items()))

safeLibraryMap = { "string" : [ "ascii_letters", "ascii_lowercase", "ascii_uppercase", 
                            "digits", "hexdigits", "letters", "lowercase", "octdigits", 
                            "punctuation", "printable", "uppercase", "whitespace", 
                            "capitalize", "expandtabs", "find", "rfind", "count", 
                            "lower", "split", "rsplit", "splitfields", "join", 
                            "joinfields", "lstrip", "rstrip", "strip", "swapcase", 
                            "upper", "ljust", "rjust", "center", "zfill", "replace"],
                "math" : [    "ceil", "copysign", "fabs", "floor", "fmod", 
                            "frexp", "fsum", "isinf", "isnan", "ldexp", "modf", "trunc", "exp", 
                            "expm1", "log", "log1p", "log10", "sqrt", "acos", "asin", 
                            "atan", "atan2", "cos", "hypot", "sin", "tan", "degrees", "radians", 
                            "acosh", "asinh", "atanh", "cosh", "sinh", "tanh", "erf", "erfc", 
                            "gamma", "lgamma", "pi", "e" ],
                "random" : [ ],
                "__future__" : ["nested_scopes", "generators", "division", "absolute_import",
                            "with_statement", "print_function", "unicode_literals"] }

libraryMap = { "string" : [ "ascii_letters", "ascii_lowercase", "ascii_uppercase", 
                            "digits", "hexdigits", "letters", "lowercase", "octdigits", 
                            "punctuation", "printable", "uppercase", "whitespace", 
                            "capwords", "maketrans", "atof", "atoi", "atol", "capitalize", 
                            "expandtabs", "find", "rfind", "index", "rindex", "count", 
                            "lower", "split", "rsplit", "splitfields", "join", 
                            "joinfields", "lstrip", "rstrip", "strip", "swapcase", 
                            "translate", "upper", "ljust", "rjust", "center", "zfill", 
                            "replace", "Template", "Formatter" ],
                "math" : [    "ceil", "copysign", "fabs", "factorial", "floor", "fmod", 
                            "frexp", "fsum", "isinf", "isnan", "ldexp", "modf", "trunc", "exp", 
                            "expm1", "log", "log1p", "log10", "pow", "sqrt", "acos", "asin", 
                            "atan", "atan2", "cos", "hypot", "sin", "tan", "degrees", "radians", 
                            "acosh", "asinh", "atanh", "cosh", "sinh", "tanh", "erf", "erfc", 
                            "gamma", "lgamma", "pi", "e" ],
                "random" : ["seed", "getstate", "setstate", "jumpahead", "getrandbits",
                            "randrange", "randrange", "randint", "choice", "shuffle", "sample",
                            "random", "uniform", "triangular", "betavariate", "expovariate", 
                            "gammavariate", "gauss", "lognormvariate", "normalvariate", 
                            "vonmisesvariate", "paretovariate", "weibullvariate", "WichmannHill", 
                            "whseed", "SystemRandom" ],
                "__future__" : ["nested_scopes", "generators", "division", "absolute_import",
                            "with_statement", "print_function", "unicode_literals"],
                "copy" : ["copy", "deepcopy"] }

libraryDictMap = { "string" : builtInStringFunctions,
                    "math" : mathFunctions,
                    "random" : randomFunctions,
                    "__future__" : futureFunctions,
                    "copy" : copyFunctions }

typeMethodMap = {"string" : ["capitalize", "center", "count", "decode", "encode", "endswith", 
                            "expandtabs", "find", "format", "index", "isalnum", "isalpha",
                            "isdigit", "islower", "isspace", "istitle", "isupper", "join",
                            "ljust", "lower", "lstrip", "partition", "replace", "rfind",
                            "rindex", "rjust", "rpartition", "rsplit", "rstrip", "split",
                            "splitlines", "startswith", "strip", "swapcase", "title",
                            "translate", "upper", "zfill"],
                "list" : [     "append", "extend", "count", "index", "insert", "pop", "remove",
                            "reverse", "sort"],
                "set" : [    "isdisjoint", "issubset", "issuperset", "union", "intersection",
                            "difference", "symmetric_difference", "update", "intersection_update",
                            "difference_update", "symmetric_difference_update", "add",
                            "remove", "discard", "pop", "clear"],
                "dict" : [    "iter", "clear", "copy", "fromkeys", "get", "has_key", "items",
                            "iteritems", "iterkeys", "itervalues", "keys", "pop", "popitem",
                            "setdefault", "update", "values", "viewitems", "viewkeys", 
                            "viewvalues"] }

astNames = {
        ast.Module : "Module", ast.Interactive : "Interactive Module", 
        ast.Expression : "Expression Module", ast.Suite : "Suite",

        ast.FunctionDef : "Function Definition",
        ast.ClassDef : "Class Definition", ast.Return : "Return",
        ast.Delete : "Delete", ast.Assign : "Assign",
        ast.AugAssign : "AugAssign", ast.For : "For",
        ast.While : "While", ast.If : "If", ast.With : "With",
        ast.Raise : "Raise",
        ast.Try : "Try", ast.Assert : "Assert",
        ast.Import : "Import", ast.ImportFrom : "Import From",
        ast.Global : "Global", ast.Expr : "Expression",
        ast.Pass : "Pass", ast.Break : "Break", ast.Continue : "Continue",

        ast.BoolOp : "Boolean Operation", ast.BinOp : "Binary Operation",
        ast.UnaryOp : "Unary Operation", ast.Lambda : "Lambda",
        ast.IfExp : "Ternary", ast.Dict : "Dictionary", ast.Set : "Set",
        ast.ListComp : "List Comprehension", ast.SetComp : "Set Comprehension",
        ast.DictComp : "Dict Comprehension", 
        ast.GeneratorExp : "Generator", ast.Yield : "Yield",
        ast.Compare : "Compare", ast.Call : "Call",
        ast.Num : "Number", ast.Str : "String", ast.Bytes : "Bytes", 
        ast.NameConstant : "Name Constant",
        ast.Attribute : "Attribute",
        ast.Subscript : "Subscript", ast.Name : "Name", ast.List : "List",
        ast.Tuple : "Tuple", ast.Starred : "Starred",

        ast.Load : "Load", ast.Store : "Store", ast.Del : "Delete",
        ast.AugLoad : "AugLoad", ast.AugStore : "AugStore",
        ast.Param : "Parameter",

        ast.Ellipsis : "Ellipsis", ast.Slice : "Slice",
        ast.ExtSlice : "ExtSlice", ast.Index : "Index",

        ast.And : "And", ast.Or : "Or", ast.Add : "Add", ast.Sub : "Subtract",
        ast.Mult : "Multiply", ast.Div : "Divide", ast.Mod : "Modulo",
        ast.Pow : "Power", ast.LShift : "Left Shift",
        ast.RShift : "Right Shift", ast.BitOr : "|", ast.BitXor : "^",
        ast.BitAnd : "&", ast.FloorDiv : "Integer Divide",
        ast.Invert : "Invert", ast.Not : "Not", ast.UAdd : "Unsigned Add",
        ast.USub : "Unsigned Subtract", ast.Eq : "==", ast.NotEq : "!=",
        ast.Lt : "<", ast.LtE : "<=", ast.Gt : ">", ast.GtE : ">=",
        ast.Is : "Is", ast.IsNot : "Is Not", ast.In : "In",
        ast.NotIn : "Not In",

        ast.comprehension: "Comprehension",
        ast.ExceptHandler : "Except Handler", ast.arguments : "Arguments", ast.arg : "Argument",
        ast.keyword : "Keyword", ast.alias : "Alias", ast.withitem : "With item"
        }