def testGetMandatoryConstructorArgsFromConstructorWithMixedWithArgsAndKwargs(): class KwargzAndMore(object): def __init__(self, crosseyed, heart, *more, **kwargs): pass kwam = KwargzAndMore(False, True, "some", "more", things="here") with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(kwam.__class__) assert ["crosseyed", "heart"] == args
def testGetMandatoryConstructorArgsFromConstructorWithWildcardArguments(): class WildcardOnly(object): def __init__(self, *only): pass wo = WildcardOnly("yeah", "great", "thing") with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(wo.__class__) assert [] == args
def testGetMandatoryConstructorArgsFromConstructorWithKeywordArguments(): class Kwargz(object): def __init__(self, **kwargs): pass kw = Kwargz(go=1, get="asdf", them=[], girl=True) with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(kw.__class__) assert [] == args
def testGetMandatoryConstructorArgsFromConstructorWithMixedArguments(): class MixedArgs(object): def __init__(self, i, want, this, but=0, that=0, notso=0, much=0): pass ma = MixedArgs(True, True, True) with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(ma.__class__) assert ['i', 'want', 'this'] == args
def testGetMandatoryConstructorArgsFromConstructorWithOnlyOptionalArguments(): class OnlyOptional(object): def __init__(self, only=1, optional=2, arguments=[]): pass oo = OnlyOptional() with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(oo.__class__) assert [] == args
def testGetMandatoryConstructorArgsFromConstructorWithOnlyMandatoryArguments(): class OnlyMandatory(object): def __init__(self, give, me, this): pass om = OnlyMandatory(1, 1, 1) with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(om.__class__) assert ['give', 'me', 'this'] == args
def testGetMandatoryConstructorArgsFromConstructorWithNoArguments(): class NoArgs(object): def __init__(self): pass n = NoArgs() with cleanMandatoryConstructorArgsCache(): args = mandatoryConstructorArgs(n.__class__) assert [] == args
def _filterObjects(self, objects, acls, exceptionOnTruncate=True, exceptionIfAllRemoved=True): logger.info(u"Filtering objects by acls") newObjects = [] for obj in forceList(objects): isDict = isinstance(obj, dict) if isDict: objHash = obj else: objHash = obj.toHash() allowedAttributes = set() for acl in acls: if acl.get('type') == 'self': objectId = None for identifier in ('id', 'objectId', 'hostId', 'clientId', 'depotId', 'serverId'): try: objectId = objHash[identifier] break except KeyError: pass if not objectId or objectId != self._username: continue if acl.get('allowAttributes'): attributesToAdd = acl['allowAttributes'] elif acl.get('denyAttributes'): attributesToAdd = ( attribute for attribute in objHash if attribute not in acl['denyAttributes']) else: attributesToAdd = objHash.keys() for attribute in attributesToAdd: allowedAttributes.add(attribute) if not allowedAttributes: continue if not isDict: allowedAttributes.add('type') for attribute in mandatoryConstructorArgs(obj.__class__): allowedAttributes.add(attribute) for key in objHash.keys(): if key not in allowedAttributes: if exceptionOnTruncate: raise BackendPermissionDeniedError( u"Access to attribute '%s' denied" % key) del objHash[key] if isDict: newObjects.append(objHash) else: newObjects.append(obj.__class__.fromHash(objHash)) orilen = len(objects) newlen = len(newObjects) if newlen < orilen: logger.warning( u"{0} objects removed by acl, {1} objects left".format( (orilen - newlen), newlen)) if newlen == 0 and exceptionIfAllRemoved: raise BackendPermissionDeniedError(u"Access denied") return newObjects