Skip to content

Commit 330e781

Browse files
collections appear to be working correctly
1 parent 6c2dce6 commit 330e781

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

pyvba/browser.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _generate(self):
102102
if value.type not in visited:
103103
visited[value.type] = []
104104

105-
if value not in visited[value.type]:
105+
if not any(map(lambda item: value.cf(item), visited[value.type])):
106106
visited[value.type].append(value)
107107

108108
def browse_all(self):
@@ -112,9 +112,20 @@ def browse_all(self):
112112

113113
# populate child browsers if not already visited
114114
for name, value in self._all.items():
115-
if isinstance(value, Browser) and value not in visited[value.type]:
115+
if isinstance(value, Browser) and not any(map(lambda item: value.cf(item), visited[value.type])):
116116
name.browse_all()
117117

118+
def cf(self, other) -> bool:
119+
"""Comparison alternative to __eq__.
120+
121+
The comparison avoids checking any Viewer instances and compares the values of the standard objects within.
122+
"""
123+
return super().cf(other) and all([
124+
a == b
125+
for a, b in zip(self._all.values(), other.all.values())
126+
if not isinstance(a, (Viewer, FunctionViewer, list)) and not isinstance(b, (Viewer, FunctionViewer, list))
127+
])
128+
118129
def regen(self):
119130
"""Regenerate the `all` property."""
120131
self._all = {}
@@ -134,6 +145,10 @@ def _generate(self):
134145
super()._generate()
135146
self._all['Item'] = self._items
136147

148+
# generate the Item list
149+
if len(self._items) > 0 and isinstance(self._items[0], Browser):
150+
_ = [item.all for item in self._items]
151+
137152
# add items to the visited dictionary
138153
for value in self._all['Item']:
139154
if isinstance(value, Viewer):
@@ -142,3 +157,4 @@ def _generate(self):
142157

143158
if value not in visited[value.type]:
144159
visited[value.type].append(value)
160+

pyvba/export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _generate_tag(self, elem, tabs: int = 0, **kwargs) -> str:
150150
tag = XMLExport.Tag(elem.name)
151151

152152
# check if in stack already
153-
if elem in stack:
153+
if any(map(lambda obj: elem.cf(obj), stack)):
154154
return tag.enclose('BrowserObject: See ancestors', tabs)
155155
else:
156156
stack.append(elem)
@@ -384,7 +384,7 @@ def _generate_vba(self, elem, tabs: int = 0, **kwargs) -> str:
384384

385385
if isinstance(elem, Browser):
386386
# check if in stack already
387-
if elem in stack:
387+
if any(map(lambda obj: elem.cf(obj), stack)):
388388
return "\t" * tabs + f"{{ \"{self.json_encode(elem.name)}\": \"BrowserObject: See ancestors\" }},\n"
389389
else:
390390
stack.append(elem)

pyvba/viewer.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,27 @@ def getattr(self, item):
9090

9191
return self.gettype(obj, item)
9292

93+
def cf(self, other) -> bool:
94+
"""Comparison alternative to __eq__.
95+
96+
The comparison avoids checking any Viewer instances and compares the values of the standard objects within.
97+
"""
98+
if type(self) != type(other):
99+
return False
100+
return self._type == other.type and self._name == other.name and self._objects == other.objects
101+
93102
@property
94103
def com(self):
95104
"""Return the COM object."""
96105
return self._com
97106

98107
@property
99-
def name(self):
108+
def name(self) -> str:
100109
"""Return the name of the COM object."""
101110
return self._name
102111

103112
@property
104-
def type(self):
113+
def type(self) -> str:
105114
"""Return the type of the object within the COM object."""
106115
return self._type
107116

@@ -121,7 +130,7 @@ def methods(self) -> list:
121130
return self._methods
122131

123132
@property
124-
def errors(self):
133+
def errors(self) -> dict:
125134
"""Return a dictionary in format {obj: Error}."""
126135
return self._errors
127136

0 commit comments

Comments
 (0)