Радослав обнови решението на 30.04.2015 01:09 (преди над 9 години)
+import os
+
+
+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = 'Something with the file system went terribly wrong!'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non explicit directory deletion attempt!'
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ self.message = 'Can not make hard link to a directory!'
+
+
+class NotAFileError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non explicit directory deletion attempt!'
+
+
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ self.message = 'Can not mount'
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ self.message = 'Not enough space'
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ def __init__(self):
+ self.message = 'Specified destination is not a directory'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non empty directory deletion attempt'
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point is not empty!'
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point does not exist!'
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point is not a directory!'
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ self.message = 'Destination node exists!'
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ self.message = 'Node does not exist!'
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'Destination node does not exist!'
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'Source node does not exist!'
+
+
+class Element(object):
+ def __init__(self, path, is_directory, content=''):
+ self.path = path
+ self.is_directory = is_directory
+ self.content = content
+ self.size = len(content) + 1
+ self.parent = None
+
+ def get_name(self):
+ return self.path.rpartition('/')[2]
+
+ def append(self, text):
+ if self.is_directory:
+ raise NotAFileError
+
+ if self.parent.available_size <= len(text):
+ raise NotEnoughSpaceError
+ else:
+ self.content += text
+ self.parent.available_size = self.parent.available_size - len(text)
+
+ def truncate(self, text):
+ if self.is_directory:
+ raise NotAFileError
+
+ size_difference = len(text) - self.size
+ if self.parent.available_size >= size_difference:
+ self.content = text
+ self.parent.available_size = self.parent.available_size \
+ - size_difference
+
+ def directories(self):
+ if not self.is_directory:
+ raise NotADirectoryError
+ else:
+ directories = list()
+ for x in self.parent.elements:
+ if x.is_directory and x.path.rpartition('/')[0] == self.path:
+ directories.append(x)
+ return directories
+
+ def files(self):
+ if not self.is_directory:
+ raise NotADirectoryError
+ else:
+ files = list()
+ for x in self.parent.elements:
+ if not x.is_directory \
+ and x.path.rpartition('/')[0] == self.path:
+ files.append(x)
+ return files
+
+
+class FileSystem(object):
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.elements = list()
+
+ def _get_parrent_folder(self, path):
+ return path.rpartition('/')[0]
+
+ def _parent_folder_exists(self, path):
+ for x in self.elements:
+ if x.path == self._get_parrent_folder(path):
+ return True
+ return False
+
+ def _element_has_parent(self, element, parent):
+ return element.path.starswith(parent)
+
+
+ def _element_exists(self, path):
+ for elem in self.elements:
+ if elem.path == path:
+ return True
+ return False
+
+ def _folder_empty(self, path):
+ for x in self.elements:
+ if path == self._get_parrent_folder(x.path):
+ return False
+ return True
+
+ def get_node(self, path):
+ for element in self.elements:
+ if element.path == path:
+ return element
+
+ def create(self, path, directory=False, content=''):
+ if len(content) + 1 > self.available_size:
+ raise NotEnoughSpaceError
+ if self._element_exists(path):
+ raise DestinationNodeExistsError
+ if self._parent_folder_exists(path) or path.count('/') == 1:
+ element = Element(path, directory, content)
+ self.elements.append(element)
+ self.available_size -= element.size
+ element.parent = self
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ if self._element_exists(path):
+ if not directory:
+ for element in self.elements:
+ if element.path == path:
+ if element.is_directory:
+ raise NonExplicitDirectoryDeletionError
+ self.elements.remove(element)
+ else:
+ if self._folder_empty(path):
+ for element in self.elements:
+ if element.path == path:
+ self.elements.remove(element)
+ else:
+ if not force:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ for element in self.elements:
+ if element.path == path \
+ or path == self.\
+ _get_parrent_folder(element.path):
+ self.elements.remove(element)
+ else:
+ raise NodeDoesNotExistError
+
+ def move(self, source, destination):
+ if not self._element_exists(source):
+ raise SourceNodeDoesNotExistError
+ if not self._element_exists(destination):
+ raise DestinationNodeDoesNotExistError
+ for x in self.elements:
+ if x.path == destination:
+ if not x.is_directory:
+ raise DestinationNotADirectoryError
+ else:
+ if self._element_exists(destination + '/' + source):
+ raise DestinationNodeExistsError
+ else:
+ if x.is_directory:
+ for e in self.elements:
+ if self._element_has_parent(e.path, x.path):
+ e.path = destination \
+ + e.path.lstrip(source)
+ x.path = destination + '/' + x.get_name()
+
+ def link(self, source, destination, symbolic=True):
+ if not self._element_exists(source):
+ raise SourceNodeDoesNotExistError
+ elif not self._element_exists(destination):
+ raise DestinationNodeDoesNotExistError
+ elif self._element_exists(source) and not symbolic \
+ and self.get_node(source).is_directory:
+ raise DirectoryHardLinkError
+ else:
+ if not symbolic and self.available_size >= len(fs.get_node(source)):
+ fs.create(destination, content=fs.get_node(source).content)
+