Hi,
I wrote some code some time ago. The code also checks the differences between two states of an object.
The first part is similar to yours, it's also a recursive function, but it creates a dict and you can use a level to stop searching, so you won't get an infinite cycle.
The second part - I think, I used some code from stackoverflow - compares two nested dicts with each other and prints out the results.
It works best inside of an extension, where you can keep objects alive.
so it would be:
level = 2
self.res1 = get_attribs(object,level )
#do something
# and start the function again with:
self.res2 = get_attribs(object,level )
findDiff(self.res1, self.res2)
Regards,
Xaver
Here's the code:
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
doc = desktop.getCurrentComponent()
current_Contr = doc.CurrentController
viewcursor = current_Contr.ViewCursor
object = doc
max_lvl = 3
def get_attribs(obj,lvl):
results = {}
for key in dir(obj):
try:
value = getattr(obj, key)
if 'callable' in str(type(value)):
continue
except :
#print(key)
continue
if key not in results:
if type(value) in (
type(None),
type(True),
type(1),
type(.1),
type('string'),
type(()),
type([]),
type(b''),
type(r''),
type(u'')
):
results.update({key: value})
elif lvl < max_lvl:
try:
results.update({key: get_attribs(value,lvl+1)})
except:
pass
return results
diff = []
def findDiff(d1, d2, path=""):
for k in d1.keys():
if not d2.has_key(k):
print path, ":"
print k + " as key not in d2", "\n"
else:
if type(d1[k]) is dict:
if path == "":
path = k
else:
path = path + "->" + k
findDiff(d1[k],d2[k], path)
else:
if d1[k] != d2[k]:
diff.append((path,k,d1[k],d2[k]))
path = ''
res1 = get_attribs(object,1)
viewcursor.gotoEnd(False)
viewcursor.setString('Test ')
res2 = get_attribs(object,1)
findDiff(res1, res2)
for d in diff:
print(d)
time.sleep(.4)