MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Plugin Development (https://www.mobileread.com/forums/showthread.php?t=251452)

Doitsu 12-03-2014 08:30 AM

Quote:

Originally Posted by turbulent (Post 2992480)
In an output sigil plugin, is it possible to get the file name of the epub file currently opened by sigil? So it's convinient to name the generated file after the epub file.

If you have a Windows machine, you could use the win32gui library to find the Sigil title bar text. Here's a quick & dirty solution:

Code:

import win32gui
from win32gui import GetWindowText
class_name = 'Qt5QWindowIcon'
window_name = None
hwnd = win32gui.FindWindow(class_name, window_name)

if hwnd:
    print GetWindowText(hwnd)
    # returns 'untitled.epub - Sigil'

Note that this simple code will return the title bar text of the Qt app that was opened last. I.e. if you open multiple Sigil windows, you'll only get the title bar text of the last window and if you open Calibre (or any other Qt5 app) after Sigil, you'll get the Calibre title bar text.

For a more robust solution, you'll need to enumerate all windows using win32gui.EnumWindows.

KevinH 12-03-2014 10:11 AM

Hi,
You can of course get the title from the opf or doctitle from the ncx. One of the routines in the libgui I posted will give you a valid filename from a title, and you can use that to initialize the fileSaveAs gui element and let the user easily specify the location and change the name for your output file.

The ePub3-itizer plugin uses that approach if you are looking for an example.

Kevin

Doitsu 12-03-2014 10:45 AM

@KevinH: I also think that the book title would make for a better file name and I use it as the file name in my Kindlegen wrapper. My file name generation method is not as elegant as your method, though, but it works. :)
I merely mentioned the the win32gui library, because the OP specifically asked for a method of getting the file name.
(Since this method cannot be easily ported to Linux/OSX and all Sigil plugins should ideally work on all three platforms, I wouldn't use it for plugins uploaded to MR.)

KevinH 12-03-2014 05:52 PM

Hi All,

I have updated the first two posts with links to a new pre-release version of the launcher code for Sigil that will support Preferences. All thanks to DiapDealer for designing and implementing these new launcher features!

He has nicely created Plugin examples that illustrate their use. I have included a launcher_updater python program that will install the updates for you. This updater should only be run with stock Sigil 0.8.2.

If you have any questions just ask.

As new features are added to the launcher code, we will periodically making these pre-releases so that plugin developers can more easily test things out before the next Sigil Release is made.

Take care,

KevinH

Doitsu 12-03-2014 07:00 PM

Quote:

Originally Posted by KevinH (Post 2993013)
He has nicely created Plugin examples that illustrate their use. I have included a launcher_updater python program that will install the updates for you. This updater should only be run with stock Sigil 0.8.2.

I've just tested the launcher updater with DiapDealer's inofficial 0.8.2 Linux build and the official 64bit Windows 0.8.2 version. I got a 'Sigil launcher code updated successfully' message, but the Python files in /usr/local/share/sigil/plugin_launchers/python and C:\Program Files\Sigil\plugin_launchers\python weren't updated.

KevinH 12-03-2014 07:08 PM

Hi Doitsu,

Well that's not good. It works on my Mac OS X build.

On Mac it looks here:

_DIRS_TO_SEARCH.append("/Applications/Sigil.app/Contents/plugin_launchers/python")

On linux it will look in the following locations and can overide via an environment var:

_DIRS_TO_SEARCH.append("/usr/share/sigil/plugin_launchers/python")
_DIRS_TO_SEARCH.append("/usr/local/share/sigil/plugin_launchers/python")
_DIRS_TO_SEARCH.append("/opt/share/sigil/plugin_launchers/python")

On Windows it looks here:
Code:

    programfiles = os.environ.get("ProgramFiles")
    programfilesx86 = os.environ.get("ProgramFiles(x86)")
    if programfiles is not None:
        _DIRS_TO_SEARCH.append(os.path.join(programfiles,"Sigil","plugin_launchers","python"))
    if programfilesx86 is not None:
        _DIRS_TO_SEARCH.append(os.path.join(programfilesx86,"Sigil","plugin_launchers","python"))

Do you need to be admin/root to write to those locations on your machine? If so perhaps the python shutil.copy was simply ignored.

You might try an sudo on Linux to see if that fixes things or being Admin on Linux.

If that doesn't help, your best bet until I figure this out is manually copy the files. Don't forget the completely new file preferences.py.

Kevin


Quote:

Originally Posted by Doitsu (Post 2993068)
I've just tested the launcher updater with DiapDealer's inofficial 0.8.2 Linux build and the official 64bit Windows 0.8.2 version. I got a 'Sigil launcher code updated successfully' message, but the Python files in /usr/local/share/sigil/plugin_launchers/python and C:\Program Files\Sigil\plugin_launchers\python weren't updated.


Doitsu 12-03-2014 07:14 PM

Quote:

Originally Posted by KevinH (Post 2993079)
Do you need to be admin/root to write to those locations?

Yes on both counts. I even tried to execute Python as a root, but it didn't make a difference.

DiapDealer 12-03-2014 07:20 PM

@KevinH:

Yeah, it's not copying any files on Windows either.
It's only succeeding because technically ... nothing went wrong. :)

I've got it narrowed down to the call to unipath.walk("./payload") in __main__.py. It's returning an empty list instead of the files in the payload folder. Is it possible that something's different with accessing a folder "relatively" (./payload) inside a zipfile on Windows/Linux?

KevinH 12-03-2014 07:31 PM

Hi DiapDealer,

Forget about it. I just tried again and now it doesn't work on Macs either. I bet I ran it once from inside the directory and then tried it after zipping it up and it said successful and I looked and the files were there.

I don't think this is ever going to work the way I want it to.

So can you please try one thing:

Unzip the launcher_updater_20141204.zip but do not cd into it. Then try doing the following:

python launcher_updater_20141204

And yes we are just passing the folder name into python, but that is supposed to work as well. This does work on my Mac.

Kevin

Quote:

Originally Posted by DiapDealer (Post 2993098)
@KevinH:

Yeah, it's not copying any files on Windows either.
It's only succeeding because technically ... nothing went wrong. :)

I've got it narrowed down to the call to unipath.walk("./payload") in __main__.py. It's returning an empty list instead of the files in the payload folder. Is it possible that something's different with accessing a folder "relatively" (./payload) inside a zipfile on Windows/Linux?


DiapDealer 12-03-2014 08:28 PM

Quote:

Originally Posted by KevinH (Post 2993115)
So can you please try one thing:

Unzip the launcher_updater_20141204.zip but do not cd into it. Then try doing the following:

python launcher_updater_20141204

And yes we are just passing the folder name into python, but that is supposed to work as well. This does work on my Mac.

Same thing.
It SAYS everything went OK, but nothing gets copied and flist is empty.

KevinH 12-03-2014 08:35 PM

Hi DiapDealer,

Okay I tried one more time and put up a new version that uses full paths instead of relative paths.

This one now feeds back as it is copying files as well.

So unzip it
then try running:

python launcher_updater_20141204

So please give the new version a try (there is a new md5 sum as well).

If that doesn't work then I am going back to the drawing board!


Quote:

Originally Posted by DiapDealer (Post 2993161)
Same thing.
It SAYS everything went OK, but nothing gets copied and flist is empty.


DiapDealer 12-03-2014 10:11 PM

That worked!

Permission denied errors on Windows, but that's not surprising. Opening a command-prompt as Administrator takes care of that problem. I imagine Linux users would need to use sudo as well.

KevinH 12-03-2014 10:43 PM

Hi DiapDaler,

Whew! I am glad this one worked. I would rather it worked from the zip but that seems impossible. I will add info on the need to open a command prompt as Administrator on Windows, and use sudo on Linux.

Thanks so much for testing it and tracking down the error!

KevinH

Quote:

Originally Posted by DiapDealer (Post 2993220)
That worked!

Permission denied errors on Windows, but that's not surprising. Opening a command-prompt as Administrator takes care of that problem. I imagine Linux users would need to use sudo as well.


eschwartz 12-04-2014 12:42 AM

What about something like these, at least for linux?

https://codereview.stackexchange.com...ot-from-python
https://gist.github.com/davejamesmiller/1965559

Or at least do an euid check and error out with an appropriate message.

KevinH 12-04-2014 07:59 AM

Hi eschwartz,

I guess I could use it, but they only would work for unix/linux and not Windows. I'll think about it for the next time.

Thanks,

Kevin

Quote:

Originally Posted by eschwartz (Post 2993292)
What about something like these, at least for linux?

https://codereview.stackexchange.com...ot-from-python
https://gist.github.com/davejamesmiller/1965559

Or at least do an euid check and error out with an appropriate message.



All times are GMT -4. The time now is 08:24 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.