<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def discover():
 """ Discover handlers based on convention. Handlers will be loaded from the following directories and their subdirectories: * ``$CHARM_DIR/reactive/`` * ``$CHARM_DIR/hooks/reactive/`` * ``$CHARM_DIR/hooks/relations/`` They can be Python files, in which case they will be imported and decorated functions registered. Or they can be executables, in which case they must adhere to the :class:`ExternalHandler` protocol. """

    # Add $CHARM_DIR and $CHARM_DIR/hooks to sys.path so
    # 'import reactive.leadership', 'import relations.pgsql' works
    # as expected, as well as relative imports like 'import ..leadership'
    # or 'from . import leadership'. Without this, it becomes difficult
    # for layers to access APIs provided by other layers. This addition
    # needs to remain in effect, in case discovered modules are doing
    # late imports.
    _append_path(hookenv.charm_dir())
    _append_path(os.path.join(hookenv.charm_dir(), 'hooks'))

    for search_dir in ('reactive', 'hooks/reactive', 'hooks/relations'):
        search_path = os.path.join(hookenv.charm_dir(), search_dir)
        for dirpath, dirnames, filenames in os.walk(search_path):
            for filename in filenames:
                filepath = os.path.join(dirpath, filename)
                _register_handlers_from_file(search_path, filepath)