VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/server/loader.py@ 28509

Last change on this file since 28509 was 11746, checked in by vboxsync, 16 years ago

export python lib to OSE

  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1# ***** BEGIN LICENSE BLOCK *****
2# Version: MPL 1.1/GPL 2.0/LGPL 2.1
3#
4# The contents of this file are subject to the Mozilla Public License Version
5# 1.1 (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7# http://www.mozilla.org/MPL/
8#
9# Software distributed under the License is distributed on an "AS IS" basis,
10# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11# for the specific language governing rights and limitations under the
12# License.
13#
14# The Original Code is the Python XPCOM language bindings.
15#
16# The Initial Developer of the Original Code is
17# Activestate Tool Corp.
18# Portions created by the Initial Developer are Copyright (C) 2000
19# the Initial Developer. All Rights Reserved.
20#
21# Contributor(s):
22# Mark Hammond <[email protected]>
23#
24# Alternatively, the contents of this file may be used under the terms of
25# either the GNU General Public License Version 2 or later (the "GPL"), or
26# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27# in which case the provisions of the GPL or the LGPL are applicable instead
28# of those above. If you wish to allow use of your version of this file only
29# under the terms of either the GPL or the LGPL, and not to allow others to
30# use your version of this file under the terms of the MPL, indicate your
31# decision by deleting the provisions above and replace them with the notice
32# and other provisions required by the GPL or the LGPL. If you do not delete
33# the provisions above, a recipient may use your version of this file under
34# the terms of any one of the MPL, the GPL or the LGPL.
35#
36# ***** END LICENSE BLOCK *****
37
38import xpcom
39from xpcom import components, logger
40
41import module
42
43import glob, os, types
44
45from xpcom.client import Component
46
47# Until we get interface constants.
48When_Startup = 0
49When_Component = 1
50When_Timer = 2
51
52def _has_good_attr(object, attr):
53 # Actually allows "None" to be specified to disable inherited attributes.
54 return getattr(object, attr, None) is not None
55
56def FindCOMComponents(py_module):
57 # For now, just run over all classes looking for likely candidates.
58 comps = []
59 for name, object in py_module.__dict__.items():
60 if type(object)==types.ClassType and \
61 _has_good_attr(object, "_com_interfaces_") and \
62 _has_good_attr(object, "_reg_clsid_") and \
63 _has_good_attr(object, "_reg_contractid_"):
64 comps.append(object)
65 return comps
66
67def register_self(klass, compMgr, location, registryLocation, componentType):
68 pcl = PythonComponentLoader
69 from xpcom import _xpcom
70 svc = _xpcom.GetServiceManager().getServiceByContractID("@mozilla.org/categorymanager;1", components.interfaces.nsICategoryManager)
71 svc.addCategoryEntry("component-loader", pcl._reg_component_type_, pcl._reg_contractid_, 1, 1)
72
73class PythonComponentLoader:
74 _com_interfaces_ = components.interfaces.nsIComponentLoader
75 _reg_clsid_ = "{63B68B1E-3E62-45f0-98E3-5E0B5797970C}" # Never copy these!
76 _reg_contractid_ = "moz.pyloader.1"
77 _reg_desc_ = "Python component loader"
78 # Optional function which performs additional special registration
79 # Appears that no special unregistration is needed for ComponentLoaders, hence no unregister function.
80 _reg_registrar_ = (register_self,None)
81 # Custom attributes for ComponentLoader registration.
82 _reg_component_type_ = "script/python"
83
84 def __init__(self):
85 self.com_modules = {} # Keyed by module's FQN as obtained from nsIFile.path
86 self.moduleFactory = module.Module
87 self.num_modules_this_register = 0
88
89 def _getCOMModuleForLocation(self, componentFile):
90 fqn = componentFile.path
91 mod = self.com_modules.get(fqn)
92 if mod is not None:
93 return mod
94 import ihooks, sys
95 base_name = os.path.splitext(os.path.basename(fqn))[0]
96 loader = ihooks.ModuleLoader()
97
98 module_name_in_sys = "component:%s" % (base_name,)
99 stuff = loader.find_module(base_name, [componentFile.parent.path])
100 assert stuff is not None, "Couldnt find the module '%s'" % (base_name,)
101 py_mod = loader.load_module( module_name_in_sys, stuff )
102
103 # Make and remember the COM module.
104 comps = FindCOMComponents(py_mod)
105 mod = self.moduleFactory(comps)
106
107 self.com_modules[fqn] = mod
108 return mod
109
110 def getFactory(self, clsid, location, type):
111 # return the factory
112 assert type == self._reg_component_type_, "Being asked to create an object not of my type:%s" % (type,)
113 # FIXME: how to do this without obsolete component manager?
114 cmo = components.manager.queryInterface(components.interfaces.nsIComponentManagerObsolete)
115 file_interface = cmo.specForRegistryLocation(location)
116 # delegate to the module.
117 m = self._getCOMModuleForLocation(file_interface)
118 return m.getClassObject(components.manager, clsid, components.interfaces.nsIFactory)
119
120 def init(self, comp_mgr, registry):
121 # void
122 self.comp_mgr = comp_mgr
123 logger.debug("Python component loader init() called")
124
125 # Called when a component of the appropriate type is registered,
126 # to give the component loader an opportunity to do things like
127 # annotate the registry and such.
128 def onRegister (self, clsid, type, className, proId, location, replace, persist):
129 logger.debug("Python component loader - onRegister() called")
130
131 def autoRegisterComponents (self, when, directory):
132 directory_path = directory.path
133 self.num_modules_this_register = 0
134 logger.debug("Auto-registering all Python components in '%s'", directory_path)
135
136 # ToDo - work out the right thing here
137 # eg - do we recurse?
138 # - do we support packages?
139 entries = directory.directoryEntries
140 while entries.HasMoreElements():
141 entry = entries.GetNext(components.interfaces.nsIFile)
142 if os.path.splitext(entry.path)[1]==".py":
143 try:
144 self.autoRegisterComponent(when, entry)
145 # Handle some common user errors
146 except xpcom.COMException, details:
147 from xpcom import nsError
148 # If the interface name does not exist, suppress the traceback
149 if details.errno==nsError.NS_ERROR_NO_INTERFACE:
150 logger.error("Registration of '%s' failed\n %s",
151 entry.leafName, details.message)
152 else:
153 logger.exception("Registration of '%s' failed!", entry.leafName)
154 except SyntaxError, details:
155 # Syntax error in source file - no useful traceback here either.
156 logger.error("Registration of '%s' failed\n %s",
157 entry.leafName, details)
158 except:
159 # All other exceptions get the full traceback.
160 logger.exception("Registration of '%s' failed.", entry.leafName)
161
162 def autoRegisterComponent (self, when, componentFile):
163 # bool return
164
165 # Check if we actually need to do anything
166 modtime = componentFile.lastModifiedTime
167 loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
168 if not loader_mgr.hasFileChanged(componentFile, None, modtime):
169 return 1
170
171 if self.num_modules_this_register == 0:
172 # New components may have just installed new Python
173 # modules into the main python directory (including new .pth files)
174 # So we ask Python to re-process our site directory.
175 # Note that the pyloader does the equivalent when loading.
176 try:
177 from xpcom import _xpcom
178 import site
179 NS_XPCOM_CURRENT_PROCESS_DIR="XCurProcD"
180 dirname = _xpcom.GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR)
181 dirname.append("python")
182 site.addsitedir(dirname.path)
183 except:
184 logger.exception("PyXPCOM loader failed to process site directory before component registration")
185
186 self.num_modules_this_register += 1
187
188 # auto-register via the module.
189 m = self._getCOMModuleForLocation(componentFile)
190 m.registerSelf(components.manager, componentFile, None, self._reg_component_type_)
191 loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
192 loader_mgr.saveFileInfo(componentFile, None, modtime)
193 return 1
194
195 def autoUnregisterComponent (self, when, componentFile):
196 # bool return
197 # auto-unregister via the module.
198 m = self._getCOMModuleForLocation(componentFile)
199 loader_mgr = components.manager.queryInterface(components.interfaces.nsIComponentLoaderManager)
200 try:
201 m.unregisterSelf(components.manager, componentFile)
202 finally:
203 loader_mgr.removeFileInfo(componentFile, None)
204 return 1
205
206 def registerDeferredComponents (self, when):
207 # bool return
208 logger.debug("Python component loader - registerDeferred() called")
209 return 0 # no more to register
210
211 def unloadAll (self, when):
212 # This is called at shutdown time - don't get too upset if an error
213 # results from logging due to the logfile being closed
214 try:
215 logger.debug("Python component loader being asked to unload all components!")
216 except:
217 # Evil blank except, but restricting to just catching IOError
218 # failure means custom logs could still screw us
219 pass
220 self.comp_mgr = None
221 self.com_modules = {}
222
223def MakePythonComponentLoaderModule(serviceManager, nsIFile):
224 import module
225 return module.Module( [PythonComponentLoader] )
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette