def retrieve_listing(source,listing_type,startswith):

    # Generate regex that will match on any combo of lower and upper case.
    starts_lu = None
    if startswith:
        starts_lu = ''.join(['[' + x.lower() + x.upper() + ']' for x in startswith])

    if 'dir' in source:

        dw = DirWalker(directory=WALK_DIR, match_regex=starts_lu)

        if listing_type == 1:
            # 1 = short listing
            dw.limit=1
            dw.walkit()
            return jsonify({
                'matched_pattern':starts_lu,
                'root':dw.all_roots[0] if dw.all_roots else None,
                'dirs':dw.all_dirs,
                'files':dw.all_files
                })

        elif listing_type == 2:
            # 2 = long listing
            dw.limit=10
            dw.walkit()
            return jsonify({
                'matched_pattern':starts_lu,
                'roots':dw.all_roots,
                'dirs':dw.all_dirs,
                'files':dw.all_files
                })
        else:
            abort(404)

    elif 'tar' in source:
        t = tarfile.open(TAR_FILE, 'r')

        # Only one listing type for tar files.
        listing = t.getnames()

        if starts_lu:
            listing = [x for x in listing if re.match(starts_lu,x)]

        return jsonify({'files': listing, 'tarfile': TAR_FILE})

    else: 
        raise Exception("Unknown option: %s" % source)
def individual_files(dirname,file_startswith_regex):

    dirname = '/' + dirname
    if request.method == 'GET':
        # Instead of constructing regex, yours is passed stright through to the search.
        # Don't run this on a publicly accessible server, this is a giant security hole. 
        dw = DirWalker(directory=dirname,match_regex=file_startswith_regex)
        dw.walkit()
        return jsonify({
            'matched_pattern':file_startswith_regex,
            'walked directory':dirname,
            'root':dw.all_roots[0] if dw.all_roots else None,
            'dirs':dw.all_dirs,
            'files':dw.all_files
            })

    elif request.method == 'POST':
        return jsonify({
           'results':'Do some update here, maybe?'
            })

    elif request.method == 'DELETE':
        pass
        '''