def test_clear_all_caches(self): class ISchema1(Interface): pass # FTI mock fti_mock1 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock1.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock1, IDexterityFTI, name=u"testtype1") fti_mock2 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock2.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock2, IDexterityFTI, name=u"testtype2") self.replay() # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_clear_all_caches(self): class ISchema1(Interface): pass # FTI mock fti_mock1 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock1.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock1, IDexterityFTI, name=u"testtype1") fti_mock2 = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock2.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock2, IDexterityFTI, name=u"testtype2") self.replay() # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_clear_all_caches(self): class ISchema1(Interface): pass fti1 = DexterityFTI(u"testtype") fti1.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti1, IDexterityFTI, name=u"testtype1") fti2 = DexterityFTI(u"testtype") fti2.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti2, IDexterityFTI, name=u"testtype2") # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_attribute_and_value_error_not_cached(self): class ISchema1(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).throw(AttributeError) self.expect(fti_mock.lookupSchema()).throw(ValueError) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema2 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is None) self.assertTrue(schema3 is ISchema1)
def test_clear_all_caches(self): class ISchema1(Interface): pass fti1 = DexterityFTI(u"testtype") fti1.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti1, IDexterityFTI, name=u"testtype1") fti2 = DexterityFTI(u"testtype") fti2.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti2, IDexterityFTI, name=u"testtype2") # reset schemacache counter SCHEMA_CACHE.invalidations = 0 # fill cache should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1) # clear SCHEMA_CACHE.clear() self.assertEqual(SCHEMA_CACHE.invalidations, 2) # check invalidations # fill cache again should call lookupschema one time schema1 = SCHEMA_CACHE.get(u"testtype1") schema2 = SCHEMA_CACHE.get(u"testtype2") self.assertTrue(schema1 is schema2 is ISchema1)
def test_invalidate_cache(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) SCHEMA_CACHE.get(portal_type) SCHEMA_CACHE.behavior_schema_interfaces(fti) self.assertIn("_v_schema_behavior_schema_interfaces", fti.__dict__.keys()) invalidate_cache(fti) self.assertNotIn("_v_schema_behavior_schema_interfaces", fti.__dict__.keys())
def test_invalidate_cache(self): portal_type = u"testtype" fti = DexterityFTI(portal_type) SCHEMA_CACHE.get(portal_type) SCHEMA_CACHE.behavior_schema_interfaces(fti) self.assertIn('_v_schema_behavior_schema_interfaces', fti.__dict__.keys()) invalidate_cache(fti) self.assertNotIn('_v_schema_behavior_schema_interfaces', fti.__dict__.keys())
def test_repeated_get_lookup(self): class ISchema(Interface): pass fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 is ISchema)
def test_repeated_get_lookup(self): class ISchema(Interface): pass fti_mock = DexterityFTI(u"testtype") fti_mock.lookupSchema = Mock(return_value=ISchema) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 is ISchema)
def test_unknown_type_not_cached(self): class ISchema1(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"othertype") schema2 = SCHEMA_CACHE.get(u"testtype") schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def test_repeated_get_lookup(self): class ISchema(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 is ISchema)
def test_repeated_lookup(self): class ISchema(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.failUnless(schema1 is schema2 is ISchema)
def test_repeated_lookup_with_changed_schema(self): class ISchema1(Interface): pass class ISchema2(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(side_effect=[ISchema1, ISchema2]) self.mock_utility(fti, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 and schema2 is ISchema1)
def test_unknown_type_not_cached(self): class ISchema1(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(return_value=ISchema1) self.mock_utility(fti, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"othertype") schema2 = SCHEMA_CACHE.get(u"testtype") schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def test_repeated_lookup_with_changed_schema(self): class ISchema1(Interface): pass class ISchema2(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(side_effect=[ISchema1, ISchema2]) self.mock_utility(fti, IDexterityFTI, name=u"testtype") schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 and schema2 is ISchema1)
def omitted_fields(self): """ Gets the omitted fields from the schema. May be specified like this: ISchema.setTaggedValue('seantis.dir.base.omitted', ['field1', 'field2']) """ iface = SCHEMA_CACHE.get(self.form.portal_type) return iface.queryTaggedValue('seantis.dir.base.omitted', [])
def __getattr__(self, name): # python basics: __getattr__ is only invoked if the attribute wasn't # found by __getattribute__ # # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). # also handle special dynamic providedBy cache here. if name.startswith('__') or name == '_v__providedBy__': raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default value = _default_from_schema(self, SCHEMA_CACHE.get(self.portal_type), name) if value is not _marker: return value # do the same for each subtype assignable = IBehaviorAssignable(self, None) if assignable is not None: for behavior_registration in assignable.enumerateBehaviors(): if behavior_registration.interface: value = _default_from_schema( self, behavior_registration.interface, name) if value is not _marker: return value raise AttributeError(name)
def getRichTextFieldNames(self): """ see ISchema interface """ schema = SCHEMA_CACHE.get(self.getportaltype()) return [ name for name, field in schema.namesAndDescriptions() if isinstance(field, RichText) ]
def __getattr__(self, name): # python basics: __getattr__ is only invoked if the attribute wasn't # found by __getattribute__ # # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). # also handle special dynamic providedBy cache here. # Ignore also some other well known names like # Permission, Acquisition and AccessControl related ones. if (name.startswith('__') or name.startswith('_v') or name.endswith('_Permission') or name in ATTRIBUTE_NAMES_TO_IGNORE): raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default value = _default_from_schema(self, SCHEMA_CACHE.get(self.portal_type), name) if value is not _marker: return value # do the same for each behavior assignable = get_assignable(self) if assignable is not None: for behavior_registration in assignable.enumerateBehaviors(): if behavior_registration.interface: value = _default_from_schema( self, behavior_registration.interface, name) if value is not _marker: return value raise AttributeError(name)
def __getattr__(self, name): # python basics: __getattr__ is only invoked if the attribute wasn't # found by __getattribute__ # # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). if name.startswith('__'): raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default value = _default_from_schema( self, SCHEMA_CACHE.get(self.portal_type), name ) if value is not _marker: return value # do the same for each subtype for schema in SCHEMA_CACHE.subtypes(self.portal_type): value = _default_from_schema(self, schema, name) if value is not _marker: return value raise AttributeError(name)
def test_unknown_type_not_cached(self): class ISchema1(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"othertype") schema2 = SCHEMA_CACHE.get(u"testtype") schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def test_unknown_type_not_cached(self): class ISchema1(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"othertype") schema2 = SCHEMA_CACHE.get(u"testtype") schema3 = SCHEMA_CACHE.get(u"testtype") self.failUnless(schema1 is None) self.failUnless(schema2 is schema3 is ISchema1)
def _get_schema(self, inst): portal_type = getattr(inst, 'portal_type', None) if portal_type is not None: try: return SCHEMA_CACHE.get(portal_type) except (ValueError, AttributeError,): pass return None
def _get_schema(self, inst): portal_type = getattr(inst, 'portal_type', None) if portal_type is not None: try: return SCHEMA_CACHE.get(portal_type) except (ValueError, AttributeError,): pass return None
def iterSchemata(context): """Return an iterable containing first the object's schema, and then any form field schemata for any enabled behaviors. """ main_schema = SCHEMA_CACHE.get(context.portal_type) if main_schema: yield main_schema for schema in getAdditionalSchemata(context=context): yield schema
def iterSchemata(context): """Return an iterable containing first the object's schema, and then any form field schemata for any enabled behaviors. """ main_schema = SCHEMA_CACHE.get(context.portal_type) if main_schema: yield main_schema for schema in getAdditionalSchemata(context=context): yield schema
def __get__(self, inst, cls=None): # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) # Find the data we need to know if our cache needs to be invalidated direct_spec = getattr(inst, '__provides__', None) portal_type = getattr(inst, 'portal_type', None) spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # If the instance has no portal type, then we're done. if portal_type is None: return spec fti = queryUtility(IDexterityFTI, name=portal_type) if fti is None: return spec schema = SCHEMA_CACHE.get(portal_type) subtypes = SCHEMA_CACHE.subtypes(portal_type) # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) updated = inst._p_mtime, schema, subtypes, direct_spec # See if we have a valid cache. Reasons to do this include: # # - The schema was modified. # - The subtypes were modified. # - The instance was modified and persisted since the cache was built. # - The instance has a different direct specification. if cache is not None: cached_mtime, cached_schema, cached_subtypes, \ cached_direct_spec, cached_spec = cache if cache[:-1] == updated: return cached_spec dynamically_provided = [] if schema is None else [schema] dynamically_provided.extend(subtypes) # If we have neither a schema, nor a subtype, then we're also done. if not dynamically_provided: return spec dynamically_provided.append(spec) spec = Implements(*dynamically_provided) inst._v__providedBy__ = updated + (spec, ) return spec
def __get__(self, inst, cls=None): # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) # Find the data we need to know if our cache needs to be invalidated direct_spec = getattr(inst, '__provides__', None) portal_type = getattr(inst, 'portal_type', None) spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # If the instance has no portal type, then we're done. if portal_type is None: return spec fti = queryUtility(IDexterityFTI, name=portal_type) if fti is None: return spec schema = SCHEMA_CACHE.get(portal_type) subtypes = SCHEMA_CACHE.subtypes(portal_type) # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) updated = inst._p_mtime, schema, subtypes, direct_spec # See if we have a valid cache. Reasons to do this include: # # - The schema was modified. # - The subtypes were modified. # - The instance was modified and persisted since the cache was built. # - The instance has a different direct specification. if cache is not None: cached_mtime, cached_schema, cached_subtypes, \ cached_direct_spec, cached_spec = cache if cache[:-1] == updated: return cached_spec dynamically_provided = [] if schema is None else [schema] dynamically_provided.extend(subtypes) # If we have neither a schema, nor a subtype, then we're also done. if not dynamically_provided: return spec dynamically_provided.append(spec) spec = Implements(*dynamically_provided) inst._v__providedBy__ = updated + (spec, ) return spec
def test_none_not_cached(self): class ISchema1(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(side_effect=[None, ISchema1, ISchema1]) self.mock_utility(fti, IDexterityFTI, name=u"testtype") SCHEMA_CACHE.invalidate('testtype') schema1 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema2 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def test_repeated_lookup_with_changed_schema(self): class ISchema1(Interface): pass class ISchema2(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.expect(fti_mock.lookupSchema()).result(ISchema2).count(0, None) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 and schema2 is ISchema1)
def iterSchemataForType(portal_type): """XXX: came from plone.app.deco.utils, very similar to iterSchemata Not fully merged codewise with iterSchemata as that breaks test_webdav.test_readline_mimetype_additional_schemata. """ main_schema = SCHEMA_CACHE.get(portal_type) if main_schema: yield main_schema for schema in getAdditionalSchemata(portal_type=portal_type): yield schema
def test_none_not_cached(self): class ISchema1(Interface): pass fti = DexterityFTI(u"testtype") fti.lookupSchema = Mock(side_effect=[None, ISchema1, ISchema1]) self.mock_utility(fti, IDexterityFTI, name=u"testtype") SCHEMA_CACHE.invalidate('testtype') schema1 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema2 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def iterSchemataForType(portal_type): """XXX: came from plone.app.deco.utils, very similar to iterSchemata Not fully merged codewise with iterSchemata as that breaks test_webdav.test_readline_mimetype_additional_schemata. """ main_schema = SCHEMA_CACHE.get(portal_type) if main_schema: yield main_schema for schema in getAdditionalSchemata(portal_type=portal_type): yield schema
def test_repeated_lookup_with_changed_schema(self): class ISchema1(Interface): pass class ISchema2(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.expect(fti_mock.lookupSchema()).result(ISchema2).count(0, None) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is schema2 and schema2 is ISchema1)
def test_attribute_and_value_error_not_cached(self): class ISchema1(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).throw(AttributeError) self.expect(fti_mock.lookupSchema()).throw(ValueError) self.expect(fti_mock.lookupSchema()).result(ISchema1) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() schema1 = SCHEMA_CACHE.get(u"testtype") schema2 = SCHEMA_CACHE.get(u"testtype") schema3 = SCHEMA_CACHE.get(u"testtype") self.failUnless(schema1 is None) self.failUnless(schema2 is None) self.failUnless(schema3 is ISchema1)
def __getattr__(self, name): # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) raise AttributeError(name)
def __getattr__(self, name): # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) raise AttributeError(name)
def __getattr__(self, name): # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) # Be specific about the implementation we use return CMFOrderedBTreeFolderBase.__getattr__(self, name)
def __getattr__(self, name): # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) # Be specific about the implementation we use return CMFOrderedBTreeFolderBase.__getattr__(self, name)
def test_none_not_cached(self): class ISchema1(Interface): pass # FTI mock fti_mock = self.mocker.proxy(DexterityFTI(u"testtype")) self.expect(fti_mock.lookupSchema()).result(None) self.expect(fti_mock.lookupSchema()).result(ISchema1).count(2) self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype") self.replay() SCHEMA_CACHE.invalidate('testtype') schema1 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema2 = SCHEMA_CACHE.get(u"testtype") SCHEMA_CACHE.invalidate('testtype') schema3 = SCHEMA_CACHE.get(u"testtype") self.assertTrue(schema1 is None) self.assertTrue(schema2 is schema3 is ISchema1)
def __getattr__(self, name): # python basics: __getattr__ is only invoked if the attribute wasn't # found by __getattribute__ # # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). # also handle special dynamic providedBy cache here. # Ignore also some other well known names like # Acquisition and AccessControl related ones. if name.startswith('__') or name in ATTRIBUTE_NAMES_TO_IGNORE: raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default value = _default_from_schema( self, SCHEMA_CACHE.get(self.portal_type), name ) if value is not _marker: return value # do the same for each subtype assignable = IBehaviorAssignable(self, None) if assignable is not None: for behavior_registration in assignable.enumerateBehaviors(): if behavior_registration.interface: value = _default_from_schema( self, behavior_registration.interface, name ) if value is not _marker: return value raise AttributeError(name)
def __getattr__(self, name): # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). if name.startswith('__'): raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) # do the same for each subtype for schema in SCHEMA_CACHE.subtypes(self.portal_type): field = schema.get(name, None) if field is not None: return deepcopy(field.default) raise AttributeError(name)
def __getattr__(self, name): # optimization: sometimes we're asked for special attributes # such as __conform__ that we can disregard (because we # wouldn't be in here if the class had such an attribute # defined). if name.startswith('__'): raise AttributeError(name) # attribute was not found; try to look it up in the schema and return # a default schema = SCHEMA_CACHE.get(self.portal_type) if schema is not None: field = schema.get(name, None) if field is not None: return deepcopy(field.default) # do the same for each subtype for schema in SCHEMA_CACHE.subtypes(self.portal_type): field = schema.get(name, None) if field is not None: return deepcopy(field.default) raise AttributeError(name)
def __get__(self, inst, cls=None): # noqa # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) direct_spec = getattr(inst, '__provides__', None) # avoid recursion - fall back on default if getattr(self, '__recursion__', False): return direct_spec spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # Find the data we need to know if our cache needs to be invalidated portal_type = getattr(inst, 'portal_type', None) # If the instance has no portal type, then we're done. if portal_type is None: return spec # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) # See if we have a current cache. Reasons to do this include: # # - The FTI was modified. # - The instance was modified and persisted since the cache was built. # - The instance has a different direct specification. updated = ( inst._p_mtime, SCHEMA_CACHE.modified(portal_type), SCHEMA_CACHE.invalidations, hash(direct_spec) ) if cache is not None and cache[:-1] == updated: if cache[-1] is not None: return cache[-1] return spec main_schema = SCHEMA_CACHE.get(portal_type) if main_schema: dynamically_provided = [main_schema] else: dynamically_provided = [] # block recursion self.__recursion__ = True try: assignable = IBehaviorAssignable(inst, None) if assignable is not None: for behavior_registration in assignable.enumerateBehaviors(): if behavior_registration.marker: dynamically_provided.append( behavior_registration.marker ) finally: del self.__recursion__ if not dynamically_provided: # rare case if no schema nor behaviors with markers are set inst._v__providedBy__ = updated + (None, ) return spec dynamically_provided.append(spec) all_spec = Implements(*dynamically_provided) inst._v__providedBy__ = updated + (all_spec, ) return all_spec
def custom_labels(self): """ See label_widgets. """ iface = SCHEMA_CACHE.get(self.portal_type) return iface.queryTaggedValue('seantis.dir.base.labels', {})
def field_order(self): """ See reorder_widgets. """ iface = SCHEMA_CACHE.get(self.portal_type) return iface.queryTaggedValue('seantis.dir.base.order', ['*'])
def getRichTextFieldNames(self): """ see ISchema interface """ schema = SCHEMA_CACHE.get(self.getportaltype()) return [name for name, field in schema.namesAndDescriptions() if isinstance(field, RichText)]
def custom_labels(self): """ See label_widgets. """ iface = SCHEMA_CACHE.get(self.portal_type) return iface.queryTaggedValue('seantis.dir.base.labels', {})
def __get__(self, inst, cls=None): # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) # Find the data we need to know if our cache needs to be invalidated direct_spec = getattr(inst, '__provides__', None) portal_type = getattr(inst, 'portal_type', None) fti_counter = -1 if portal_type is not None: fti_counter = SCHEMA_CACHE.counter(portal_type) # See if we have a valid cache. Reasons to do this include: # # - We don't have a portal_type yet, so we can't have found the schema # - The FTI was modified, and the schema cache invalidated globally. # The fti_counter will have advanced. # - The instance was modified and persisted since the cache was built. # - The instance now has a different __provides__, which means that someone # called directlyProvides/alsoProvides on it. if cache is not None and portal_type is not None: cached_mtime, cached_fti_counter, cached_direct_spec, cached_spec = cache if inst._p_mtime == cached_mtime and \ fti_counter == cached_fti_counter and \ direct_spec is cached_direct_spec: return cached_spec # We don't have a cache, so we need to build a new spec and maybe cache it spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # Add the schema from the FTI and behavior subtypes dynamically_provided = [] if portal_type is not None: schema = SCHEMA_CACHE.get(portal_type) if schema is not None: dynamically_provided.append(schema) subtypes = SCHEMA_CACHE.subtypes(portal_type) if subtypes: dynamically_provided.extend(subtypes) # If we have any dynamically provided interface, prepend them to the spec # and cache. We can't cache until we have at least the schema, because # it's possible that we were called before traversal and so could not # find the schema yet. if dynamically_provided: dynamically_provided.append(spec) spec = Implements(*dynamically_provided) inst._v__providedBy__ = inst._p_mtime, SCHEMA_CACHE.counter( portal_type), direct_spec, spec return spec
def __init__(self, context): super(DexterityDataProvider, self).__init__(context) schema = SCHEMA_CACHE.get(context.portal_type) self.keys = schema.names()
def __init__(self, context): super(DexterityDataProvider, self).__init__(context) schema = SCHEMA_CACHE.get(context.portal_type) self.keys = schema.names()
def __init__(self, context): super(self.__class__, self).__init__(context) schema = SCHEMA_CACHE.get(context.portal_type) self.keys = schema.names()
def field_order(self): """ See reorder_widgets. """ iface = SCHEMA_CACHE.get(self.portal_type) return iface.queryTaggedValue('seantis.dir.base.order', ['*'])
def __get__(self, inst, cls=None): # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) direct_spec = getattr(inst, '__provides__', None) # avoid recursion - fall back on default if getattr(self, '__recursion__', False): return direct_spec spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # Find the data we need to know if our cache needs to be invalidated portal_type = getattr(inst, 'portal_type', None) # If the instance has no portal type, then we're done. if portal_type is None: return spec # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) # See if we have a current cache. Reasons to do this include: # # - The FTI was modified. # - The instance was modified and persisted since the cache was built. # - The instance has a different direct specification. updated = (inst._p_mtime, SCHEMA_CACHE.modified(portal_type), SCHEMA_CACHE.invalidations, hash(direct_spec)) if cache is not None and cache[:-1] == updated: if cache[-1] is not None: return cache[-1] return spec main_schema = SCHEMA_CACHE.get(portal_type) if main_schema: dynamically_provided = [main_schema] else: dynamically_provided = [] # block recursion self.__recursion__ = True try: assignable = IBehaviorAssignable(inst, None) if assignable is not None: for behavior_registration in assignable.enumerateBehaviors(): if behavior_registration.marker: dynamically_provided.append( behavior_registration.marker) finally: del self.__recursion__ if not dynamically_provided: # rare case if no schema nor behaviors with markers are set inst._v__providedBy__ = updated + (None, ) return spec dynamically_provided.append(spec) all_spec = Implements(*dynamically_provided) inst._v__providedBy__ = updated + (all_spec, ) return all_spec
def __get__(self, inst, cls=None): # We're looking at a class - fall back on default if inst is None: return getObjectSpecification(cls) # Find the cached value. This calculation is expensive and called # hundreds of times during each request, so we require a fast cache cache = getattr(inst, '_v__providedBy__', None) # Find the data we need to know if our cache needs to be invalidated direct_spec = getattr(inst, '__provides__', None) portal_type = getattr(inst, 'portal_type', None) fti_counter = -1 if portal_type is not None: fti_counter = SCHEMA_CACHE.counter(portal_type) # See if we have a valid cache. Reasons to do this include: # # - We don't have a portal_type yet, so we can't have found the schema # - The FTI was modified, and the schema cache invalidated globally. # The fti_counter will have advanced. # - The instance was modified and persisted since the cache was built. # - The instance now has a different __provides__, which means that someone # called directlyProvides/alsoProvides on it. if cache is not None and portal_type is not None: cached_mtime, cached_fti_counter, cached_direct_spec, cached_spec = cache if inst._p_mtime == cached_mtime and \ fti_counter == cached_fti_counter and \ direct_spec is cached_direct_spec: return cached_spec # We don't have a cache, so we need to build a new spec and maybe cache it spec = direct_spec # If the instance doesn't have a __provides__ attribute, get the # interfaces implied by the class as a starting point. if spec is None: spec = implementedBy(cls) # Add the schema from the FTI and behavior subtypes dynamically_provided = [] if portal_type is not None: schema = SCHEMA_CACHE.get(portal_type) if schema is not None: dynamically_provided.append(schema) subtypes = SCHEMA_CACHE.subtypes(portal_type) if subtypes: dynamically_provided.extend(subtypes) # If we have any dynamically provided interface, prepend them to the spec # and cache. We can't cache until we have at least the schema, because # it's possible that we were called before traversal and so could not # find the schema yet. if dynamically_provided: dynamically_provided.append(spec) spec = Implements(*dynamically_provided) inst._v__providedBy__ = inst._p_mtime, SCHEMA_CACHE.counter(portal_type), direct_spec, spec return spec