View Single Post
Old 04-12-2014, 11:56 AM   #1
varlog
actually it is /var/log
varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.varlog ought to be getting tired of karma fortunes by now.
 
varlog's Avatar
 
Posts: 341
Karma: 2994236
Join Date: Sep 2012
Location: usually Europa
Device: prs t1
Sigil script manager

Introducing DGSM.
Spoiler:

This assumes that some of you will be able and willing to play with it.

I'm writing some code for Sigil - to scratch my itch and to learn some C++, which I wanted to do for a considerable time now. As C was many many years ago my (unreturned ) love at first sight, I had thought embracing C++ would be the next step to digital nirvana, but I chickened out up to now. Well - now, almost half a year later digital nirvana is nowhere to be seen but I think I'll code from now on in C++. And probably Qt too - in a way of user documentation Qt is something of developer wet dream.

The itch comes from this discussion.
It is learn-by-doing endeavor - I've just started to do something I have no idea about and my plan evolves as I go. The general plan was to equip Sigil with plugin functionality, which was thought of but never emerged.

So as I go my knowledge of C++/Qt/Sigil/XML/Epub grows, so the project mutates. Actual code is something like second full rewrite and many many more partial rewrites. Actually I'm continuously rewriting.

I'm mostly only adding to original code - the Sigil original functionality is at the moment off limits for me.

I do it using Qt Creator in Linux (Ubuntu 12.04 64b) and I have no idea if it works on other platforms. I do try to use what I hold for generic C++/Qt so there is a good chance that that it almost not platform dependent? This is one of the secondary reasons for this expose - I'm curious how much Qt really abstracts.

Disclaimer: I am not willing to set up c++ development environment in Windows just for this project... and I'll never ever posses an Apple device: doesn't agree with my ambiance .

This is not! plugin interface for Sigil. It is just DUPA or ASS: Dumb Unified Processing Addition or Additional Scripting Supplement... actually it is DGSM: Dumb Generic Script Manager. Let's stick with it.

DGSM allows to execute from within Sigil a sort of call "[interpreter][parameters] scriptName[.scriptSuffix] [parameters]". Similar to Sigil context menu "Open with" function, just trying to be more general.
Theoretically interpreter could be anything: bash, cmd, python, Java, JavaScript, Basic... whatever. I concentrated in my tests on Python - as it seems to be language of choice in this environment.
It exposes to invoked scrip some internal data structures of Sigil, temporary Sigil folder mostly and actually selected items, and tries to react to the eventual changes the script has made.
The communication between script and Sigil is at the moment very unsophisticated one: script has to have XML manifest: scriptName.xml where script exposes itself and all its necessities.

The actual Sigil script XML manifest definition is:

tag "sigilScript": root tag
|has three attributes:
|interpreter=name of interpreter program ("python", "java"... etc)
|internal="true"/"false" -at the moment ignored! to be used for real plugins! if ever...
|resident="true"/"false" - should it appear in some Sigil menu?
|includes tags:
|tag "name": name of the script, necessary, no spaces, letters/numbers only!
|tag "author": author
|tag "description": short descriptions
|tag "files": script file tree
||includes:
||tag "file" : name of file
|||has one optional attribute "type":
|||type="entry"/"icon":
|||entry: entry script, necessary for one file!
|||icon: icon for menu, not implemented yet.
|||one file has to have attribute "entry"
||tag "dir":
|||has one no-optional attribute:
|||name="name of the folder"
|||to be on the safe side don't use spaces, letters/numbers only!
|||tag "dir" could include tag "file" and tag "dir" (scary... recursion!).
|tag "requires":read as "script requires"
||includes tags:
||tag "sigilMenu": menu text, should be human readable script name.
|||has one optional attribute "menu":
|||menu="menuBar"/"context"/"none" - what menu should script appear in
||tag "sigilResource": should be one of:
|||noResource/generic/text/xml/html/css/image/svg
|||/font/opf/ncx/miscText/audio/video/book/selected/all -
|||what resource script needs from Sigil
||tag "sigilAction": noAction/reloadResource/markBookChanged/reloadBook/:
|||what should Sigil do after the script is done

Not all is implemented yet of course. Perhaps not all should be, perhaps I've missed something essential, perhaps it is all beside the point: answer to this questions is one of primary reasons for this expose.

I've ported all known to me Sigil scripts to DGSM. Just to see what is possible I did minimalistic port of Mobi_Unpack_v053 too: it is supposed to read .mobi (and other formats?) directly into Sigil. As I don't know Python (Python sucks: I miss my lovely curly braces, indent rule is major PITA!, use PyDev in Eclipse, better ideas?) I've done a poor job of it. I've tested it on some .mobi .azw .prc files and it works on my test files in my enviroment, but there be sure dragons there.
I've put all those scripts in file pluginsDGSM-0.0.0.zip. You will find there also the actual XML schema of the script manifest: sigilScript.xsd - subject to alternations!

Global things:
I've changed C++ standard: C++x0 is now set up in cmake, C++98 won't compile now (initializations, lambda functions I wanted to explore but haven't)! Because of that - narrowing conversions C++0x error - I had to modify (scary,scary!) Sigil/src/Xerces/xercesc/util/regx/ParserForXMLSchema.cpp and Sigil/src/Xerces/xercesc/util/regx/RegxParser.cpp.

Qt version: I use 5.2.1 - but it should not matter.
The number of scripts handled is theoretically 999 (settings thing, easy to change).

Eventual memory leaks could've escaped me. Threads and concurrency I'll be glad to learn of. I'm trying to use existing Sigil functionality as I'm unraveling it - sometimes it's revelation, sometimes PITA.

I've added three new classes: ScriptWidget, ScriptManager and ScriptInterface. I've modified MainWindow, BookBrowser and Preferences to accommodate them.
MainWindow and BookBrowser include now script functionality elements. Depending on sigilScript resident attribute script will appear - depending on sigilMenu menu attribute under Scripts menu on menu bar or in right mouse click context menu.

Preferences accommodates ScriptWidged.
ScriptWidget is responsible for panel Edit->Preferences->Scripts which is front end for ScriptManager class. You can add/remove interpreter, set path to it, set path to script folder, install/remove/execute scripts with it. There are default values for paths which function perfectly for my Linux and should hopefully do something reasonable in other environments?.

ScriptInterface is responsible for executing script. This is the primary reason for this expose. What is the minimalistic useful functionality? My temporar goal was to be able to execute all known to me Sigil scripts. It is surly not enough.

This code is not even pre-alpha: it is just feasibility study/learning project. Don't use it in production environment! The sanity checks are sparse. After I managed to erase my home directory I've build in some - so read carefully any pop up messages before clicking OK.

After starting your successfully(?) compiled DGSM-Sigil you should import included (unzip first!) scripts through Edit->Preferences->Scripts->install button: the interpreter will be created automatically, you must check the "Interpreter Path" value and eventually per "Browse" or typing in the path point to the interpreter executable and set your preferred interpreter parameters. Alternatively you can set up interpreter per "New interpreter" button and than point "Scripts Path" per "Browse" or typing in the path to folder containing unzipped scripts. I included only python scripts, so it's python you'll want to configure. And than you can start to play with it.

The included patch is based on at the moment latest Sigil git commit.

Depending on response I'll consider future actions.

In case somebody wonders: YES, I'm aware that in the meantime Calibre has an editor with functionality approaching Sigil level, growing fast. It doesn't concern me, not my itch.

Last but not least: If you have not understood it - just forget it!
If you have doubt(s) or are curious - well, feel free to ask...

The pictures:

About informs you now that DGSM has proudly reached version 0.0.0
Next you see Script Manager Cockpit.
Script menu in menu bar.
Context script menu.
Attached Thumbnails
Click image for larger version

Name:	01_about.jpg
Views:	446
Size:	23.0 KB
ID:	121619   Click image for larger version

Name:	02_ScriptWidget.jpg
Views:	450
Size:	94.7 KB
ID:	121620   Click image for larger version

Name:	03_scripts_menu.jpg
Views:	414
Size:	78.1 KB
ID:	121621   Click image for larger version

Name:	04_context_menu.jpg
Views:	412
Size:	94.8 KB
ID:	121622  
Attached Files
File Type: zip DSGM-0.0.2.zip (29.9 KB, 161 views)
File Type: zip pluginsDSGM-0.0.2.zip (334.8 KB, 160 views)
File Type: zip DGSM-0.0.3.patch.zip (23.6 KB, 156 views)

Last edited by varlog; 07-26-2014 at 07:53 AM. Reason: added DGSM-0.0.3, which is DGSM-0.0.2 rebased on latest KevinH work
varlog is offline   Reply With Quote