def MultiChecker(specs): """Create a checker from a sequence of specifications A specification is: - A two-tuple with: o a sequence of names or an interface o a permission id All the names in the sequence of names or the interface are protected by the permission. - A dictionoid (having an items method), with items that are name/permission-id pairs. """ data = {} for spec in specs: if type(spec) is tuple: names, permission_id = spec if IInterface.providedBy(names): names = names.names(all=True) for name in names: if data.get(name, permission_id) is not permission_id: raise DuplicationError(name) data[name] = permission_id else: for name, permission_id in spec.items(): if data.get(name, permission_id) is not permission_id: raise DuplicationError(name) data[name] = permission_id return Checker(data)
def _renameItem(self, oldName, newName): object = self.container.get(oldName) if object is None: raise ItemNotFoundError(self.container, oldName) mover = IObjectMover(object) if newName in self.container: raise DuplicationError("%s is already in use" % newName) return mover.moveTo(self.container, newName)
def defineChecker(type_, checker): """Define a checker for a given type of object The checker can be a :class:`Checker`, or a function that, when called with an object, returns a :class:`Checker`. """ if not isinstance(type_, DEFINABLE_TYPES): raise TypeError( 'type_ must be a type, class or module, not a %s' % type_) if type_ in _checkers: raise DuplicationError(type_) _checkers[type_] = checker
def defineChecker(type_, checker): """Define a checker for a given type of object The checker can be a Checker, or a function that, when called with an object, returns a Checker. """ if not isinstance(type_, (type, types.ClassType, types.ModuleType)): raise TypeError('type_ must be a type, class or module, not a %s' % type_) if type_ in _checkers: raise DuplicationError(type_) _checkers[type_] = checker
def NamesChecker(names=(), permission_id=CheckerPublic, **__kw__): """Return a checker that grants access to a set of names. A sequence of names is given as the first argument. If a second argument, permission_id, is given, it is the permission required to access the names. Additional names and permission IDs can be supplied as keyword arguments. """ data = {} data.update(__kw__) for name in names: if data.get(name, permission_id) is not permission_id: raise DuplicationError(name) data[name] = permission_id return Checker(data)
def __setitem__(self, name, object): """Add the given object to the folder under the given name.""" if ECHO: print 'Folder:setitem' if not (isinstance(name, str) or isinstance(name, unicode)): raise TypeError("Name must be a string rather than a %s" % name.__class__.__name__) try: unicode(name) except UnicodeError: raise TypeError("Non-unicode names must be 7-bit-ascii only") if not name: raise TypeError("Name must not be empty") if name in self.data: raise DuplicationError("name, %s, is already in use" % name) # TODO: need to change setitem so that events are fired for the object, # not the object stored form - override the __setitem__ method of self.data setitem(self, self.set_data_item, name, object)