Author: jmorliaguet
Date: Thu Mar 23 22:22:24 2006
New Revision: 2705

Modified:
   cpsskins/branches/jmo-perspectives/setup/README.txt
   cpsskins/branches/jmo-perspectives/setup/interfaces.py
   cpsskins/branches/jmo-perspectives/setup/manager.py
   cpsskins/branches/jmo-perspectives/setup/setting.py
Log:

- API / test updates



Modified: cpsskins/branches/jmo-perspectives/setup/README.txt
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/README.txt (original)
+++ cpsskins/branches/jmo-perspectives/setup/README.txt Thu Mar 23 22:22:24 2006
@@ -3,21 +3,34 @@
 APPLICATION SETUP
 =================
 
-The application is set up by registering resources. Resources can be of
-different types:
+The application is set up by registering _resources_. Any application object 
can
+be registered as a resource.
 
-- global resources (stored in RAM)
+When registering a resource a _setting_ is created that "contains" the 
resource.
+A setting encapsulates the resource and is used to simplify the registration
+process.
 
-- local resources (stored in the ZODB)
+Resources can be of different types, depending on the type of setting used:
 
-global resources can be customized into local resources and they can
-be decustomized.
+- _global resources_ are stored on the filesystem, they are loaded into RAM and
+  they cannot be modified or removed. To a global resource corresponds a
+  _global setting_ (implemented using global utilities).
 
-resources can be loaded from the filesystem.
+- _local resources_ are stored in the site itself (e.g. ZODB). Local resources
+  are registered as _local settings_ (using local utilities).
+
+Resources have a unique _name_ which is used to identify them.
+
+Global resources can be customized into local resources in order to apply local
+modifications to them.
+
+Customized resources can also be decustomized, i.e the customization be 
removed.
 
 
 Test setup:
 
+    >>> from pprint import pprint
+
     >>> from zope.app.testing import ztapi
     >>> from zope.component import getUtility
     >>> from zope.interface import implements, Interface
@@ -30,7 +43,6 @@
     >>> tmutil = addThemeManager(root)
 
     >>> ztapi.provideUtility(IResourceManager, ResourceManager())
-    >>> resources = getUtility(IResourceManager)
 
     >>> class IDummyResource(Interface):
     ...     """Dummy resource interface"""
@@ -44,47 +56,111 @@
     ...     def __repr__(self):
     ...         return '<Dummy resource: %s>' % self.id
 
+We create a couple of resources:
 
     >>> resource1 = DummyResource(u'resource1')
     >>> resource2 = DummyResource(u'resource2')
+    >>> resource3 = DummyResource(u'resource3')
     >>> root[u'resource1'] = resource1
     >>> root[u'resource2'] = resource2
+    >>> root[u'resource3'] = resource3
+
+Resources are available via the resource manager:
 
-none of the resources are available yet:
+    >>> resources = getUtility(IResourceManager)
 
-    >>> resources.lookup('setting1')
+But none of the above resources are available yet in the application:
 
-    >>> resources.lookup('setting2')
+    >>> resources.lookup(u'setting1') is None
+    True
 
-register a resource:
+    >>> resources.lookup(u'setting2') is None
+    True
+
+Registering resources
+---------------------
+
+Resources need to be registered, so we register them:
 
     >>> resources.register(name=u'setting1', title=u'Setting 1',
     ...                    resource=resource1)
 
-lookup resources:
+and we look up the registered resource:
 
-    >>> resources.lookup('setting1')
+    >>> resources.lookup(u'setting1')
     <Dummy resource: resource1>
 
     >>> resources.lookup('setting2')
 
-list resources:
+We can get the complete list of registered resources:
 
     >>> resources.list(type=IDummyResource)
     [<Dummy resource: resource1>]
 
-register a new resource:
+Now we register the second resource:
 
     >>> resources.register(name=u'setting2', title=u'Setting 2',
     ...                    resource=resource2)
 
-lookup resources:
+and look it up in the registry:
 
-    >>> resources.lookup('setting2')
+    >>> resources.lookup(u'setting2')
     <Dummy resource: resource2>
 
-list resources:
+or we find it by listing all registered resources:
 
     >>> resources.list(type=IDummyResource)
     [<Dummy resource: resource1>, <Dummy resource: resource2>]
 
+
+Trying to unregister a global resource raises an error:
+
+    >>> resources.isGlobal(u'setting1')
+    True
+
+    >>> resources.unregister(name=u'setting1', context=root)
+    Traceback (most recent call last):
+    ...
+    ValueError: No such local setting: 'setting1'
+
+so we register a local resource, we do this by passing a context to register():
+
+    >>> resources.register(name=u'setting3', title=u'Setting 3',
+    ...                    resource=resource3, context=root)
+
+We now have a local resource in the context of the root folder:
+
+    >>> resources.isLocal(name=u'setting3', context=root)
+    True
+
+but for the application and in the context of the root folder there is no
+difference between local and global resources:
+
+    >>> pprint(resources.list(type=IDummyResource, context=root))
+    [<Dummy resource: resource1>, <Dummy resource: resource2>, <Dummy
+    resource: resource3>]
+
+
+Unregistering resources
+-----------------------
+
+when unregistering a resource, a context must be given:
+
+    >>> resources.unregister(name='setting3')
+    Traceback (most recent call last):
+    ...
+    ValueError: Must specify a context.
+
+and a resource name must be specified:
+
+    >>> resources.unregister(context=root)
+    Traceback (most recent call last):
+    ...
+    KeyError: 'Must specify a resource name.'
+
+so we pass both:
+
+    >>> resources.unregister(name='setting3', context=root)
+
+
+global settings cannot be modified but they can be customized:

Modified: cpsskins/branches/jmo-perspectives/setup/interfaces.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/interfaces.py      (original)
+++ cpsskins/branches/jmo-perspectives/setup/interfaces.py      Thu Mar 23 
22:22:24 2006
@@ -39,6 +39,7 @@
 
     resource = Attribute("The resource.")
 
+
 class IGlobalSetting(ISetting):
     """ """
 
@@ -81,3 +82,8 @@
     def customize(name, context):
         """Customize a resource."""
 
+    def isGlobal(name):
+        """Returns true if the setting is global"""
+
+    def isLocal(name):
+        """Returns true if the setting is local"""

Modified: cpsskins/branches/jmo-perspectives/setup/manager.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/manager.py (original)
+++ cpsskins/branches/jmo-perspectives/setup/manager.py Thu Mar 23 22:22:24 2006
@@ -29,7 +29,7 @@
                             provideUtility, providedBy)
 
 from cpsskins.setup.interfaces import IResourceManager, IResource, ISettings
-from cpsskins.setup.interfaces import ISetting
+from cpsskins.setup.interfaces import ISetting, IGlobalSetting, ILocalSetting
 from cpsskins.setup.setting import GlobalSetting, LocalSetting
 
 class ResourceManager(object):
@@ -72,22 +72,18 @@
         if context is None:
             raise ValueError("Must specify a context.")
 
-        setting = queryUtility(ISetting, name, context=self.context)
-        if setting is None:
-            raise ValueError("No such setting: '%s'." % name)
+        setting = queryUtility(ISetting, name, context=context)
+        if setting is None or not setting.isLocal():
+            raise ValueError("No such local setting: '%s'" % name)
 
         settings = getUtility(ISettings, context=context)
         regManager = settings.registrationManager
         for reg in list(regManager.values()):
-            if not reg.provided.isOrExtends(IGlobalSetting):
-                raise ValueError("Attempted to unregister a global setting: "
-                                 "'%s'" % name)
-            if reg.provided.isOrExtends(ILocalSetting) and name == reg.name:
+            if reg.provided.isOrExtends(ISetting) and name == reg.name:
                 reg.status = InactiveStatus
                 del regManager[zapi.name(reg)]
                 del settings[name]
-            else:
-                raise ValueError("No such setting: '%s'." % name)
+                break
 
     def lookup(self, name=u'', context=None, default=None):
         if not name:
@@ -104,7 +100,7 @@
 
     def list(self, type=None, context=None):
         resources = []
-        for name, setting in getUtilitiesFor(ISetting):
+        for name, setting in getUtilitiesFor(ISetting, context=context):
             resource_getter = IResource(setting, None)
             if resource_getter is None:
                 continue
@@ -114,14 +110,23 @@
             resources.append(resource)
         return resources
 
-    def customize(self, name=u'', context=None):
-        if context is None:
-            raise ValueError("Must specify a context.")
+    def isLocal(self, name=u'', context=None):
+        setting = queryUtility(ISetting, name, context=context)
+        if setting is None:
+            raise ValueError("No such setting: '%s'." % name)
+        return ILocalSetting.providedBy(setting)
 
+    def isGlobal(self, name=u''):
         setting = queryUtility(ISetting, name)
         if setting is None:
-            raise ValueError("No such setting.")
+            raise ValueError("No such setting: '%s'." % name)
+        return IGlobalSetting.providedBy(setting)
 
+    def customize(self, name=u'', context=None):
+        if context is None:
+            raise ValueError("Must specify a context.")
+
+        setting = self._getSetting(name)
         resource = None
         resource_getter = IResource(setting)
         if resource_getter is not None:
@@ -142,3 +147,4 @@
 
         self.unregister(name=name, context=context)
 
+

Modified: cpsskins/branches/jmo-perspectives/setup/setting.py
==============================================================================
--- cpsskins/branches/jmo-perspectives/setup/setting.py (original)
+++ cpsskins/branches/jmo-perspectives/setup/setting.py Thu Mar 23 22:22:24 2006
@@ -33,6 +33,12 @@
         self.resource.__parent__ = self
         self.resource.__name__ = name
 
+    def isGlobal(self):
+        return IGlobalSetting.providedBy(self)
+
+    def isLocal(self):
+        return ILocalSetting.providedBy(self)
+
 class GlobalSetting(Setting):
     """A local persistent setting
     """
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to