<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_jobs(when=None, only_scheduled=False):
 """ Return a dictionary mapping of job names together with their respective application class. """

    # FIXME: HACK: make sure the project dir is on the path when executed as ./manage.py
    try:
        cpath = os.path.dirname(os.path.realpath(sys.argv[0]))
        ppath = os.path.dirname(cpath)
        if ppath not in sys.path:
            sys.path.append(ppath)
    except Exception:
        pass
    _jobs = {}

    for app_name in [app.name for app in apps.get_app_configs()]:
        scandirs = (None, 'minutely', 'quarter_hourly', 'hourly', 'daily', 'weekly', 'monthly', 'yearly')
        if when:
            scandirs = None, when
        for subdir in scandirs:
            try:
                path = find_job_module(app_name, subdir)
                for name in find_jobs(path):
                    if (app_name, name) in _jobs:
                        raise JobError("Duplicate job %s" % name)
                    job = import_job(app_name, name, subdir)
                    if only_scheduled and job.when is None:
                        # only include jobs which are scheduled
                        continue
                    if when and job.when != when:
                        # generic job not in same schedule
                        continue
                    _jobs[(app_name, name)] = job
            except ImportError:
                # No job module -- continue scanning
                pass

    return _jobs