fix myplugin JS example on_new_record function
[myslice.git] / manifold / util / misc.py
1 import os, glob, inspect
2 from types import StringTypes
3
4 def find_local_modules(filepath):
5     modules = []
6     for f in glob.glob(os.path.dirname(filepath)+"/*.py"):
7         name = os.path.basename(f)[:-3]
8         if name != '__init__':
9             modules.append(name)
10     return modules 
11
12 def make_list(elt):
13     if not elt or isinstance(elt, list):
14         return elt
15     if isinstance(elt, StringTypes):
16         return [elt]
17     if isinstance(elt, (tuple, set, frozenset)):
18         return list(elt)
19
20
21 # FROM: https://gist.github.com/techtonik/2151727
22 # Public Domain, i.e. feel free to copy/paste
23 # Considered a hack in Python 2
24
25 import inspect
26  
27 def caller_name(skip=2):
28     """Get a name of a caller in the format module.class.method
29     
30        `skip` specifies how many levels of stack to skip while getting caller
31        name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
32        
33        An empty string is returned if skipped levels exceed stack height
34     """
35     stack = inspect.stack()
36     start = 0 + skip
37     if len(stack) < start + 1:
38       return ''
39     parentframe = stack[start][0]    
40     
41     name = []
42     module = inspect.getmodule(parentframe)
43     # `modname` can be None when frame is executed directly in console
44     # TODO(techtonik): consider using __main__
45     if module:
46         name.append(module.__name__)
47     # detect classname
48     if 'self' in parentframe.f_locals:
49         # I don't know any way to detect call from the object method
50         # XXX: there seems to be no way to detect static method call - it will
51         #      be just a function call
52         name.append(parentframe.f_locals['self'].__class__.__name__)
53     codename = parentframe.f_code.co_name
54     if codename != '<module>':  # top level usually
55         name.append( codename ) # function or a method
56     del parentframe
57     return ".".join(name)
58
59 def is_sublist(x, y, shortcut=None):
60     if not shortcut: shortcut = []
61     if x == []: return (True, shortcut)
62     if y == []: return (False, None)
63     if x[0] == y[0]:
64         return is_sublist(x[1:],y[1:], shortcut)
65     else:
66         return is_sublist(x, y[1:], shortcut + [y[0]])