Цветан обнови решението на 30.04.2015 16:37 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class File:
+ def __init__(self, name='',content=''):
+ self.name = name
+ self.content = content
+
+
+ def size(self):
+ return len(self.content) + 1
+
+
+ def append(self, text):
+ self.content += text
+
+
+ def truncate(self, text):
+ del self.content
+ self.content = text
+
+
+class Directory:
+ directories =[]
+ files = []
+ nodes = []
+ def __init__(self, name='/'):
+ self.name = name
+
+
+ def add_file(self, name, content=''):
+ f = File(name, content)
+ self.nodes.append(f)
+ self.files.append(f)
+
+
+ def add_dir(self, name):
+ d = Directory(name)
+ self.nodes.append(d)
+ self.directories.append(d)
+
+
+class FileSystem:
+ available_size = 0
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.root = Directory()
+ self.root.add_dir('/')
+
+
+ def get_node(self, path):
+ names = []
+ names_of_files = []
+ names_of_dirs = []
+ for x in self.root.nodes:
+ names.append(x.name)
+
+ for y in self.root.files:
+ names_of_files.append(y.name)
+
+ for z in self.root.directories:
+ names_of_dirs.append(z.name)
+
+ if path not in names:
+ raise NodeDoesNotExistError
+
+ if path in names_of_dirs:
+ for directorie in self.root.directories:
+ if directorie.name == path:
+ return directorie
+
+ else:
+ for f in self.root.files:
+ if f.name == path:
+ return f
+
+
+ def create(self, path, directory=False, content=''):
+ if path.count('/') == 1:
+ a = self.get_node(path[:path.rfind('/') + 1])
+ else:
+ a = self.get_node(path[:path.rfind('/')])
+
+ if path.count('/') != len(a.nodes):
+ raise DestinationNodeExistsError
+ names = []
+ for x in self.root.nodes:
+ names.append(x.name)
+
+ if path.count('/') == 1:
+ if path[:path.rfind('/') + 1] not in names:
+ raise DestinationNodeDoesNotExistError
+ else:
+ if path[:path.rfind('/')] not in names:
+ raise DestinationNodeDoesNotExistError
+
+ if content != '':
+ if self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+ self.root.add_file(path, content)
+ self.available_size -= len(content) + 1
+
+ else:
+ if self.available_size < 1:
+ raise DestinationNodeDoesNotExistError
+ self.root.add_dir(path)
+ self.available_size -= 1