Back to index

lightning-sunbird  0.9+nobinonly
module.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is the Python XPCOM language bindings.
00015 #
00016 # The Initial Developer of the Original Code is ActiveState Tool Corp.
00017 # Portions created by ActiveState Tool Corp. are Copyright (C) 2000, 2001
00018 # ActiveState Tool Corp.  All Rights Reserved.
00019 #
00020 # Contributor(s):
00021 #   Mark Hammond <MarkH@ActiveState.com> (original author)
00022 #
00023 # Alternatively, the contents of this file may be used under the terms of
00024 # either the GNU General Public License Version 2 or later (the "GPL"), or
00025 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026 # in which case the provisions of the GPL or the LGPL are applicable instead
00027 # of those above. If you wish to allow use of your version of this file only
00028 # under the terms of either the GPL or the LGPL, and not to allow others to
00029 # use your version of this file under the terms of the MPL, indicate your
00030 # decision by deleting the provisions above and replace them with the notice
00031 # and other provisions required by the GPL or the LGPL. If you do not delete
00032 # the provisions above, a recipient may use your version of this file under
00033 # the terms of any one of the MPL, the GPL or the LGPL.
00034 #
00035 # ***** END LICENSE BLOCK *****
00036 
00037 from xpcom import components
00038 from xpcom import ServerException, Exception
00039 from xpcom import nsError
00040 
00041 import factory
00042 
00043 import types
00044 import os
00045 
00046 class Module:
00047     _com_interfaces_ = components.interfaces.nsIModule
00048     def __init__(self, comps):
00049         # Build a map of classes we can provide factories for.
00050         c = self.components = {}
00051         for klass in comps:
00052             c[components.ID(klass._reg_clsid_)] = klass
00053         self.klassFactory = factory.Factory
00054 
00055     def getClassObject(self, compMgr, clsid, iid):
00056         # Single retval result.
00057         try:
00058             klass = self.components[clsid]
00059         except KeyError:
00060             raise ServerException(nsError.NS_ERROR_FACTORY_NOT_REGISTERED)
00061         
00062         # We can ignore the IID - the auto-wrap process will automatically QI us.
00063         return self.klassFactory(klass)
00064 
00065     def registerSelf(self, compMgr, location, loaderStr, componentType):
00066         # void function.
00067         fname = os.path.basename(location.path)
00068         for klass in self.components.values():
00069             reg_contractid = klass._reg_contractid_
00070             print "Registering '%s' (%s)" % (reg_contractid, fname)
00071             reg_desc = getattr(klass, "_reg_desc_", reg_contractid)
00072             compMgr = compMgr.queryInterface(components.interfaces.nsIComponentRegistrar)
00073             compMgr.registerFactoryLocation(klass._reg_clsid_,
00074                                               reg_desc,
00075                                               reg_contractid,
00076                                               location,
00077                                               loaderStr,
00078                                               componentType)
00079 
00080             # See if this class nominates custom register_self
00081             extra_func = getattr(klass, "_reg_registrar_", (None,None))[0]
00082             if extra_func is not None:
00083                 extra_func(klass, compMgr, location, loaderStr, componentType)
00084 
00085     def unregisterSelf(self, compMgr, location, loaderStr):
00086         # void function.
00087         for klass in self.components.values():
00088             ok = 1
00089             try:
00090                 compMgr.unregisterComponentSpec(klass._reg_clsid_, location)
00091             except Exception:
00092                 ok = 0
00093             # Give the class a bash even if we failed!
00094             extra_func = getattr(klass, "_reg_registrar_", (None,None))[1]
00095             if extra_func is not None:
00096                 try:
00097                     extra_func(klass, compMgr, location, loaderStr)
00098                 except Exception:
00099                     ok = 0
00100             if ok:
00101                 print "Successfully unregistered", klass.__name__
00102             else:
00103                 print "Unregistration of", klass.__name__, "failed. (probably just not already registered)"
00104         
00105     def canUnload(self, compMgr):
00106         # single bool result
00107         return 0 # we can never unload!
00108