File: loader.py

package info (click to toggle)
mozilla-firefox 1.0.4-2sarge17
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 255,356 kB
  • ctags: 267,207
  • sloc: cpp: 1,623,961; ansic: 792,828; xml: 85,380; makefile: 41,934; perl: 27,802; asm: 14,884; sh: 14,807; cs: 4,507; python: 4,398; java: 4,004; yacc: 1,380; lex: 409; pascal: 354; php: 244; csh: 132; objc: 73; ada: 44; sql: 4
file content (203 lines) | stat: -rw-r--r-- 8,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is the Python XPCOM language bindings.
#
# The Initial Developer of the Original Code is ActiveState Tool Corp.
# Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
# ActiveState Tool Corp.  All Rights Reserved.
#
# Contributor(s): Mark Hammond <MarkH@ActiveState.com> (original author)
#

import xpcom
from xpcom import components

import module

import glob, os, types
import traceback

from xpcom.client import Component

# Until we get interface constants.
When_Startup = 0
When_Component = 1
When_Timer = 2

def _has_good_attr(object, attr):
    # Actually allows "None" to be specified to disable inherited attributes.
    return getattr(object, attr, None) is not None

def FindCOMComponents(py_module):
    # For now, just run over all classes looking for likely candidates.
    comps = []
    for name, object in py_module.__dict__.items():
        if type(object)==types.ClassType and \
           _has_good_attr(object, "_com_interfaces_") and \
           _has_good_attr(object, "_reg_clsid_") and \
           _has_good_attr(object, "_reg_contractid_"):
            comps.append(object)
    return comps

def register_self(klass, compMgr, location, registryLocation, componentType):
    pcl = PythonComponentLoader
    from xpcom import _xpcom
    svc = _xpcom.GetGlobalServiceManager().getServiceByContractID("@mozilla.org/categorymanager;1", components.interfaces.nsICategoryManager)
    svc.addCategoryEntry("component-loader", pcl._reg_component_type_, pcl._reg_contractid_, 1, 1)

class PythonComponentLoader:
    _com_interfaces_ = components.interfaces.nsIComponentLoader
    _reg_clsid_ = "{63B68B1E-3E62-45f0-98E3-5E0B5797970C}" # Never copy these!
    _reg_contractid_ = "moz.pyloader.1"
    _reg_desc_ = "Python component loader"
    # Optional function which performs additional special registration
    # Appears that no special unregistration is needed for ComponentLoaders, hence no unregister function.
    _reg_registrar_ = (register_self,None)
    # Custom attributes for ComponentLoader registration.
    _reg_component_type_ = "script/python"

    def __init__(self):
        self.com_modules = {} # Keyed by module's FQN as obtained from nsIFile.path
        self.moduleFactory = module.Module
        self.num_modules_this_register = 0

    def _getCOMModuleForLocation(self, componentFile):
        fqn = componentFile.path
        mod = self.com_modules.get(fqn)
        if mod is not None:
            return mod
        import ihooks, sys
        base_name = os.path.splitext(os.path.basename(fqn))[0]
        loader = ihooks.ModuleLoader()

        module_name_in_sys = "component:%s" % (base_name,)
        stuff = loader.find_module(base_name, [componentFile.parent.path])
        assert stuff is not None, "Couldnt find the module '%s'" % (base_name,)
        py_mod = loader.load_module( module_name_in_sys, stuff )

        # Make and remember the COM module.
        comps = FindCOMComponents(py_mod)
        mod = self.moduleFactory(comps)
        
        self.com_modules[fqn] = mod
        return mod
        
    def getFactory(self, clsid, location, type):
        # return the factory
        assert type == self._reg_component_type_, "Being asked to create an object not of my type:%s" % (type,)
        file_interface = components.manager.specForRegistryLocation(location)
        # delegate to the module.
        m = self._getCOMModuleForLocation(file_interface)
        return m.getClassObject(components.manager, clsid, components.interfaces.nsIFactory)

    def init(self, comp_mgr, registry):
        # void
        self.comp_mgr = comp_mgr
        if xpcom.verbose:
            print "Python component loader init() called"

     # Called when a component of the appropriate type is registered,
     # to give the component loader an opportunity to do things like
     # annotate the registry and such.
    def onRegister (self, clsid, type, className, proId, location, replace, persist):
        if xpcom.verbose:
            print "Python component loader - onRegister() called"

    def autoRegisterComponents (self, when, directory):
        directory_path = directory.path
        self.num_modules_this_register = 0
        if xpcom.verbose:
            print "Auto-registering all Python components in", directory_path

        # ToDo - work out the right thing here
        # eg - do we recurse?
        # - do we support packages?
        entries = directory.directoryEntries
        while entries.HasMoreElements():
            entry = entries.GetNext(components.interfaces.nsIFile)
            if os.path.splitext(entry.path)[1]==".py":
                try:
                    self.autoRegisterComponent(when, entry)
                # Handle some common user errors
                except xpcom.COMException, details:
                    from xpcom import nsError
                    # If the interface name does not exist, suppress the traceback
                    if details.errno==nsError.NS_ERROR_NO_INTERFACE:
                        print "** Registration of '%s' failed\n %s" % (entry.leafName,details.message,)
                    else:
                        print "** Registration of '%s' failed!" % (entry.leafName,)
                        traceback.print_exc()
                except SyntaxError, details:
                    # Syntax error in source file - no useful traceback here either.
                    print "** Registration of '%s' failed!" % (entry.leafName,)
                    traceback.print_exception(SyntaxError, details, None)
                except:
                    # All other exceptions get the full traceback.
                    print "** Registration of '%s' failed!" % (entry.leafName,)
                    traceback.print_exc()

    def autoRegisterComponent (self, when, componentFile):
        # bool return
        
        # Check if we actually need to do anything
        modtime = componentFile.lastModifiedTime
        loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
        if not loader_mgr.hasFileChanged(componentFile, None, modtime):
            return 1

        if self.num_modules_this_register == 0:
            # New components may have just installed new Python
            # modules into the main python directory (including new .pth files)
            # So we ask Python to re-process our site directory.
            # Note that the pyloader does the equivalent when loading.
            try:
                from xpcom import _xpcom
                import site
                NS_XPCOM_CURRENT_PROCESS_DIR="XCurProcD"
                dirname = _xpcom.GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR)
                dirname.append("python")
                site.addsitedir(dirname.path)
            except:
                print "PyXPCOM loader failed to process site directory before component registration"
                traceback.print_exc()

        self.num_modules_this_register += 1

        # auto-register via the module.
        m = self._getCOMModuleForLocation(componentFile)
        m.registerSelf(components.manager, componentFile, None, self._reg_component_type_)
        loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
        loader_mgr.saveFileInfo(componentFile, None, modtime)
        return 1

    def autoUnregisterComponent (self, when, componentFile):
        # bool return
        # auto-unregister via the module.
        m = self._getCOMModuleForLocation(componentFile)
        loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
        try:
            m.unregisterSelf(components.manager, componentFile)
        finally:
            loader_mgr.removeFileInfo(componentFile, None)
        return 1

    def registerDeferredComponents (self, when):
        # bool return
        if xpcom.verbose:
            print "Python component loader - registerDeferred() called"
        return 0 # no more to register
    def unloadAll (self, when):
        if xpcom.verbose:
            print "Python component loader being asked to unload all components!"
        self.comp_mgr = None
        self.com_modules = {}

def MakePythonComponentLoaderModule(serviceManager, nsIFile):
    import module
    return module.Module( [PythonComponentLoader] )