Show More
@@ -86,20 +86,30 b' class NameSpace(dict):' | |||
|
86 | 86 | reg=re.compile(pattern+"$",re.I) |
|
87 | 87 | else: |
|
88 | 88 | reg=re.compile(pattern+"$") |
|
89 |
|
|
|
90 | return dict((key,obj) for key,obj in self.iteritems() if all((\ | |
|
91 | reg.match(key), # Matches pattern | |
|
92 |
show_hidden(key, show_all) |
|
|
93 |
is_type(obj, type_pattern) |
|
|
89 | ||
|
90 | # Check each one matches regex; shouldn't be hidden; of correct type. | |
|
91 | return dict((key,obj) for key,obj in self.iteritems() if reg.match(key)\ | |
|
92 | and show_hidden(key, show_all)\ | |
|
93 | and is_type(obj, type_pattern)) | |
|
94 | 94 | |
|
95 | def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=False): | |
|
95 | def list_namespace(namespace, type_pattern, filter, ignore_case=False, show_all=False): | |
|
96 | 96 | """Return dictionary of all objects in a namespace dictionary that match |
|
97 | 97 | type_pattern and filter.""" |
|
98 | 98 | pattern_list=filter.split(".") |
|
99 | 99 | ns = NameSpace(namespace) |
|
100 | return _list_namespace(ns, type_pattern, pattern_list, ignore_case,show_all) | |
|
101 | # This function is a more convenient wrapper around the recursive one below. | |
|
102 | ||
|
103 | def _list_namespace(ns, type_pattern, pattern_list, ignore_case=False, show_all=False): | |
|
104 | """Return dictionary of objects in a namespace which match type_pattern | |
|
105 | and filter (pattern_list). | |
|
106 | ||
|
107 | This is a recursive function behind list_namespace, which is intended to be | |
|
108 | the public interface. Unlike that function, this expects a NameSpace | |
|
109 | instance as the first argument, and the name pattern split by '.'s.""" | |
|
100 | 110 | if len(pattern_list) == 1: |
|
101 |
|
|
|
102 | ignore_case=ignore_case, show_all=show_all) | |
|
111 | return ns.filter(name_pattern=pattern_list[0], type_pattern=type_pattern, | |
|
112 | ignore_case=ignore_case, show_all=show_all) | |
|
103 | 113 | else: |
|
104 | 114 | # This is where we can change if all objects should be searched or |
|
105 | 115 | # only modules. Just change the type_pattern to module to search only |
@@ -108,27 +118,8 b' def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=Fals' | |||
|
108 | 118 | ignore_case=ignore_case, show_all=show_all) |
|
109 | 119 | results = {} |
|
110 | 120 | for name, obj in filtered.iteritems(): |
|
111 |
ns = lis |
|
|
112 |
|
|
|
113 | for inner_name, inner_obj in ns.iteritems(): | |
|
114 | results["%s.%s"%(name,inner_name)] = inner_obj | |
|
115 | return results | |
|
116 | ||
|
117 | def list_object_namespace(ns_obj, type_pattern, pattern_list, ignore_case=False, | |
|
118 | show_all=False): | |
|
119 | """Return dictionary of all attributes of an object which match type_pattern | |
|
120 | and filter (pattern_list).""" | |
|
121 | ns = NameSpace.from_object(ns_obj) | |
|
122 | if len(pattern_list) == 1: | |
|
123 | return ns.filter(name_pattern=pattern_list[0], type_pattern=type_pattern, | |
|
124 | ignore_case=ignore_case, show_all=show_all) | |
|
125 | else: | |
|
126 | filtered = ns.filter(name_pattern=pattern_list[0], type_pattern="all", | |
|
127 | ignore_case=ignore_case, show_all=show_all) | |
|
128 | results = {} | |
|
129 | for name, obj in filtered.iteritems(): | |
|
130 | ns = list_object_namespace(obj, type_pattern, pattern_list[1:], | |
|
131 | ignore_case=ignore_case, show_all=show_all) | |
|
121 | ns = _list_namespace(NameSpace.from_object(obj), type_pattern, | |
|
122 | pattern_list[1:], ignore_case=ignore_case, show_all=show_all) | |
|
132 | 123 | for inner_name,inner_obj in ns.iteritems(): |
|
133 | 124 | results["%s.%s"%(name,inner_name)] = inner_obj |
|
134 | 125 | return results |
General Comments 0
You need to be logged in to leave comments.
Login now