1
# -*- coding: utf-8 -*-
3
# Copyright 2008 Simon Edwards <simon@simonzone.com>
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.
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
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.
20
from PyQt4.QtCore import *
21
from PyQt4.QtGui import *
22
from PyKDE4.plasma import Plasma
23
import plasma_importer
26
class PythonDataEngineScript(Plasma.DataEngineScript):
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
36
self.moduleName = str(self.dataEngine().pluginName())
37
self.pluginName = 'dataengine_' + self.moduleName.replace('-','_')
39
PythonDataEngineScript.importer.register_top_level(self.pluginName, str(self.dataEngine().package().path()))
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]
54
self.pydataengine = self.module.__dict__[basename].CreateDataEngine(None)
55
self.pydataengine.setDataEngine(self.dataEngine())
56
self.pydataengine.setDataEngineScript(self)
57
self.pydataengine.init()
59
self.initialized = True
63
PythonDataEngineScript.importer.unregister_top_level(self.pluginName)
64
self.pydataengine = None
67
return self.pydataengine.sources()
69
def serviceForSource(self,source):
70
return self.pydataengine.serviceForSource(source)
72
def sourceRequestEvent(self,name):
73
return self.pydataengine.sourceRequestEvent(name)
75
def updateSourceEvent(self,source):
76
return self.pydataengine.updateSourceEvent(source)
78
def CreatePlugin(widget_parent, parent, component_data):
79
return PythonDataEngineScript(parent)