Васил обнови решението на 29.04.2015 23:54 (преди над 9 години)
+class FileSystemError(Exception):
+
+ def __init_(self):
+ self.message = 'FileSystemError'
+
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NodeDoesNotExist'
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'DestinationNodeDoesNotExist'
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NotEnoughSpaceError'
+
+
+class DestinationNotADirectoryError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'DestinationNotADirectory'
+
+
+class DestinationNodeExistsError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'DestinationNodeExists'
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'SourceNodeDoesNotExist'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = "NonExplicitDirectoryDeletion"
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ self.message = 'NonEmptyDirectoryDeletion'
+
+
+class Basic:
+
+ def __init__(self, name):
+ self.name = name
+
+ def _print(self, t='Basic'):
+ print('Name: {0}, Type: {1}'.format(self.name, t))
+
+
+class File(Basic):
+
+ def __init__(self, name, cont=''):
+ Basic.__init__(self, name)
+ self.content = cont
+ self.is_directory = False
+
+ def _print(self):
+ Basic._print(self, 'File , Content: ' + self.content)
+
+ def size(self):
+ return len(self.content) + 1
+
+ def append(self, text):
+ self.content += text
+
+ def truncate(self, text=''):
+ self.content = text
+
+
+class Directory(Basic):
+
+ def __init__(self, name, direct=[], fls=[]):
+ Basic.__init__(self, name)
+ self.directories = direct
+ self.files = fls
+ self.nodes = [self.directories, self.files]
+ self.is_directory = True
+
+ def _print(self):
+ Basic._print(self, 'Directory')
+
+
+import itertools
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = self.size - 1
+ self.root = Directory('')
+
+ def get_node(self, path):
+ words = path.split('/')
+ temp = self.root
+ paths_len = len(words)
+
+ if paths_len == 1 or temp.name is not words[0]:
+ raise NodeDoesNotExistError
+
+ if words == ['', '']:
+ return temp
+ else:
+ words.pop(0)
+ i = 0
+ found = False
+ while True:
+ for node in list(itertools.chain(*temp.nodes)):
+ if node.name == words[i]:
+ found = True
+ temp = node
+ if i + 1 == paths_len - 1:
+ return temp
+ elif type(temp) is File:
+ raise NodeDoesNotExistError
+
+ if not found:
+ raise NodeDoesNotExistError
+ found = False
+ i += 1
+
+ def create(self, path, directory=False, content=''):
+
+ if directory == False and content is not '':
+ new_node = File(path.split('/')[-1], content)
+ elif directory or content == '':
+ new_node = Directory(path.split('/')[-1])
+
+ path_dir = path.split('/')
+ path_dir.pop(len(path_dir) - 1)
+ if path_dir == ['']:
+ path_dir = '/'
+ else:
+ path_dir = str.join('/', path_dir)
+
+ try:
+ dir_to_create = self.get_node(path_dir)
+
+ except NodeDoesNotExistError as e:
+ raise DestinationNodeDoesNotExistError
+ else:
+ try:
+ self.get_node(path)
+ except NodeDoesNotExistError:
+ if type(new_node) is File and new_node.size() <= self.available_size:
+ dir_to_create.files.append(new_node)
+ self.available_size -= new_node.size()
+ elif type(new_node) is Directory and self.available_size >= 1:
+ dir_to_create.directories.append(new_node)
+ self.available_size -= 1
+ else:
+ raise NotEnoughSpaceError
+ else:
+ raise DestinationNodeExistsError
+
+ def remove(self, path, directory=False, force=True):
+ try:
+ to_remove = self.get_node(path)
+ except FileExistsError:
+ raise NodeDoesNotExistError
+ else:
+ if not directory and to_remove.is_directory:
+ raise NonExplicitDirectoryDeletionError
+ elif (type(to_remove) is File or
+ (directory and
+ len(list(itertools.chain(*to_remove.nodes))) is not 0)):
+ if not force:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ path_dir = path.split('/')
+ path_dir.pop(len(path_dir) - 1)
+ if path_dir == ['']:
+ path_dir = '/'
+ else:
+ path_dir = str.join('/', path_dir)
+ if to_remove.is_directory:
+ self.get_node(path_dir).directories.remove(to_remove)
+ else:
+ self.get_node(path_dir).files.remove(to_remove)
+
+ def move(self, source, destination):
+ from copy import deepcopy
+ try:
+ to_move = deepcopy(self.get_node(source))
+ # self.remove(source)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError
+ else:
+ try:
+ dest = self.get_node(destination)
+ # dest._print()
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+ else:
+ if dest.is_directory:
+ if to_move.is_directory:
+ dest.directory.append(to_move)
+ self.remove(source)
+ else:
+ dest.files.append(to_move)
+ self.remove(source)
+
+ else:
+ raise DestinationNotADirectoryError