Мария обнови решението на 30.04.2015 16:28 (преди над 9 години)
+separator = "/"
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ if size <= 0:
+ raise NotEnoughSpaceError
+ root = Directory("", "", {})
+ self.structure = {root.name: root}
+ self.flattened_structure = {}
+ self.size = size
+ self.available_size = size - 1
+
+ def system_size(self):
+ size = 0
+ for cur_file in flatten_dict(self.structure[""]).values():
+ if cur_file.endswith(".d"):
+ size += 1
+ if cur_file.endswith(".f"):
+ size += len(get_name(cur_file[:-2]))
+ return size + 1
+
+ def get_node(self, path):
+ if not does_exist(path, self.structure[""]):
+ raise NodeDoesNotExistError
+ parent_folder = parent_folder_path(path, self.structure[""])
+ name = get_name(path)
+ if path == "":
+ return self.structure[""]
+ if parent_folder.subdirs[name].is_directory:
+ return parent_folder.subdirs[name]
+ elif not parent_folder.subdirs[name].is_directory:
+ return parent_folder.subdirs[name]
+
+ def create(self, path, directory=False, content=''):
+ if does_exist(path, self.structure[""]):
+ raise DestinationNodeExistsError
+ if not does_exist(get_path(path), self.structure[""]):
+ raise DestinationNodeDoesNotExistError
+ if directory:
+ if self.available_size < 1:
+ raise NotEnoughSpaceError
+ else:
+ insert_directory_with_path(
+ path,
+ self.structure[""],
+ {}
+ )
+ else:
+ if self.available_size < 1 + len(content):
+ raise NotEnoughSpaceError
+ else:
+ insert_file_with_path(path,
+ self.structure[""],
+ content)
+
+
+class FileSystemError(Exception):
+
+ def __init__(self, message="System error."):
+ self.message = message
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self, message="Insufficient recources."):
+ self.message = message
+
+
+class DestinationNodeExistsError(FileSystemError):
+
+ def __init__(self, message="File/ folder already exists."):
+ self.message = message
+
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self, message="File/ directory does not exist."):
+ self.message = message
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self, message="Source does not exist."):
+ self.message = message
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self, message="Destination does not exist"):
+ self.message = message
+
+
+class FileSystemMountError(FileSystemError):
+
+ def __init__(self, message="FileSystem mount error."):
+ self.message = message
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+
+ def __init__(self, message="Mount point does not exist."):
+ self.message = message
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+
+ def __init__(self, message="Mount point is not a directory."):
+ self.message = message
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+
+ def __init__(self, message="Mount point is not an empty directory."):
+ self.message = message
+
+
+def parent_folder_path(path, structure):
+ path = get_path(path)
+ subfolders = get_subfolders(path)[1:]
+ for name in subfolders:
+ structure = structure.subdirs[name]
+ return structure
+
+
+def does_exist(path, structure):
+ subfolders = get_subfolders(path)[1:]
+ try:
+ for name in subfolders:
+ structure = structure.subdirs[name]
+ return True
+ except KeyError:
+ return False
+ except AttributeError:
+ return True
+
+
+def directory_insertions(
+ path, structure, dict_from_structure):
+ if not does_exist(get_path(path), structure) or \
+ (dict_from_structure != {}) and \
+ (not does_exist(path, structure)):
+ return None
+ name = get_name(path)
+ folder = Directory(name, path, dict_from_structure)
+ cur_dict_from_structure = {folder.name: folder}
+ parent_folder = parent_folder_path(path, structure)
+ parent_folder.subdirs.update(cur_dict_from_structure)
+
+
+def insert_file_with_path(path, structure, content=""):
+ if not does_exist(get_path(path), structure):
+ return None
+ cur_file = File(get_name(path), path, content)
+ cur_dict_from_structure = {cur_file.name: cur_file}
+ parent_folder = parent_folder_path(path, structure)
+ parent_folder.subdirs.update(cur_dict_from_structure)
+
+
+class File:
+
+ def __init__(self, name, path="", content=''):
+ self.content = content
+
+ self.name = name
+ self.path = path
+ self.size = len(self.content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.content += text
+ self.size = len(self.content) + 1
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(self.content) + 1
+
+
+class Directory:
+
+ def __init__(self, name, path="", dirs={}):
+ self.subdirs = dirs
+
+ self.name = name
+ self.path = path
+ self.size = 1
+ self.is_directory = True
+
+
+def get_name(path):
+ return path[path.rfind("/") + 1:]
+
+
+def get_path(path):
+ return path[:path.rfind("/")]
+
+
+def get_subfolders(path):
+ return path.split("/")
+
+
+def flatten_dict(structure, left_key=''):
+ result = {}
+ for right_key, object_with_structure in structure.subdirs.items():
+ key = left_key + right_key
+ if (object_with_structure.is_directory and
+ object_with_structure.subdirs != {}):
+ result[separator + key] = object_with_structure.name + ".d"
+ result.update(flatten_dict(object_with_structure, key + separator))
+
+ elif object_with_structure.is_directory:
+ result[separator + key] = object_with_structure.name + ".d"
+ else:
+ result[separator + key] = object_with_structure.name + ".f"
+ return result
+
+
+def is_directory(path, structure):
+ try:
+ parent_folder = parent_folder_path(path, structure)
+ dir_name = get_name(path)
+ cur_dir = parent_folder.subdirs[dir_name]
+ if cur_dir.is_directory:
+ return True
+ else:
+ return False
+ except KeyError as error:
+ if path == "":
+ return True
+ return 'ERROR', error