Example #1
0
def _my_fs(module):
    """Create a mock filesystem to be used in examples."""
    my_fs = MemoryFS()
    if module == "fs.base":
        my_fs.makedir("Desktop")
        my_fs.makedir("Videos")
        my_fs.touch("Videos/starwars.mov")
        my_fs.touch("file.txt")
    elif module == "fs.info":
        my_fs.touch("foo.tar.gz")
        my_fs.settext("foo.py", "print('Hello, world!')")
        my_fs.makedir("bar")
    elif module in {"fs.walk", "fs.glob"}:
        my_fs.makedir("dir1")
        my_fs.makedir("dir2")
        my_fs.settext("foo.py", "print('Hello, world!')")
        my_fs.touch("foo.pyc")
        my_fs.settext("bar.py", "print('ok')\n\n# this is a comment\n")
        my_fs.touch("bar.pyc")
    return my_fs
Example #2
0
def make_temp_fs(fff):
    # make virtual filesystem in ram with the final
    # organization of the filesystem
    ff = cache_directory(read_only(fff))
    ram = MemoryFS()

    # for path, dirs, files in ff.walk():
    posprocsub = []
    fils = set()
    files = ff.scandir('/')
    path = '/'
    folds = set([i.name for i in files if i.is_dir])
    files = ff.scandir('/')
    for j in files:
        if not j.is_file:
            continue
        if splitext(j.name)[1] in subs_formats:
            posprocsub.append(j.name)
            continue
        pp = rename(j.name)
        if pp.error:
            pp = parse(j.name)
        try:
            if pp.is_video:
                fold = transform(pp.title)
                if not (fold in folds):
                    fold = best_ed(fold, folds)
                    folds.add(fold)
                pth = join('/', fold)
                if not ram.exists(pth):
                    ram.makedir(fold)
                fils.add(pp.title)
                if pp.episode:
                    if pp.season:
                        fill = pp.title + ' - ' + \
                            str(pp.season) + 'X' + str(pp.episode)
                    else:
                        fill = pp.title + ' - ' + str(pp.episode)
                else:
                    fill = pp.title
                if pp.episode_title:
                    fill += ' - ' + str(pp.episode_title)
                fill += pp.ext
                ram.settext(join(pth, fill), join(path, j.name))
        except KeyError:
            continue

        for j in posprocsub:
            pp = rename(j)
            if pp.error:
                pp = parse(j.name)
            fold = transform(pp.title)
            pth = join('/', fold)
            if pp.episode:
                if pp.season:
                    fill = pp.title + ' - ' + \
                        str(pp.season) + 'X' + str(pp.episode)
                else:
                    fill = fold + ' - ' + str(pp.episode)
            else:
                fill = fold
            if pp.episode_title:
                fill = fill + ' - ' + str(pp.episode_title)
            fill += pp.ext
            if ram.exists(pth):
                ram.settext(join(pth, fill), join(path, j))
            elif len(fils) == 1:
                pth = join('/', list(fils)[0])
                ram.settext(join(pth, fill), join(path, j))
            elif len(fils) > 1:
                best = None
                gap = 3
                for i in fils:
                    n = editDistance(i, fold)
                    if n < 3 and n < gap:
                        best = i
                        gap = n
                    elif n == 0:
                        best = i
                        break
                if best:
                    pth = join('/', best)
                    ram.settext(join(pth, fill), join(path, j))
                else:
                    if not (ram.exists('/subs')):
                        ram.makedir('/subs')
                    ram.settext(join('/subs', j), join(path, j))
            else:
                if not (ram.exists('/subs')):
                    ram.makedir('/subs')
                ram.settext(join('/subs', j), join(path, j))
    return ram