~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/python/pydataengine.py

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2008 Simon Edwards <simon@simonzone.com>
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU Library General Public License as
 
7
# published by the Free Software Foundation; either version 2, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details
 
14
#
 
15
# You should have received a copy of the GNU Library General Public
 
16
# License along with this program; if not, write to the
 
17
# Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
#
 
20
from PyQt4.QtCore import *
 
21
from PyQt4.QtGui import *
 
22
from PyKDE4.plasma import Plasma
 
23
import plasma_importer
 
24
import os.path
 
25
 
 
26
class PythonDataEngineScript(Plasma.DataEngineScript):
 
27
    importer = None
 
28
 
 
29
    def __init__(self, parent):
 
30
        Plasma.DataEngineScript.__init__(self,parent)
 
31
        if PythonDataEngineScript.importer is None:
 
32
            PythonDataEngineScript.importer = plasma_importer.PlasmaImporter()
 
33
        self.initialized = False
 
34
 
 
35
    def init(self):
 
36
        self.moduleName = str(self.dataEngine().pluginName())
 
37
        self.pluginName = 'dataengine_' + self.moduleName.replace('-','_')
 
38
 
 
39
        PythonDataEngineScript.importer.register_top_level(self.pluginName, str(self.dataEngine().package().path()))
 
40
 
 
41
        # import the code at the file name reported by mainScript()
 
42
        relpath = os.path.relpath(str(self.mainScript()),str(self.dataEngine().package().path()))
 
43
        if relpath.startswith("contents/code/"):
 
44
            relpath = relpath[14:]
 
45
        if relpath.endswith(".py"):
 
46
            relpath = relpath[:-3]
 
47
        relpath = relpath.replace("/",".")
 
48
        self.module = __import__(self.pluginName+'.'+relpath)
 
49
        # The script may not necessarily be called "main"
 
50
        # So we use __dict__ to look up the right name
 
51
        basename = os.path.basename(str(self.mainScript()))
 
52
        basename = os.path.splitext(basename)[0]
 
53
 
 
54
        self.pydataengine = self.module.__dict__[basename].CreateDataEngine(None)
 
55
        self.pydataengine.setDataEngine(self.dataEngine())
 
56
        self.pydataengine.setDataEngineScript(self)
 
57
        self.pydataengine.init()
 
58
 
 
59
        self.initialized = True
 
60
        return True
 
61
 
 
62
    def __dtor__(self):
 
63
        PythonDataEngineScript.importer.unregister_top_level(self.pluginName)
 
64
        self.pydataengine = None
 
65
 
 
66
    def sources(self):
 
67
        return self.pydataengine.sources()
 
68
 
 
69
    def serviceForSource(self,source):
 
70
        return self.pydataengine.serviceForSource(source)
 
71
 
 
72
    def sourceRequestEvent(self,name):
 
73
        return self.pydataengine.sourceRequestEvent(name)
 
74
 
 
75
    def updateSourceEvent(self,source):
 
76
        return self.pydataengine.updateSourceEvent(source)
 
77
 
 
78
def CreatePlugin(widget_parent, parent, component_data):
 
79
    return PythonDataEngineScript(parent)