Спасимир обнови решението на 30.04.2015 15:00 (преди над 9 години)
+class FileSystemError(Exception):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.is_directory = True
+ self.directories = []
+ self.files = []
+ self.nodes = self.directories + self.files
+
+ def __iter__(self):
+ return iter(self.nodes)
+
+
+class File:
+ def __init__(self, name, content=''):
+ self.name = name
+ self.is_directory = False
+ self.content = content
+ self.size = len(self.content) + 1
+
+ def content(self):
+ return self.content
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text):
+ self.content = text
+
+
+def prev_path(path):
+ sample = ''
+ for i in path.split('/')[1:-1]:
+ sample += ('/' + i)
+ return sample
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.path = Directory('/')
+
+ def __validate_existence_of_dir(self, dirs):
+ size = len(dirs) - 1
+ print(dirs)
+ if size is -1:
+ return True
+ root = self.path
+ for i in range(0, size):
+ current_dir = (r for r in root.directories if r.name == dirs[i])
+ if not current_dir:
+ return False
+ index = root.directories.index(list(current_dir)[0])
+ root = root.directories[index]
+ return True
+
+ def get_node(self, path):
+ if path == '' or path not in self.path.name:
+ raise NodeDoesNotExistError
+ elif self.path.is_directory:
+ return Directory(path)
+ else:
+ return File(path)
+
+ def create(self, path, directory=False, content=''):
+ paths = list(filter(None, path.split('/')))
+ if prev_path(path) not in self.path.name:
+ raise DestinationNodeDoesNotExistError
+ elif self.available_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ if directory:
+ root_dir = self.get_node('/'.join(prev_path(path)))
+ child_dir = Directory(paths[-1])
+ root_dir.directories.append(child_dir)
+ self.available_size -= 1
+ else:
+ self.path = File(path, content)
+ self.available_size -= len(content) + 1
+
+ def remove(self, path, directory=False, force=True):
+ pass
+
+ def move(self, source, destination):
+ pass