Save UNO state?

Often it is really hard to figure out for how to do something in UNO
unless someone already did it, and left a description on the Internet.
Even in the presence of MRI.

So I'm wondering: perhaps is there a way to save UNO state? So that
one could just save the state, change something they're interested in,
next again save the state. And the only thing that's left to do, is
just to peek at the difference between two files with a diff utility
to figure out the properties that were just changed.

Is your interest is in a specific object. For example, what properties changed on the first table.

I can easily compare properties for an object, but, that only deals with simple types such as strings and numbers. So, I can inspect an object and display the values of all the simple types with the attribute names and values. I can then save this in a text file, make a change to the object, then check the values again.

First problem is that I am only looking at simple types. Sure, I could change my inspection code to attempt to expand attributes that are other UNO services, but, I would need to watch for referential loops (like if a table refers to the owning document that would then refer to that table).

Well, I think at some level that all comes to a simple objects.

Anyway, how do you compare properties? E.g. I was recently needed to
change with UNO the first page style of Writer document. In the end it
turned out to be the property «PageDescName» (a string) of the first
paragraph, and that's just impossible to infer deductively (why
paragraph? I could imagine it would be e.g. TextCursor, but a
paragraph could easily span for multiple pages).

So, could you compare properties for a Writer document before and
after the first page style was manually changed, and find that the
«PageDescName» just did changed?

Okay, I reported an enhancement request; let's if it could be done
https://bugs.documentfoundation.org/show_bug.cgi?id=93516

Yes, you can certainly compare properties, but it would be a bit arduous to write with a bunch of special cases. Part of the problem is understanding which properties can be ignored, and which cannot.... and understanding how to write them. I wrote some code (not sure where it is) that copies a style. This is done one property at a time. I don't remember if I made it recursive or not. I probably did not, and, for this case, you would probably need to do that.

You would need to inspect the objects when possible and make comparison decisions based on that. It would also require error handling and special case code (probably). You would need to recognize when you had properties that you should not try to trace into if it is recursive in nature, which strikes me as the most difficult part.

Quick and dirty Basic routine to copy properties from obj1 to obj2:

Yay, that's actually cool :з So, I modified the code a bit to make it
Pythonish. It doesn't print all existing properties though, but I
modified it to print also at least every paragraph property. It is:

doc = desktop.loadComponentFromURL("file:///tmp/output.odt" ,"_blank", 0, ())
file = open('/tmp/log', 'w')
for p in doc.PropertySetInfo.getProperties():
    file.write("•••••••••••\n" + str(p).replace(',','\n') + "\n")
enum = doc.Text.createEnumeration()
while enum.hasMoreElements():
    file.write("•••••••••••\n" +
str(enum.nextElement().PropertySetInfo.getProperties()).replace(',','\n')
+ "\n")
file.close()

I am also replace commas with newlines to make it easier to
differentiate (otherwise there're very long lines). It's 16150 lines
from a simple test document btw :relaxed:

WTF are you trying to do? What is the purpose of comma separated machine
data in a word processor document? Why don't you save the shit in plain text
and then do whatever you want with it without struggling this horrible API?

What is the purpose of comma separated machine data in a word processor document?

That is to save in file. The more newlines, the easier differ the text
with vimdiff. Because if'd left these thousands symbols lines as is,
it would be really hard to see what just changed.

Why don't you save the shit in plain text and then do whatever you want with it without struggling this horrible API?

Aren't that exactly what I did in the code I just posted? Saved that
to the file "/tmp/log".

Okay, I wrote a python code that explores every property, and doesn't
fall into an infinite cycle. It is

def printUNO(parent, listChecked):
    try:
        for propName in dir(parent):
            if str(propName)[0].islower(): #function, skip
                continue
            try:
                property = getattr(parent, propName)
            except Exception: #inspect.UnknownPropertyException: wtf,
that doesn't work
                continue
            if str(property).startswith('pyuno'):
                if not any(propName == s for s in listChecked):
                    l = list(listChecked)
                    l.append(propName)
                    printUNO(property, l)
                continue
            print(propName + ': ' + str(property) + '\n')
    except Exception:
        return

The function accepts anything, and prints every property of the thing,
ignores functions (in PyUNO all functions starts with lower letter),
and recursively goes deeper and deeper keeping track of where it was
(in other to not visit the same property since these are sometimes
recursive).

However it produces too much output, e.g. from a test document it
produced 1gb of output. I think the problem is that most elements
still appears in output many times — the check that in backtrace
wasn't the current element is ensures only that it wouldn't fall in an
infinite cycle.

Hi-Angel wrote:

> Okay, I wrote a python code that explores every property, and doesn't
> fall into an infinite cycle. It is
> [...]
> However it produces too much output, e.g. from a test document it
> produced 1gb of output. I think the problem is that most elements
> still appears in output many times — the check that in backtrace
> wasn't the current element is ensures only that it wouldn't fall in an
> infinite cycle.

You can get an infinite cycle at any level, so only checking at the top level is not good enough. You will have to check at each node.

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)

Yay, that's actually cool! I added to my function also a maximum
level. With level 5 it produces ≈23Mb output which is a miserable
267604 lines, vimdiff works fine with that.

def printUNO(parent, listChecked, maxLevel):
    try:
        for propName in dir(parent):
            if str(propName)[0].islower(): #function, skip
                continue
            try:
                property = getattr(parent, propName)
            except Exception: #inspect.UnknownPropertyException: wtf,
that doesn't work
                continue
            if str(property).startswith('pyuno'):
                if ( not any(propName == s for s in listChecked)
                     and len(listChecked) < maxLevel):
                    l = list(listChecked)
                    l.append(propName)
                    printUNO(property, l, maxLevel)
                continue
            print(propName + ': ' + str(property) + '\n')
    except Exception: #in this awful API we can't even catch exception by name
        return

Although I still couldn't find where defined a global OutLineNumbering
of Writer, I think that code should work for most of tasks.