Re: Dia+Python from the command line? (was: does Dia have a Python script interface?)



When dia starts, files in python dir are "de facto" executed (or at least in my tests).
But, as you will see, there is not a clean way to run a dia python script from command line (or i dont know how).
I made a little test script, it is linux oriented.

- copy text and paste in  file "~/.dia/example.py"
- i assume that the dia file to be processed by script is "/tmp/mydiagram.dia"

Here comes the dirty code

    import dia

    # this line, in interactive mode, generates
    # WARNING **: Can't find list for recent-files when adding /tmp/mydiagram.dia
    # but seems to work
    dia.load("/tmp/mydiagram.dia")

    # open a file for some output
    tmpfile = open("/tmp/mytmpfile", "a")
    tmpfile.write("hello world from dia\n")

    # this line doesn't work and breaks the script
    #tmpfile.write("found diagrams " + len(dia.diagrams()) + "\n")

    # notice that diagram file passed in command line is not loaded
    # when this code is executed
    tmpfile.write("loaded diagram " + str(dia.diagrams()[0].filename) + "\n")

    # this line doesn't work too and breaks the script
    #tmpfile.write("num layers " + len(dia.diagrams()[0].data.layers) + "\n")

    tmpfile.write("first layer name " + str(dia.diagrams()[0].data.layers[0].name))

    tmpfile.close()


After that, there is a more dirty way to answer Martin's question.
You can run the script non interactively  with this dirty command line

    $ dia -e /tmp/filetodelete.dia -t dia emptydiagram.dia

"emptydiagram.dia" can be any valid dia file, it will not be used by example.py script (not visible in script when it is executed).
"/tmp/filetodelete.dia" can be removed when command ends, it is necessary for executing dia in export mode from command line without GUI. 
"-t dia" again this parameter is just for not executing Dia wit GUI.

When dia is launched (with or without GUI) the script example.py is executed and with dia python class other diagram files can be loaded.
In my script i just check the name of first layer in diagram file containing data to be processed, dont know if accessing shapes in diagram works but it can be easily tested.

Notice that when script example.py is executed, any diagram file passed as parameter is not still loaded or accessible by dia python class (or at least in my test). 
Here comes the need to load the diagram with your data directly in example.py script (this can be parametrized with some little python coding and environment variables or temporary files or what your hackitude can imagine)

I know the code is dirty and the solution is quite a trick.

A cleaner solution but not using Dia program or dia python plugin is a good xml parser and XPath. Martin, If you really need something like that, i made a php lib for accessing dia files: it is on launchpad, it is open source but 1/3 of the documentation is not in english (workin on it). Send me an email for more info, i dont think this is the place to advertise my work.

Hope to be useful and sorry for bad english

Carlo


2010/6/16 W. Martin Borgert <debacle debian org>
Quoting "Carlo Calderoni" <calderoni carlo gmail com>:
You can write python scripts and  put them in "~/.dia/python" directory. You
can call them via dia python console or map them in dia menu entries.

Btw, is there a way to execute a Python plugin from the command line?
Assume, I have a diagram showing the structure of a local network,
and all servers have their name in the diagram. A Python script
could extract the names and list them, for example. Is it possible
to do that without using the Dia GUI? TIA.

_______________________________________________
dia-list mailing list
dia-list gnome org
http://mail.gnome.org/mailman/listinfo/dia-list
FAQ at http://live.gnome.org/Dia/Faq
Main page at http://live.gnome.org/Dia




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