Show More
@@ -62,19 +62,19 b' def show_hidden(str,show_all=False):' | |||||
62 | class NameSpace(object): |
|
62 | class NameSpace(object): | |
63 | """NameSpace holds the dictionary for a namespace and implements filtering |
|
63 | """NameSpace holds the dictionary for a namespace and implements filtering | |
64 | on name and types""" |
|
64 | on name and types""" | |
65 |
def __init__(self, |
|
65 | def __init__(self, ns_dict, name_pattern="*", type_pattern="all", | |
66 | show_all=True): |
|
66 | ignore_case=True, show_all=True): | |
67 | self.show_all = show_all #Hide names beginning with single _ |
|
67 | self.show_all = show_all #Hide names beginning with single _ | |
68 | self.object = obj |
|
|||
69 | self.name_pattern = name_pattern |
|
68 | self.name_pattern = name_pattern | |
70 | self.type_pattern = type_pattern |
|
69 | self.type_pattern = type_pattern | |
71 | self.ignore_case = ignore_case |
|
70 | self.ignore_case = ignore_case | |
|
71 | self._ns = ns_dict | |||
72 |
|
72 | |||
73 | # We should only match EXACT dicts here, so DON'T use isinstance() |
|
73 | @classmethod | |
74 | if type(obj) == types.DictType: |
|
74 | def from_object(cls, obj, *args, **kwargs): | |
75 | self._ns = obj |
|
75 | """Instantiate a namespace by constructing a dictionary of an object's | |
76 | else: |
|
76 | attributes. A class method, returns a new NameSpace instance.""" | |
77 |
|
|
77 | attrs = {} | |
78 |
|
|
78 | for key in dir2(obj): | |
79 |
|
|
79 | if isinstance(key, basestring): | |
80 |
|
|
80 | # This seemingly unnecessary try/except is actually needed | |
@@ -84,10 +84,10 b' class NameSpace(object):' | |||||
84 |
|
|
84 | # object's dictionary. Properties can actually do the same | |
85 |
|
|
85 | # thing. In particular, Traits use this pattern | |
86 |
|
|
86 | try: | |
87 |
|
|
87 | attrs[key] = getattr(obj,key) | |
88 |
|
|
88 | except AttributeError: | |
89 |
|
|
89 | pass | |
90 | self._ns = dict(kv) |
|
90 | return cls(attrs, *args, **kwargs) | |
91 |
|
91 | |||
92 | def get_ns(self): |
|
92 | def get_ns(self): | |
93 | """Return name space dictionary with objects matching type and name patterns.""" |
|
93 | """Return name space dictionary with objects matching type and name patterns.""" | |
@@ -123,8 +123,8 b' class NameSpace(object):' | |||||
123 | #TODO: Implement dictionary like access to filtered name space? |
|
123 | #TODO: Implement dictionary like access to filtered name space? | |
124 |
|
124 | |||
125 | def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=False): |
|
125 | def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=False): | |
126 |
"""Return dictionary of all objects in namespace that match |
|
126 | """Return dictionary of all objects in a namespace dictionary that match | |
127 | and filter.""" |
|
127 | type_pattern and filter.""" | |
128 | pattern_list=filter.split(".") |
|
128 | pattern_list=filter.split(".") | |
129 | if len(pattern_list)==1: |
|
129 | if len(pattern_list)==1: | |
130 | ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern=type_pattern, |
|
130 | ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern=type_pattern, | |
@@ -137,9 +137,27 b' def list_namespace(namespace,type_pattern,filter,ignore_case=False,show_all=Fals' | |||||
137 | ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern="all", |
|
137 | ns=NameSpace(namespace,name_pattern=pattern_list[0],type_pattern="all", | |
138 | ignore_case=ignore_case,show_all=show_all) |
|
138 | ignore_case=ignore_case,show_all=show_all) | |
139 | res={} |
|
139 | res={} | |
140 | nsdict=ns.ns |
|
140 | for name,obj in ns.ns.iteritems(): | |
141 | for name,obj in nsdict.iteritems(): |
|
141 | ns = list_object_namespace(obj, type_pattern, pattern_list[1:], | |
142 | ns=list_namespace(obj,type_pattern,".".join(pattern_list[1:]), |
|
142 | ignore_case=ignore_case, show_all=show_all) | |
|
143 | for inner_name, inner_obj in ns.iteritems(): | |||
|
144 | res["%s.%s"%(name,inner_name)]=inner_obj | |||
|
145 | return res | |||
|
146 | ||||
|
147 | def list_object_namespace(ns_obj, type_pattern, pattern_list, ignore_case=False, | |||
|
148 | show_all=False): | |||
|
149 | """Return dictionary of all attributes of an object which match type_pattern | |||
|
150 | and filter.""" | |||
|
151 | if len(pattern_list)==1: | |||
|
152 | ns=NameSpace.from_object(ns_obj, name_pattern=pattern_list[0], | |||
|
153 | type_pattern=type_pattern, ignore_case=ignore_case, show_all=show_all) | |||
|
154 | return ns.ns | |||
|
155 | else: | |||
|
156 | ns=NameSpace.from_object(ns_obj, name_pattern=pattern_list[0], | |||
|
157 | type_pattern="all", ignore_case=ignore_case, show_all=show_all) | |||
|
158 | res={} | |||
|
159 | for name,obj in ns.ns.iteritems(): | |||
|
160 | ns=list_object_namespace(obj, type_pattern, pattern_list[1:], | |||
143 | ignore_case=ignore_case,show_all=show_all) |
|
161 | ignore_case=ignore_case, show_all=show_all) | |
144 | for inner_name,inner_obj in ns.iteritems(): |
|
162 | for inner_name,inner_obj in ns.iteritems(): | |
145 | res["%s.%s"%(name,inner_name)]=inner_obj |
|
163 | res["%s.%s"%(name,inner_name)]=inner_obj |
General Comments 0
You need to be logged in to leave comments.
Login now