5

Is there a built in function in PyQGIS to import the values from a plugin's metadata.txt?

I am developing a plugin and want to show the version corresponding to the value from metadata.txt. Is there a native way to get the values or would I have to parse the file by myself?

1 Answer 1

9

No native way by my knowledge. Last time I used the fine code with a ConfigParser from Dockable MirrorMap plugin by Faunalia for an aboutbox.

import ConfigParser
import os
import platform

try:
    import resources
except ImportError:
    import resources_rc

class DlgAbout(QDialog, Ui_DlgAbout):

    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)

        config = ConfigParser.ConfigParser()
        config.read(os.path.join(os.path.dirname(__file__),'metadata.txt'))

        name        = config.get('general', 'name')
        description = config.get('general', 'description')
        version     = config.get('general', 'version')

        self.logo.setPixmap( QPixmap( ":/faunalia/logo" ) )
        self.title.setText( name )
        self.description.setText( description )

        text = self.txt.toHtml()
        text = text.replace( "$PLUGIN_NAME$", name )

        subject = "Help: %s" % name
        body = """\n\n
--------
Plugin name: %s
Plugin version: %s
Python version: %s
Platform: %s - %s
--------
""" % ( name, version, platform.python_version(), platform.system(), platform.version() )

        mail = QUrl( "mailto:[email protected]" )
        mail.addQueryItem( "subject", subject )
        mail.addQueryItem( "body", body )

        text = text.replace( "$MAIL_SUBJECT$", unicode(mail.encodedQueryItemValue( "subject" )) )
        text = text.replace( "$MAIL_BODY$", unicode(mail.encodedQueryItemValue( "body" )) )

        self.txt.setHtml(text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.