import functools


def polygon_wrapper(func):
    """
    Wrapper function to perform the setup and teardown of polygon
    attributes before and after creating the polygon.

    Keyword arguments:
        func (function) -- the function to draw the polygon.
    """

    @functools.wraps(func)
    def draw_polygon(self, *args, **kwargs):
        """
        Setup the Context, draw the polygon with attributes applied, and
        teardown the environment.
        """
        # Save the Context so we can restore it when this is done
        self.context.save()

        # Initialize the polygon's attributes
        self._init_attributes(**kwargs)

        # Call the function
        result = func(self, *args, **kwargs)

        # Fill the polygon, if it's being filled
        if self.fill:
            self.context.fill_preserve()

        # Set the outline fill_color and outline the polygon
        self.calling_surface._set_color(self.line_color)
        self.context.stroke()

        # Restore the Context now that the polygon is drawn
        self.context.restore()

        return result

    return draw_polygon