Package: botoinator
BotoinatorA python module that allows for declaring decorators to be added to boto3 methodsOverviewBotoinator allows you to apply decorators to boto3 methods on either a class or object level. It works through boto3 sessions to allow you to apply decorators to either all clients/resources of a particular session, or to specific clients/resources of boto3.DEFAULT_SESSION.Generated documentationYou can see the pydoc generated documentationHEREUsageDecorate a method belonging to a client object to a single sessionsession=boto3.session.Session()session.register_client_decorator(service_name,method_names,decorator)Arguments:service_name -- the boto3 name (as a string) of the client to apply the decorator to.method_names -- one or more method names of the client to apply the decorator to. Single names can be a string, while multiple names can be a list/tuple/set.decorator -- the decorator function. Must be a function that takes a function and returns a function. The returned function must take (*args, **kwargs) as arguments.Decorate a method belonging to a resource object in a single sessionsession=boto3.session.Session()session.register_resource_decorator(service_name,resource_name,method_names,decorator)Arguments:service_name -- the boto3 name (as a string) of the service to apply the decorator to.resource_name -- the boto3 name of the resource of the service to apply the decorator to.method_names -- one or more method names of the resource to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/set.decorator -- the decorator function. Must be a function that takes a function and returns a function. The returned function must take (*args, **kwargs) as argumentsDecorate a method for clients created in any sessionboto3.session.Session.add_client_decorator(service_name,method_names,decorator)Arguments:service_name -- the boto3 name (as a string) of the client to apply the decorator to.method_names -- one or more method names of the client to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/set.decorator -- the decorator function. Must be a function that takes a function and returns a function. The returned function must take (*args, **kwargs) as arguments.Decorate a method of a resource in all sessionsboto3.session.Session.add_resource_decorator(service_name,resource_name,method_names,decorator)Arguments:service_name -- the boto3 name of the service to apply the decorator to.resource_name -- the boto3 name of the resource of the service to apply the decorator to.method_names -- one or more method names of the resource to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/setdecorator -- the decorator function. Must be a function that takes a function and returns a function. The returned function must take (*args, **kwargs) as argumentsUnregister a decorator so that future clients will not have their methods decorated. Clients that have already registered decorators to methods will retain their decoration.session=boto3.session.Session()session.unregister_client_decorator(service_name,method_names)Arguments:service_name -- the boto3 name of the service to apply the decorator to.method_names -- one or more method names of the client to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/setUnregister a decorator so that future resources will not have their methods decorated. Resources that have already registered decorators to methods will retain their decoration.session=boto3.session.Session()session.unregister_resource_decorator(service_name,resource_name,method_names)Arguments:service_name -- the boto3 name of the service to apply the decorator to.resource_name -- the boto3 name of the resource of the service to apply the decorator to.method_names -- one or more method names of the resource to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/setUndecorate a method for clients created in any session.boto3.session.Session.remove_client_decorator(service_name,method_names)Arguments:service_name -- the boto3 name (as a string) of the client to apply the decorator to.method_names -- one or more method names of the client to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/set.Undecorate a method of a resource in all sessionsboto3.session.Session.remove_resource_decorator(service_name,resource_name,method_names)Arguments:service_name -- the boto3 name of the service to apply the decorator to.resource_name -- the boto3 name of the resource of the service to apply the decorator to.method_names -- one or more method names of the resource to apply the decorator to. Single names can be a string, while multiple names should be a list/tuple/setboto3 convienence methodsIf you use theboto3.client()orboto3.resource()methods, these create a default session object found atboto3.DEFAULT_SESSION.
Changing the default session's decorators requires using theregister_xxxandunregister_xxxmethods documented here.
For exampleboto3.DEFAULT_SESSION.register_client_decorator(...).Example of decorating create_bucket() on a single boto3 sessionimportboto3importbotoinatorfrommotoimportmock_s3,mock_sqs""" This is our decorator that we will apply to boto3 methods """defmyDecorator(func):deftest_decorator(*args,**kwargs):setattr(test_decorator,'testValue',True)# Add this attribute to the returned function for testingreturnfunc(*args,**kwargs)returntest_decorator@mock_s3deftestRegisterToClient():"""Test registering a decorator to a single boto3 session"""# Create a boto3 sessions=boto3.session.Session()# Register the create_bucket() method to use our decorator for this sessions.register_client_decorator('s3','create_bucket',myDecorator)# Now create our client as we normally wouldclient1=s.client('s3')# Now we can see that create_bucket() was decorated by testing the attribute we addedclient1.create_bucket(Bucket='foo')asserthasattr(client1.create_bucket,'testValue')# We can also see that this only applies to calls made by the session we registered by creating a new session through boto3.client() and not registering a decoratorclient2=boto3.client('s3')client2.create_bucket(Bucket='foo')# Now we can see that client.create_bucket() is not decoratedassertnothasattr(client2.create_bucket,'testValue')# Remove the decorator from the sessions.unregister_client_decorator('s3','create_bucket')# Now create a new client on the same session we created at firstclient3=s.client('s3')client3.create_bucket(Bucket='bar')# The session should no longer be decorating methods for new clientsassertnothasattr(client3.create_bucket,'testValue1')Viewmore examples in the project documentation directory.