Теодора обнови решението на 29.04.2015 23:10 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class Node():
+
+ def __init__(self, path, is_directory, file_system):
+ self.path = path
+ self.is_directory = is_directory
+ self.file_system = file_system
+
+ def __repr__(self):
+ return self.path
+
+ @property
+ def size(self):
+ return 0
+
+
+class File(Node):
+
+ def __init__(self, path, content, file_system):
+ Node.__init__(self, path, False, file_system)
+ splitted_path = path.split('/')
+ self.content = content
+
+ # def __repr__(self):
+ # return self.content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+ @property
+ def size(self):
+ return len(self.content) + 1
+
+
+class Directory(Node):
+
+ def __init__(self, path, file_system):
+ Node.__init__(self, path, True, file_system)
+
+ @property
+ def directories(self):
+ return [node for node in self.file_system.file_system[self.path] if node.is_directory]
+
+ @property
+ def files(self):
+ return [node for node in self.file_system.file_system[self.path] if not node.is_directory]
+
+ @property
+ def nodes(self):
+ return self.file_system.file_system[self.path]
+
+ @property
+ def size(self):
+ return 1
+
+
+class FileSystem():
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.file_system = {'/': []}
+
+ # MNOGO, MNOGO GROZNO, UJASNO
+ def get_path_to(self, path):
+ splitted_path = path.split('/')
+ if '' in splitted_path:
+ splitted_path.remove('')
+ splitted_path = splitted_path[:-1]
+ for i in range(1, len(splitted_path)):
+ splitted_path[i] = '/' + splitted_path[i]
+ path_to = '/'
+ for p in splitted_path:
+ path_to += p
+
+ return path_to
+
+ def create(self, path, directory=False, content=''):
+ path_to = self.get_path_to(path)
+ if path_to not in self.file_system.keys():
+ raise DestinationNodeDoesNotExistError()
+
+ if path in self.file_system.keys():
+ raise DestinationNodeExistsError()
+
+ new_node = None
+ if directory == True:
+ new_node = Directory(path, self)
+ else:
+ new_node = File(path, content, self)
+
+ self.available_size -= new_node.size
+
+ if self.available_size < 0:
+ raise NotEnoughSpaceError()
+
+ self.file_system[path_to].append(new_node)
+ if directory:
+ self.file_system[path] = []
+
+ def get_node(self, path):
+ path_to = self.get_path_to(path)
+
+ if path_to not in self.file_system.keys():
+ raise NodeDoesNotExistError()
+
+ for p in self.file_system[path_to]:
+ if p.path == path:
+ return p
+ if path in self.file_system.keys():
+ return Directory(path, self)
+ raise NodeDoesNotExistError()
+
+ def recursive_remove(self, path):
+ if path not in self.file_system.keys():
+ return
+ for s in self.file_system[path]:
+ self.recursive_remove(s.path)
+ if s.path in self.file_system.keys():
+ self.file_system.pop(s.path)
+
+ def create_node(self, node):
+ content = ''
+ if not node.is_directory:
+ content = node.content
+ try:
+ self.create(node.path, node.is_directory, content)
+ except:
+ pass
+
+ def remove(self, path, directory=False, force=True):
+ node = None
+ try:
+ node = self.get_node(path)
+ except:
+ raise
+ path_to = self.get_path_to(path)
+ if not directory and node.is_directory:
+ raise NonExplicitDirectoryDeletionError()
+ if directory and len(node.nodes) > 0 and not force:
+ raise NonEmptyDirectoryDeletionError()
+ if directory and force:
+ self.recursive_remove(path_to)
+ self.file_system[path_to].remove(node)
+
+ def move(self, source, destination):
+ node = None
+ try:
+ node = self.get_node(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError()
+ destination_node = None
+ try:
+ destination_node = self.get_node(destination)
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError()
+
+ if destination_node.is_directory == False:
+ raise DestinationNotADirectoryError()
+ if node in self.file_system[destination]:
+ raise DestinationNodeExistsError()
+
+ node_name = source.split('/')[-1]
+ node.path = destination + '/' + node_name
+ self.create_node(node)
+ self.remove(source, node.is_directory)