Елица обнови решението на 30.04.2015 02:19 (преди над 9 години)
+class Filee:
+
+ def __init__(self, name, content):
+ self.name = name
+ self.content = content
+ self.size = len(content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.content.append(text)
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(size) + 1
+
+
+class Direc:
+
+ def __init__(self, directory, files=list()):
+ self.name = directory
+ self.nodes = list()
+ self.directories = list()
+ self.files = files
+ self.is_directory = True
+
+ def addDirectory(self, directory):
+ new_directories = list()
+ new_directories.extend(self.directories)
+ new_directories.append(directory)
+ self.directories = new_directories
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.append(directory)
+ self.nodes = new_nodes
+
+ def addFile(self, filee):
+ new_files = list()
+ new_files.extend(self.files)
+ new_files.append(filee)
+ self.files = new_files
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.append(filee)
+ self.nodes = new_nodes
+
+ def deleteNodes(self, name):
+ for d in self.directories:
+ if name == d.name:
+ new_dirs = list()
+ new_dirs.extend(self.directories)
+ new_dirs.remove(d)
+ self.directories = new_dirs
+ for f in self.files:
+ if name == f.name:
+ new_files = list()
+ new_files.extend(self.files)
+ new_files.remove(f)
+ self.files = new_files
+ for n in self.nodes:
+ if name == n.name:
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.remove(n)
+ self.nodes = new_nodes
+
+ def getAllNames(self):
+ names = list()
+ for n in self.nodes:
+ names.append(n.name)
+ return names
+
+
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError (FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError (FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError (FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError (FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError (FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError (NodeDoesNotExistError):
+ pass
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.direc = Direc('/')
+
+ def get_paths(self, directory, last_dir, path):
+ if last_dir is '':
+ new_name = directory.name
+ elif last_dir is '/':
+ new_name = last_dir + directory.name
+ else:
+ new_name = last_dir + '/' + directory.name
+ if new_name not in path:
+ path[new_name] = directory
+ for f in directory.files:
+ if last_dir == '':
+ path['/' + f.name] = f
+ elif last_dir == '/':
+ path['/' + directory.name + '/' + f.name] = f
+ else:
+ path[last_dir + '/' + directory.name + '/' + f.name] = f
+ for d in directory.directories:
+ self.get_paths(d, new_name, path)
+ return path
+
+ def get_node(self, path):
+ paths = self.get_paths(self.direc, '', dict())
+ if path in paths:
+ return paths[path]
+ else:
+ raise NodeDoesNotExistError()
+
+ def createFile(self, content, path, paths, node, current_dir, sub_dirs):
+ if self.available_size >= len(content) + 1:
+ if path not in paths:
+ if node is None:
+ node = Filee(sub_dirs[len(sub_dirs) - 1], content)
+ paths[current_dir].addFile(node)
+ self.available_size -= len(content) + 1
+ else:
+ raise DestinationNodeExistsError()
+ else:
+ raise NotEnoughSpaceError()
+
+ def createDir(self, path, paths, node, sub_dirs, current_dir):
+ if self.available_size >= 1:
+ if path not in paths:
+ if node is None:
+ node = Direc(sub_dirs[len(sub_dirs) - 1])
+ paths[current_dir].addDirectory(node)
+ self.available_size -= 1
+ else:
+ raise DestinationNodeExistsError()
+ else:
+ raise NotEnoughSpaceError()
+
+ def create(self, path, directory=False, content='', node=None):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = path.split('/')
+ current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if current_dir in paths:
+ if type(paths[current_dir]) is Direc:
+ if directory:
+ self.createDir(path, paths, node, sub_dirs, current_dir)
+ else:
+ self.createFile(content, path, paths, node,
+ current_dir, sub_dirs)
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = path.split('/')
+ current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if path in paths:
+ if type(paths[path]) is Direc and directory:
+ if len(paths[path].nodes) == 0 or force is False:
+ paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ else:
+ raise NonEmptyDirectoryDeletionError()
+ elif type(paths[path]) is Filee:
+ paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ else:
+ raise NonExplicitDirectoryDeletionError()
+ else:
+ raise NodeDoesNotExistError()
+
+ def createAndRemove(self, sub_dirs, paths, current_dir, source, destination):
+ if sub_dirs[len(sub_dirs) - 1] not in paths[current_dir].getAllNames():
+ if type(paths[source]) is Direc:
+ self.create(destination, directory=True, node=paths[source])
+ self.remove(source, directory=True)
+ else:
+ self.create(destination, directory=False, node=paths[source])
+ self.remove(source, directory=False)
+ else:
+ raise DestinationNodeExistsError()
+
+ def move(self, source, destination):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = source.split('/')
+ destination_splitted = destination.split('/')
+ current_dir = destination[0:len(destination) - len(
+ destination_splitted[len(destination_splitted) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if source in paths:
+ if destination in paths:
+ if type(paths[destination]) is Direc:
+ self.createAndRemove(sub_dirs, paths, current_dir, source,
+ destination)
+ else:
+ raise DestinationNotADirectoryError()
+ else:
+ raise DestinationNodeDoesNotExistError()
+ else:
+ raise SourceNodeDoesNotExistError()