Welcome, guest | Sign In | My Account | Store | Cart

DAL5 continues to refine the file system's interface and provides methods that should look similar to what most programmers are used to working with. Files have still not been completely abstracted away and all path names must be given as relative to the root. The path separator is defined in this module along with a list of characters for valid file and directory names.

Python, 166 lines
  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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from dal_4 import DAL4

################################################################################

class DAL5:

    # DEFAULTS
    PATH_SEPARATOR = ' '
    NAME_CHARACTERS = ''.join([chr(i) for i in range(256) \
                               if len(repr(chr(i))) == 3])

    # Disk Abstraction Layer
    def __init__(self, blocks, size):
        self.__disk = DAL4(blocks, size)

    # Make New Directory
    def make_directory(self, path):
        block, name = self.__resolve_path(path)
        self.__disk.make_directory(block, name)

    # Remove Old Directory
    def remove_directory(self, path):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        assert block
        assert self.__disk.is_directory(block)
        assert self.__disk.empty(block)
        self.__disk.remove_directory(block)

    # Make New File
    def make_file(self, path):
        block, name = self.__resolve_path(path)
        self.__disk.make_file(block, name)

    # Remove Old File
    def remove_file(self, path):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        assert block
        assert self.__disk.is_file(block)
        self.__disk.remove_file(block)

    # Read From File
    def read_file(self, path):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        assert block
        return self.__disk.read_file(block)

    # Write To File
    def write_file(self, path, data):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        assert block
        self.__disk.write_file(block, data)

    # Get Directory Contents
    def list_directory(self, path):
        if path:
            block, name = self.__resolve_path(path)
            block = self.__disk.find(block, name)
            assert block
        else:
            block = 1
        directory = self.__disk.list_directory(block)
        names = [self.__disk.name(block) for block in directory]
        return names

    # Check If Empty
    def empty(self, path):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        assert block
        return self.__disk.empty(block)

    # Changes Directory/File Name
    def rename(self, path, name):
        block, old_name = self.__resolve_path(path)
        block = self.__disk.find(block, old_name)
        assert block
        self.__disk.rename(block, name)

    # Test For Existance
    def exists(self, path):
        try:
            block, name = self.__resolve_path(path)
            block = self.__disk.find(block, name)
            assert block
            return True
        except:
            return False

    # Check If File
    def is_file(self, path):
        block, name = self.__resolve_path(path)
        block = self.__disk.find(block, name)
        return self.__disk.is_file(block)

    # Check If Directory
    def is_directory(self, path):
        assert type(path) is str
        if path:
            block, name = self.__resolve_path(path)
            block = self.__disk.find(block, name)
            return self.__disk.is_directory(block)
        else:
            return True

    # Seed Control Interface
    def seed(self, data=None):
        return self.__disk.seed(data)

    # Probability Of Failure
    def fail(self, probability):
        self.__disk.fail(probability)

    # Dump To File
    def dump(self, name):
        self.__disk.dump(name)

    # Load From File
    def load(self, name, abstract):
        assert type(abstract) is bool
        self.__disk.load(name, abstract)
        if abstract:
            self.__soft()
        else:
            self.__hard()

    # Fix All Errors
    def __soft(self):
        # Not Yet Implemented
        pass

    # Find Any Error
    def __hard(self):
        # Not Yet Implemented
        pass

    # Private Utility Function
    def __resolve_path(self, path):
        assert type(path) is str
        if path:
            table = ''.join([chr(i) for i in range(256)])
            parts = path.split(self.PATH_SEPARATOR)
            block = 1
            for name in parts[:-1]:
                assert len(name.translate(table, self.NAME_CHARACTERS)) == 0
                block = self.__disk.find(block, name)
                assert block
            assert parts[-1]
            assert len(parts[-1].translate(table, self.NAME_CHARACTERS)) == 0
            return block, parts[-1]
        else:
            return 1, ''

################################################################################

def test():
    # Not Yet Implemented
    pass

################################################################################

if __name__ == '__main__':
    test()

This recipe is part of a group of recipes that detail the creation of an abstract file system. dal_4.py can be found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/492212