GithubHelp home page GithubHelp logo

Add a way to save the state in a file about mri HOT 4 OPEN

hanya avatar hanya commented on September 2, 2024
Add a way to save the state in a file

from mri.

Comments (4)

hanya avatar hanya commented on September 2, 2024

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, " "))

from mri.

Hi-Angel avatar Hi-Angel commented on September 2, 2024

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…»

from mri.

hanya avatar hanya commented on September 2, 2024

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.

from mri.

Hi-Angel avatar Hi-Angel commented on September 2, 2024

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.

from mri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.