MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Development (https://www.mobileread.com/forums/forumdisplay.php?f=240)
-   -   Using image in plugin code (https://www.mobileread.com/forums/showthread.php?t=235505)

Jellby 03-09-2014 11:02 AM

Using image in plugin code
 
In the Prince PDF plugin I use two small images in the "about" dialog (second screnshot in that thread). However, I just noticed it doesn't work. In fact, the images are not read from the plugin file, but from the current directory, so I only see the images if I launch calibre from the directory where I'm developing the plugin (that's why I thought it worked before).

What I want is using the images in HTML code, which currently looks something like this:

Code:

<p style="margin:1em 0 0 0"><img src=":images/prince_icon.png"/> <i>Prince is copyrighted &copy; YesLogic Pty. Ltd.</i></p>
<p style="margin:0"><img src=":images/small_icon.png"/> This plugin is released under GPL v3.</p>

Is there some other way to write the src attribute such that it reads the images from the plugin file?

kovidgoyal 03-09-2014 01:09 PM

In what context are you using this HTML file? Just extract the images into a temp directory and use absolute paths to them, that way it will always work.

Jellby 03-09-2014 02:06 PM

It's just the text for the "about" or "help" dialog (inside the plugin). I simply define a variable with the HTML code:

Code:

help_txt = _('''
<h3 style="text-align:center">The Prince PDF Plugin</h3>
<p style="text-align:center"><i>Created by Jellby</i></p>
...
<p style="margin:1em 0 0 0"><i><img src=":images/prince_icon.png"/> Prince is copyrighted &copy; YesLogic Pty. Ltd.</i></p>
<p style="margin:0"><img src=":images/small_icon.png"/> This plugin is released under GPL v3.</p>
''')

and then show it with.

Code:

QMessageBox.about(self, _('About the Prince PDF Plugin'), help_txt)
How can I portably extract files to a temp dir inside the calibre framework?

kovidgoyal 03-09-2014 11:34 PM

Code:

from calibre.ptempfile import TemporaryDirectory

with TemporaryDirectory('xxx') as tdir:
  for x in ('one.jpg', 'two.jpg'):
      with open(os.path.join(tdir, x) as f:
        f.write(get_resources('images/' + x))
    about.exec_()

It is important to note that you should exec your dialog inside the outermost with block as the temp dir will be deleted when you exit the with block.

Jellby 03-10-2014 05:01 PM

Quote:

Originally Posted by kovidgoyal (Post 2783182)
Code:

from calibre.ptempfile import TemporaryDirectory

with TemporaryDirectory('xxx') as tdir:
  for x in ('one.jpg', 'two.jpg'):
      with open(os.path.join(tdir, x) as f:
        f.write(get_resources('images/' + x))
    about.exec_()


Thanks, make that:

Code:

with open(os.path.join(tdir, x),'w') as f:
;)

A minor, probably trivial question: How can I access the "version" defined in __init__ (or "name", or "author"...) from elsewhere inside the plugin code?

kovidgoyal 03-11-2014 12:11 AM

from calibre_plugins.<plugin_name> import version

Jellby 03-11-2014 03:48 PM

That works for __copyright__, which is defined outside the class, but not for version, which is inside:

Code:

from __future__ import (unicode_literals, division, absolute_import, print_function)

__license__  = 'GPL v3'
__copyright__ = '2014, Jellby <jellby@yahoo.com>'
__docformat__ = 'restructuredtext en'

from calibre.customize import InterfaceActionBase

load_translations()

class PrincePDFPlugin(InterfaceActionBase):
    name                    = _('Prince PDF')
    description            = _('Converts to PDF using the Prince software (third-party)')
    supported_platforms    = ['linux', 'windows']
    author                  = 'Jellby'
    version                = (1, 1, 3)
    minimum_calibre_version = (1, 16, 0)
    actual_plugin          = 'calibre_plugins.prince_pdf.ui:InterfacePlugin'

...

(ImportError: cannot import name version)

I must be doing something wrong.

kovidgoyal 03-11-2014 11:56 PM

So either define version outside the class like this

version = xxx

class MyClass:

version = version

or import the class

from calibre_plugins.plugin_name import MyClass

version = MyClass.version


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

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