<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def OnSave(self, event):
 """File save event handler"""


        try:
            filetype = event.attr["filetype"]

        except (KeyError, AttributeError):
            filetype = None

        filepath = self.main_window.filepath
        if filepath is None:
            filetype = config["default_save_filetype"]

        if filetype is None:

            f2w = get_filetypes2wildcards(["pys", "pysu", "xls", "all"])
            __filetypes = f2w.keys()

            # Check if the file extension matches any valid save filetype
            for __filetype in __filetypes:
                if splitext(filepath)[-1][1:] == __filetype:
                    filetype = __filetype
                    break

        # If there is no filepath or no filetype is found then jump to save as

        if self.main_window.filepath is None or filetype is None:
            post_command_event(self.main_window,
                               self.main_window.SaveAsMsg)
            return

        # Save the grid

        post_command_event(self.main_window,
                           self.main_window.GridActionSaveMsg,
                           attr={"filepath": self.main_window.filepath,
                                 "filetype": filetype})

        # Update undo stack savepoint
        undo.stack().savepoint()

        # Display file save in status bar

        statustext = self.main_window.filepath.split("/")[-1] + " saved."
        post_command_event(self.main_window,
                           self.main_window.StatusBarMsg,
                           text=statustext)