Show More
@@ -86,20 +86,30 b' class NameSpace(dict):' | |||||
86 | reg=re.compile(pattern+"$",re.I) |
|
86 | reg=re.compile(pattern+"$",re.I) | |
87 | else: |
|
87 | else: | |
88 | reg=re.compile(pattern+"$") |
|
88 | reg=re.compile(pattern+"$") | |
89 |
|
|
89 | ||
90 | return dict((key,obj) for key,obj in self.iteritems() if all((\ |
|
90 | # Check each one matches regex; shouldn't be hidden; of correct type. | |
91 | reg.match(key), # Matches pattern |
|
91 | return dict((key,obj) for key,obj in self.iteritems() if reg.match(key)\ | |
92 |
show_hidden(key, show_all) |
|
92 | and show_hidden(key, show_all)\ | |
93 |
is_type(obj, type_pattern) |
|
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 | """Return dictionary of all objects in a namespace dictionary that match |
|
96 | """Return dictionary of all objects in a namespace dictionary that match | |
97 | type_pattern and filter.""" |
|
97 | type_pattern and filter.""" | |
98 | pattern_list=filter.split(".") |
|
98 | pattern_list=filter.split(".") | |
99 | ns = NameSpace(namespace) |
|
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 | if len(pattern_list) == 1: |
|
110 | if len(pattern_list) == 1: | |
101 |
|
|
111 | return ns.filter(name_pattern=pattern_list[0], type_pattern=type_pattern, | |
102 | ignore_case=ignore_case, show_all=show_all) |
|
112 | ignore_case=ignore_case, show_all=show_all) | |
103 | else: |
|
113 | else: | |
104 | # This is where we can change if all objects should be searched or |
|
114 | # This is where we can change if all objects should be searched or | |
105 | # only modules. Just change the type_pattern to module to search only |
|
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 | ignore_case=ignore_case, show_all=show_all) |
|
118 | ignore_case=ignore_case, show_all=show_all) | |
109 | results = {} |
|
119 | results = {} | |
110 | for name, obj in filtered.iteritems(): |
|
120 | for name, obj in filtered.iteritems(): | |
111 |
ns = lis |
|
121 | ns = _list_namespace(NameSpace.from_object(obj), type_pattern, | |
112 |
|
|
122 | pattern_list[1:], ignore_case=ignore_case, show_all=show_all) | |
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) |
|
|||
132 | for inner_name,inner_obj in ns.iteritems(): |
|
123 | for inner_name,inner_obj in ns.iteritems(): | |
133 | results["%s.%s"%(name,inner_name)] = inner_obj |
|
124 | results["%s.%s"%(name,inner_name)] = inner_obj | |
134 | return results |
|
125 | return results |
General Comments 0
You need to be logged in to leave comments.
Login now