<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def scan(queue, lock=None, ttl=None, max_tries=None, ignore_down=False,
         no_open=False, generator=FSQScanGenerator, host=False, hosts=None):
    '''Given a queue, generate a list of files in that queue, and pass it to
       FSQScanGenerator for iteration.  The generator kwarg is provided here
       as a means of implementing a custom generator, use with caution.'''
    lock = _c.FSQ_LOCK if lock is None else lock
    ttl = _c.FSQ_TTL if lock is None else ttl
    max_tries = _c.FSQ_MAX_TRIES if max_tries is None else max_tries
    item_ids = []
    try:
        if not host and hosts is None:
            item_ids = os.listdir(fsq_path.queue(queue))
            item_ids.sort()
        else:
            if hosts is None:
                hosts = fsq_hosts(queue)
            for trg_host in hosts:
                for item in os.listdir(fsq_path.queue(queue, trg_host)):
                    item_ids.append((trg_host, item))
            item_ids.sort(key=lambda x: x[1])
    except (OSError, IOError, ), e:
        if e.errno == errno.ENOENT:
            raise FSQScanError(e.errno, u'no such queue:'\
                               u' {0}'.format(queue))
        elif isinstance(e, FSQError):
            raise e
        raise FSQScanError(e.errno, wrap_io_os_err(e))

    # sort here should yield time then entropy sorted
    return generator(queue, item_ids, lock=lock, ttl=ttl, max_tries=max_tries,
                     no_open=no_open, host=host)