Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to save the state in a file #4

Open
Hi-Angel opened this issue Aug 16, 2015 · 4 comments
Open

Add a way to save the state in a file #4

Hi-Angel opened this issue Aug 16, 2015 · 4 comments

Comments

@Hi-Angel
Copy link

A feature request: it would be really nice to see a way to save the state to a file.

UNO API most often very hard to find something, also e.g PyUNO suffer of an awful architecture. So it would be a really great debugging facility if there was a way to save a state in a text file — this would allow to compare properties before and after some change in Office using a simple diff utility, and thus figure out for how to do something.

@hanya
Copy link
Owner

hanya commented Sep 10, 2015

You can write such function in MRI macro like the following.
Store the following code into a file placed in the directory specified at
Tools - Configuration... - Macros Directory.
After reloading the Macros menu of the MRI window, you can use the macro.

__all__ = [
    "store_state", 
]

import traceback
import uno
from mytools_Mri.unovalues import TypeClass, TypeClassGroups

def select_files(mri):
    def create(name):
        return mri.create_service(name, nocode=True)

    fp = create("com.sun.star.ui.dialogs.OfficeFilePicker")
    fp.initialize((1,))
    fp.appendFilter('All Files (*.*)', '*.*')
    fp.appendFilter('Text Files (*.txt)', '*.txt')
    fp.setCurrentFilter('All Files (*.*)')
    ret = None
    if fp.execute() == 1:
        ret = fp.getFiles()[0]
    fp.dispose()
    return ret


def store_state(mri):
    """ Store current
        Store property values of the current object into file. """
    file_url = select_files(mri)
    if file_url is None:
        return
    file_path = uno.fileUrlToSystemPath(file_url)

    NUMERIC = TypeClassGroups.NUMERIC
    STRING = TypeClassGroups.STR
    BOOLEAN = TypeClass.BOOLEAN
    entry = mri.current
    target = entry.target

    with open(file_path, "w") as f:
        imple_name = mri.engine.get_imple_name(entry)
        f.write("IMPLE_NAME: {}\n".format(imple_name))
        props = mri.engine.get_properties_info(entry)
        for prop in sorted(props):
            name = prop[0]
            value = prop[2]
            type = prop[1]
            type_class = prop[6]
            if type_class in NUMERIC or type_class == BOOLEAN:
                f.write("{}\t{}\t{}\n".format(name, type, value))
            elif type_class == STRING:
                try:
                    _value = getattr(target, name)
                except:
                    _value = value
                if _value is None:
                    _value = "None"
                else:
                    _value = _value.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")
                f.write("{}\t{}\t{}\n".format(name, type, _value.encode("utf-8")))
            else:
                f.write("{}\t{}\t{}\n".format(name, type, " "))

@Hi-Angel
Copy link
Author

Hi-Angel commented Oct 2, 2015

Okay, sorry for the long delay. I just tried to run it, and I'm getting an error:

com.sun.star.uno.RuntimeExceptionError during invoking function store_state in module file:///home/constantine/.config/libreoffice/4/user/Scripts/python/savestate.py (<class 'TypeError'>: store_state() missing 1 required positional argument: 'mri'
  /usr/lib/libreoffice/program/pythonscript.py:869 in function invoke() [ret = self.func( *args )]
)

Sorry, I don't know if there's some trick that should be used to execute the code — I'm just trying it from the «Tools → Macros → Run Macro…»

@hanya
Copy link
Owner

hanya commented Oct 3, 2015

It seems I've forgot to mention about how to execute the macro. It have to be executed through MRI.
Choose MRI window - Main menu - Macros - state.py - Store current.
You have to pu the script file in the Macros directory specified in the configuration of the MRI settings. See the post above where to pulace the code.

@Hi-Angel
Copy link
Author

Hi-Angel commented Oct 3, 2015

Ah, sorry, I was wondering btw why doesn't I see the option you mentioned in the first comment ☺ So, it meant to be in MRI.

Okay, I just tried it. It was a bit tricky — MRI for some reason doesn't save the directory path, and when I tried to create the directory that is by default written, MRI just ignores it.

But I found that I could save the macro to the

/home/constantine/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu325eqol1h.tmp_/MRI-1.2.5.oxt/macros/

And now everything works :з

However, it seems that the macro saves a small part of the state. I made a test document, saved the state, next applied some transformations — changed a style of the first paragraph, changed the first page style, made a direct formatting to the first paragraph… But the only moment when the state changed was when I tried to add more text — that was just a count of characters and words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants