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

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/python/plasma_importer.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
 
 
21
import sys
 
22
import os
 
23
import imp
 
24
 
 
25
class PlasmaImporter(object):
 
26
    def __init__(self):
 
27
        self.toplevel = {}
 
28
        self.toplevelcount = {}
 
29
        self.marker = '<plasma>' + str(id(self))
 
30
        sys.path.append(self.marker)
 
31
        sys.path_hooks.append(self.hook)
 
32
 
 
33
    def hook(self,path):
 
34
      if path==self.marker:
 
35
          return self
 
36
      else:
 
37
          raise ImportError()
 
38
 
 
39
    def register_top_level(self,name,path):
 
40
        if name not in self.toplevel:
 
41
            self.toplevelcount[name] = 1
 
42
            self.toplevel[name] = path
 
43
        else:
 
44
            self.toplevelcount[name] += 1
 
45
 
 
46
    def unregister_top_level(self,name):
 
47
        value = self.toplevelcount[name]-1
 
48
        self.toplevelcount[name] = value
 
49
        if value==0:
 
50
            del self.toplevelcount[name]
 
51
            del self.toplevel[name]
 
52
            for key in list(sys.modules.keys()):
 
53
                if key==name or key.startswith(name+"."):
 
54
                    del sys.modules[key]
 
55
 
 
56
    def find_module(self,fullname, path=None):
 
57
        location = self.find_pymod(fullname)
 
58
        if location is not None:
 
59
            return self
 
60
        else:
 
61
            return None
 
62
 
 
63
    # Find the given module in the plasma directory.
 
64
    # Result a tuple on success otherwise None. The tuple contains the location
 
65
    # of the module/package in disk and a boolean indicating if it is a package
 
66
    # or module
 
67
    def find_pymod(self,modulename):
 
68
        parts = modulename.split('.')
 
69
        toppackage = parts[0]
 
70
        rest = parts[1:]
 
71
 
 
72
        if toppackage in self.toplevel:
 
73
            path = self.toplevel[toppackage]
 
74
            if len(rest)==0:
 
75
                # Simple top level Plasma package
 
76
                return (path,True)
 
77
            else:
 
78
                restpath = apply(os.path.join,rest)
 
79
                fullpath = os.path.join(path,'contents','code',restpath)
 
80
                if os.path.isdir(fullpath):
 
81
                    return (fullpath,True)
 
82
                elif os.path.exists(fullpath+'.py'):
 
83
                    return (fullpath+'.py',False)
 
84
        else:
 
85
            return None
 
86
 
 
87
    def _get_code(self,location):
 
88
        return open(location).read()
 
89
 
 
90
    def load_module(self, fullname):
 
91
        location,ispkg = self.find_pymod(fullname)
 
92
        if ispkg:   # Package dir.
 
93
            initlocation = os.path.join(location,'__init__.py')
 
94
            code = None
 
95
            if os.path.isfile(initlocation):
 
96
                code = open(initlocation)
 
97
        else:
 
98
            code = open(location)
 
99
 
 
100
        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
 
101
        mod.__file__ = location #"<%s>" % self.__class__.__name__
 
102
        mod.__loader__ = self
 
103
        if ispkg:
 
104
            mod.__path__ = [self.marker]
 
105
        if code is not None:
 
106
            try:
 
107
                exec code in mod.__dict__
 
108
            finally:
 
109
                code.close()
 
110
        return mod
 
111
 
 
112
#plasma_importer = PlasmaImporter()
 
113
#plasma_importer.register_top_level('plasma_pyclock','/home/sbe/devel/python_plasma_2/test/plasma-pyclock')
 
114
#from  plasma_pyclock import main