Re: How can I save a DiagramData to a dia file in the python plug-in?



At 27.03.2012 20:13, Daniel Katz wrote:
Hi -

If I have a DiagramData object (but not a Diagram object) in the python
plugin, is there a simple way to export that DiagramData out to a .dia file?

A simple way to do this from the GUI is attached, but it might be a bit too high level for your use case.


The context is this: I have an export filter that I've written using the
python plug-in. In the "normal operation" mode, everything is working fine
and I get an export to the format in question. What I was hoping to do is
to add some error reporting such that if the input Dia diagram had a
"semantic error" (in terms of the semantics of my diagrams), then I could
put out a new Dia diagram which was identical to the original but had the
offending object marked (e.g. by coloring it red).
With the attached example code it should be easy to do this by a dedicated Semantic Check menu call.

Since this is all part
of an export filter, I have the DiagramData through the interface of the
begin_render() method, but I don't have a Diagram object.

This does break for at least three reasons:
 - Diagram::save() and even the Diagram object is not available in
   the comand line case. See app/app_procs.c(do_convert) which only
   creates the DiagramData object.
 - even if a Diagram object would be created there still can't be
   any display in the non-GUI case.
 - using an additional export filter for the standard .dia extension is
   IMO asking for trouble.

I did try a few things:

My first thought was to get the Diagram object from dia.diagrams(), but
that turned out to be empty when I ran the filter from the command line as
'dia -t foo bar.dia'.

Yes, the list is filled during the Diagram objec construction, but as I said there is none for the standard command line case.

My second thought was to create a new Diagram object via dia.new(), but I
couldn't find a way to use my existing DiagramData object as the superclass
instance - it doesn't seem like dia.new() takes a DiagramData as an
argument, and I couldn't figure out a way to set my newDiagram.data to my
existing DiagramData from begin_render() after the fact.

The Diagram class is derived from the DiagramData class. So if there is a Diagram it is_a DIagramData object (the Diagram.data member only exists for compatibility reasons).

My third thought was to try to call the existing export filter for dia
files directly, but I couldn't figure out how to get a filter instance in
python. (I saw that in bindings/test.py there were references to
dia.filter_guess_export_filter()', but when I tried to invoke that function
through the Python Console dialog I got a missing attribute error - not
part of the python API, I guess.)

Yes. Although the stand-alone bindings are designed to be compatible with the PyDia plug-in not all methods/objects are available in both versions.

My fourth thought was to bypass the passed in DiagramData object and
instead use dia.load() to load a copy of the original input diagram
directly. This works (since begin_render() also gives me the output
filename and in my particular use case I can infer the full path of the
input file from the output filename and from there make a copy, dia.load()
it, modify object colors, save it, and so on), but it feels sort of
roundabout and the input-file finding is fragile.

IMHO it is your use case realization, which is kind of round about. Either invent some dedicated export format for your diagram verification (semantic check) or don't do the semantic check as an export filter.



So, any advice on a correct and robust way to do this?

As I said you may want to rethink your use case (realization). Mixing the simple save case with a semantic check feels odd to me. It is as if an IDE would not allow to save a source code file just becuase it does not compile.

Still a clean patch to expose the registered input/output filters with the PyDia plug-in would probably be accepted.

Another idea could be to expose some dedicated actions to be run from the command line although that might involve a command line mode which has to intialize the GUI.

HTH,
        Hans

-------- Hans "at" Breuer "dot" Org -----------
Tell me what you need, and I'll tell you how to
get along without it.                -- Dilbert

Attachment: test_diagram_save.py
Description: Text document



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]