##// END OF EJS Templates
Jorgens patch for ? wildcard (single char globbing)
vivainio -
Show More
@@ -1,138 +1,138 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Support for wildcard pattern matching in object inspection.
3 3
4 4 $Id: OInspect.py 608 2005-07-06 17:52:32Z fperez $
5 5 """
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2005 JΓΆrgen Stenarson <jorgen.stenarson@bostream.nu>
9 9 #
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #*****************************************************************************
13 13
14 14 from IPython import Release
15 15 __author__ = "JΓΆrgen Stenarson <jorgen.stenarson@bostream.nu>"
16 16 __license__ = Release.license
17 17
18 18 import __builtin__
19 19 import exceptions
20 20 import pdb
21 21 import pprint
22 22 import re
23 23 import types
24 24
25 25 def create_typestr2type_dicts(dont_include_in_type2type2str=["lambda"]):
26 26 """Return dictionaries mapping lower case typename to type objects, from
27 27 the types package, and vice versa."""
28 28 typenamelist=[]
29 29 for tname in dir(types):
30 30 if tname[-4:]=="Type":
31 31 typenamelist.append(tname)
32 32 typestr2type={}
33 33 type2typestr={}
34 34 for tname in typenamelist:
35 35 name=tname[:-4].lower()
36 36 obj=getattr(types,tname)
37 37 typestr2type[name]=getattr(types,tname)
38 38 if name in dont_include_in_type2type2str:
39 39 type2typestr[obj]=name
40 40 return typestr2type,type2typestr
41 41
42 42 typestr2type,type2typestr=create_typestr2type_dicts()
43 43
44 44 def is_type(obj,typestr_or_type):
45 45 """is_type(obj,typestr_or_type) verifies if obj is of a certain type or
46 46 group of types takes strings as parameters of the for 'tuple'<->TupleType
47 47 'all' matches all types. TODO: Should be extended for choosing more than
48 48 one type
49 49 """
50 50 if typestr_or_type=="all":
51 51 return True
52 52 if type(typestr_or_type)==types.TypeType:
53 53 test_type=typestr_or_type
54 54 else:
55 55 test_type=typestr2type.get(typestr_or_type,False)
56 56 if test_type:
57 57 return isinstance(obj,test_type)
58 58 else:
59 59 return False
60 60
61 61 def show_hidden(str,show_all=False):
62 62 """Return true for strings starting with single _ if show_all is true."""
63 63 return show_all or str.startswith("__") or not str.startswith("_")
64 64
65 65
66 66 class NameSpace(object):
67 67 """NameSpace holds the dictionary for a namespace and implements filtering
68 68 on name and types"""
69 69 def __init__(self,obj,name_pattern="*",type_pattern="all",ignore_case=True,
70 70 show_all=True):
71 71 self.show_all = show_all #Hide names beginning with single _
72 72 self.object = obj
73 73 self.name_pattern = name_pattern
74 74 self.type_pattern = type_pattern
75 75 self.ignore_case = ignore_case
76 76
77 77 # We should only match EXACT dicts here, so DON'T use isinstance()
78 78 if type(obj) == types.DictType:
79 79 self._ns = obj
80 80 else:
81 81 self._ns = dict([(key,getattr(obj,key)) for key in dir(obj)
82 82 if isinstance(key, basestring)])
83 83
84 84 def get_ns(self):
85 85 """Return name space dictionary with objects matching type and name patterns."""
86 86 return self.filter(self.name_pattern,self.type_pattern)
87 87 ns=property(get_ns)
88 88
89 89 def get_ns_names(self):
90 90 """Return list of object names in namespace that match the patterns."""
91 91 return self.ns.keys()
92 92 ns_names=property(get_ns_names,doc="List of objects in name space that "
93 93 "match the type and name patterns.")
94 94
95 95 def filter(self,name_pattern,type_pattern):
96 96 """Return dictionary of filtered namespace."""
97 97 def glob_filter(lista,name_pattern,hidehidden,ignore_case):
98 98 """Return list of elements in lista that match pattern."""
99 pattern=name_pattern.replace("*",".*")
99 pattern=name_pattern.replace("*",".*").replace("?",".")
100 100 if ignore_case:
101 101 reg=re.compile(pattern+"$",re.I)
102 102 else:
103 103 reg=re.compile(pattern+"$")
104 104 result=[x for x in lista if reg.match(x) and show_hidden(x,hidehidden)]
105 105 return result
106 106 ns=self._ns
107 107 #Filter namespace by the name_pattern
108 108 all=[(x,ns[x]) for x in glob_filter(ns.keys(),name_pattern,
109 109 self.show_all,self.ignore_case)]
110 110 #Filter namespace by type_pattern
111 111 all=[(key,obj) for key,obj in all if is_type(obj,type_pattern)]
112 112 all=dict(all)
113 113 return all
114 114
115 115 #TODO: Implement dictionary like access to filtered name space?
116 116
117 117 def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=False):
118 118 """Return dictionary of all objects in namespace that matches type_pattern
119 119 and filter."""
120 120 pattern_list=filter.split(".")
121 121 if len(pattern_list)==1:
122 122 ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern=type_pattern,
123 123 ignore_case=ignore_case,show_all=show_all)
124 124 return ns.ns
125 125 else:
126 126 # This is where we can change if all objects should be searched or
127 127 # only modules. Just change the type_pattern to module to search only
128 128 # modules
129 129 ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern="all",
130 130 ignore_case=ignore_case,show_all=show_all)
131 131 res={}
132 132 nsdict=ns.ns
133 133 for name,obj in nsdict.iteritems():
134 134 ns=list_namespace(obj,type_pattern,".".join(pattern_list[1:]),
135 135 ignore_case=ignore_case,show_all=show_all)
136 136 for inner_name,inner_obj in ns.iteritems():
137 137 res["%s.%s"%(name,inner_name)]=inner_obj
138 138 return res
General Comments 0
You need to be logged in to leave comments. Login now