File: test.py

package info (click to toggle)
atheist 0.20110402-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,348 kB
  • sloc: python: 4,764; xml: 626; makefile: 113; cpp: 54; ansic: 5; sh: 5
file content (87 lines) | stat: -rwxr-xr-x 2,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
# -*- coding:utf-8; tab-width:4; mode:python -*-

# test utilities

import os
import io


def path_to_items(path):
    if path == os.sep:
        return []
    return path.strip(os.sep).split(os.sep)


class FakeFileSystem:

    class FakeFile(io.BytesIO):
        def close(self):
            self.seek(0)

    def __init__(self, init=None):
        self.dirs = {'/': []}
        self.files = dict()
        if init is not None:
            self.files.update(init)

    def mkdir(self, path):
        if not path:
            raise OSError

        assert path.startswith(os.sep)

        if self.dirs.has_key(path):
            raise OSError

        path = path.strip(os.sep)

        items = path.split('/')
        for i in range(1, len(items)+1):
            key = os.sep + str.join(os.sep, items[:i])
            if not self.dirs.has_key(key):
                self.dirs[key] = []


    def listdir(self, path):
        try:
            # files
            retval = self.dirs[path][:]

            # directories
            path_items = path_to_items(path)
            lon = len(path_items)

            for dname in self.dirs:
                if path == dname:
                    continue

                items = path_to_items(dname)
                if path_items != items[:lon]:
                    continue

                if not items[lon] in retval:
                    retval.append(items[lon])

            return retval

        except KeyError:
            e = OSError()
            e.strerror = 'No such file or directory'
            e.filename = path
            raise e


    def open(self, fname, mode='r', relative=True):
        if 'r' in mode:
            try:
                return self.files[fname]
            except KeyError:
                e = IOError()
                e.strerror="No such file or directory"
                e.filename=fname
                raise e

        if 'w' in mode:
            self.files[fname] = FakeFileSystem.FakeFile()
            return self.files[fname]