Решение на In-memory файлова система от Любослав Кънев

Обратно към всички решения

Към профила на Любослав Кънев

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 5 успешни тест(а)
  • 13 неуспешни тест(а)

Код

class FileSystemError(Exception):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class FileSystem:
def __init__(self, size_in_bytes):
if size_in_bytes < 1:
raise NotEnoughSpaceError
self.__SIZE_OF_DIR = 1
self.size = size_in_bytes
self.available_size = size_in_bytes - self.__SIZE_OF_DIR
self.__root = Directory("/")
def get_node(self, path):
if len(path) > 0 and path[0] is not "/":
raise NodeDoesNotExistError()
elif path == "/":
return self.__root
else:
node = self.__root
current_path = path[1:]
while "/" in current_path:
index = current_path.find("/")
current_dir = current_path[:index]
current_path = current_path[index + 1:]
node = FileSystem.__find_node(current_dir, node)
return self.__find_node(current_path, node)
def create(self, path, directory=False, content=""):
parent_path = path[:path.rfind("/")]
try:
parent = self.get_node(parent_path if parent_path != "" else "/")
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
if parent.is_directory:
file_size = (len(content) if not directory else 0) + 1
new_file_name = path[path.rfind("/") + 1:]
if new_file_name in parent.nodes:
raise DestinationNodeExistsError
if self.available_size < file_size:
raise NotEnoughSpaceError
try:
self.__find_node(new_file_name, parent)
except NodeDoesNotExistError:
if directory:
parent.add_directory(new_file_name)
else:
parent.add_file(new_file_name, content)
self.available_size -= file_size # check if not negative
else:
raise DestinationNodeExistsError
else:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
def remove(self, path, directory=False, force=True):
parent_path = FileSystem.__get_parent(path)
parent = self.get_node(parent_path)
node = self.get_node(FileSystem.__get_parent(path))
name = FileSystem.__get_filename(path)
if node.is_directory:
if directory:
try:
if self.__is_empty_dir(path):
self.available_size += node.size
parent.remove_dir(name)
else:
if force:
for key in node.nodes:
self.remove(path + "/" + key,
directory=True)
else:
raise NonEmptyDirectoryDeletionError
except FileSystemError as e:
pass # the node is said to be a directory but it is a file
else:
raise NonExplicitDirectoryDeletionError
else:
self.available_size += node.size
node.remove_file(name)
def remove_old(self, path, directory=False, force=True):
node = self.get_node(FileSystem.__get_parent(path))
name = FileSystem.__get_filename(node)
if not directory:
if not node.is_directory:
self.available_size += node.size
node.remove_file(name)
else:
pass # the node is said not to be directory but it is a
# directory
else:
try:
if self.__is_empty_dir(node):
pass # delete dir
else:
if force:
pass # delete recursively
else:
raise NonEmptyDirectoryDeletionError
except FileSystemError as e:
pass # the node is said to be a directory but it is a file
def move(self, source, destination):
pass
def link(self, source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pass
def unmount(self, path):
pass
@staticmethod
def __find_node(node_name, directory):
if node_name in directory.nodes:
return directory.nodes[node_name]
raise NodeDoesNotExistError
def __is_empty_dir(self, path):
node = self.get_node(path)
if node.is_directory:
return not node.nodes
else:
raise FileSystemError
@staticmethod
def __get_parent(path):
return path[:path.rfind("/")]
@staticmethod
def __get_filename(path):
return path[path.rfind("/") + 1:]
class Directory:
def __init__(self, name):
self.name = name
self.files = {}
self.directories = {}
self.nodes = {}
self.is_directory = True
self.size = 1
def add_directory(self, name):
if self.is_available_name(name):
self.directories.update({name: Directory(name)})
self.__update_nodes()
else:
raise DestinationNodeExistsError
def add_file(self, name, content):
if self.is_available_name(name):
self.files.update({name: File(name, content)})
self.__update_nodes()
else:
raise DestinationNodeExistsError
def remove_file(self, name):
self.files.pop(name, None)
self.__update_nodes()
def remove_dir(self, name):
self.directories.pop(name, None)
self.__update_nodes()
def is_empty(self):
return not self.nodes
def __update_nodes(self):
self.nodes.update(self.directories)
self.nodes.update(self.files)
def is_available_name(self, name):
return name not in self.nodes.keys()
class File:
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.__set_content(self.content + text)
def truncate(self, text):
self.__set_content(text)
def __set_content(self, text):
self.content = text
self.__update_size()
def __update_size(self):
self.size = len(self.content) + 1

Лог от изпълнението

..EEEEE.EEE..EEEEE
======================================================================
ERROR: test_hard_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_space_consumption (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_hard_link_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_link_create (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_mounting (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_not_to_a_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_move_overwrite (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_empty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_remove_nonempty_directory (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_symlink_to_missing_file (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_valid_move (test.TestFileSystem)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 18 tests in 26.156s

FAILED (errors=13)

История (3 версии и 0 коментара)

Любослав обнови решението на 30.04.2015 15:45 (преди над 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class FileSystem:
+ def __init__(self, size_in_bytes):
+ self.__SIZE_OF_DIR = 1
+ self.__size = size_in_bytes
+ self.__available_size = size_in_bytes - self.__SIZE_OF_DIR
+ self.__root = Directory("/")
+
+ def get_node(self, path):
+ if len(path) > 0 and path[0] is not "/":
+ raise NodeDoesNotExistError()
+ elif path == "/":
+ return self.__root
+ else:
+ node = self.__root
+ current_path = path[1:]
+ while "/" in current_path:
+ index = current_path.find("/")
+ current_dir = current_path[:index]
+ current_path = current_path[index + 1:]
+
+ node = FileSystem.__find_node(current_dir, node)
+
+ return self.__find_node(current_path, node)
+
+ def create(self, path, directory=False, content=""):
+ parent_path = path[:path.rfind("/")]
+
+ try:
+ parent = self.get_node(parent_path if parent_path != "" else "/")
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError("The path to the file "
+ "does not exist.")
+ if parent.is_directory:
+ file_size = (len(content) if not directory else 0) + 1
+ if self.__available_size < file_size:
+ raise NotEnoughSpaceError
+
+ new_file_name = path[path.rfind("/") + 1:]
+
+ if new_file_name in parent.nodes:
+ raise Exception
+ try:
+ self.__find_node(new_file_name, parent)
+ except NodeDoesNotExistError:
+ if directory:
+ parent.add_directory(new_file_name)
+ else:
+ parent.add_file(new_file_name, content)
+
+ self.__available_size -= file_size # check if not negative
+ else:
+ raise DestinationNodeExistsError
+
+ else:
+ raise DestinationNodeDoesNotExistError("The path to the file "
+ "does not exist.")
+
+ def remove(self, path, directory=False, force=True):
+ node = self.get_node(FileSystem.__get_parent(path))
+ name = FileSystem.__get_filename(node)
+
+ if not directory:
+ if not node.is_directory:
+ node.remove_file(name)
+ else:
+ pass # the node is said not to be directory but it is a
+ # directory
+ else:
+ try:
+ if self.__is_empty_dir(node):
+ pass # delete dir
+ else:
+ if force:
+ pass # delete recursively
+ else:
+ raise NonEmptyDirectoryDeletionError
+
+ except FileSystemError as e:
+ pass # the node is said to be a directory but it is a file
+
+ def move(self, source, destination):
+ pass
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass
+
+ @staticmethod
+ def __find_node(node_name, directory):
+ if node_name in directory.nodes:
+ return directory.nodes[node_name]
+
+ raise NodeDoesNotExistError
+
+ def __is_empty_dir(self, path):
+ node = self.get_node(path)
+
+ if node.is_directory:
+ return not node.nodes
+ else:
+ raise FileSystemError
+
+ @staticmethod
+ def __get_parent(path):
+ return path[:path.rfind("/")]
+
+ @staticmethod
+ def __get_filename(path):
+ return path[path.rfing("/") + 1:]
+
+
+class Directory:
+ def __init__(self, name):
+ self.name = name
+ self.files = {}
+ self.directories = {}
+ self.nodes = {}
+ self.is_directory = True
+
+ def add_directory(self, name):
+ if self.is_available(name):
+ self.directories.update({name: Directory(name)})
+ self.__update_nodes()
+ else:
+ raise Exception
+
+ def add_file(self, name, content):
+ if self.is_available(name):
+ self.files.update({name: File(name, content)})
+ self.__update_nodes()
+ else:
+ raise Exception
+
+ def remove_file(self, name):
+ self.files.pop(name, None)
+
+ def __update_nodes(self):
+ self.nodes.update(self.directories)
+ self.nodes.update(self.files)
+
+ def is_available(self, name):
+ return name not in self.nodes.keys()
+
+
+class File:
+ 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.__set_content(self.content + text)
+
+ def truncate(self, text):
+ self.__set_content(text)
+
+ def __set_content(self, text):
+ self.content = text
+ self.__update_size()
+
+ def __update_size(self):
+ self.size = len(self.content) + 1

Любослав обнови решението на 30.04.2015 16:50 (преди над 9 години)

class FileSystemError(Exception):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class FileSystem:
def __init__(self, size_in_bytes):
+ if size_in_bytes < 1:
+ raise NotEnoughSpaceError
self.__SIZE_OF_DIR = 1
- self.__size = size_in_bytes
- self.__available_size = size_in_bytes - self.__SIZE_OF_DIR
+ self.size = size_in_bytes
+ self.available_size = size_in_bytes - self.__SIZE_OF_DIR
self.__root = Directory("/")
def get_node(self, path):
if len(path) > 0 and path[0] is not "/":
raise NodeDoesNotExistError()
elif path == "/":
return self.__root
else:
node = self.__root
current_path = path[1:]
while "/" in current_path:
index = current_path.find("/")
current_dir = current_path[:index]
current_path = current_path[index + 1:]
node = FileSystem.__find_node(current_dir, node)
return self.__find_node(current_path, node)
def create(self, path, directory=False, content=""):
parent_path = path[:path.rfind("/")]
try:
parent = self.get_node(parent_path if parent_path != "" else "/")
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
if parent.is_directory:
file_size = (len(content) if not directory else 0) + 1
- if self.__available_size < file_size:
- raise NotEnoughSpaceError
new_file_name = path[path.rfind("/") + 1:]
if new_file_name in parent.nodes:
- raise Exception
+ raise DestinationNodeExistsError
+
+ if self.available_size < file_size:
+ raise NotEnoughSpaceError
+
try:
self.__find_node(new_file_name, parent)
except NodeDoesNotExistError:
if directory:
parent.add_directory(new_file_name)
else:
parent.add_file(new_file_name, content)
- self.__available_size -= file_size # check if not negative
+ self.available_size -= file_size # check if not negative
else:
raise DestinationNodeExistsError
else:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
def remove(self, path, directory=False, force=True):
+ parent_path = FileSystem.__get_parent(path)
+ parent = self.get_node(parent_path)
node = self.get_node(FileSystem.__get_parent(path))
+ name = FileSystem.__get_filename(path)
+
+ if node.is_directory:
+ if directory:
+ try:
+ if self.__is_empty_dir(path):
+ self.available_size += node.size
+ parent.remove_dir(name)
+ else:
+ if force:
+ for key in node.nodes:
+ print(path)
+ self.remove(path + "/" + key,
+ directory=True)
+ else:
+ raise NonEmptyDirectoryDeletionError
+
+ except FileSystemError as e:
+ pass # the node is said to be a directory but it is a file
+ else:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ self.available_size += node.size
+ node.remove_file(name)
+
+
+ def remove_old(self, path, directory=False, force=True):
+ node = self.get_node(FileSystem.__get_parent(path))
name = FileSystem.__get_filename(node)
if not directory:
if not node.is_directory:
+ self.available_size += node.size
node.remove_file(name)
else:
pass # the node is said not to be directory but it is a
# directory
else:
try:
if self.__is_empty_dir(node):
pass # delete dir
else:
if force:
pass # delete recursively
else:
raise NonEmptyDirectoryDeletionError
except FileSystemError as e:
pass # the node is said to be a directory but it is a file
def move(self, source, destination):
pass
def link(self, source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pass
def unmount(self, path):
pass
@staticmethod
def __find_node(node_name, directory):
if node_name in directory.nodes:
return directory.nodes[node_name]
raise NodeDoesNotExistError
def __is_empty_dir(self, path):
node = self.get_node(path)
if node.is_directory:
return not node.nodes
else:
raise FileSystemError
@staticmethod
def __get_parent(path):
return path[:path.rfind("/")]
@staticmethod
def __get_filename(path):
- return path[path.rfing("/") + 1:]
+ return path[path.rfind("/") + 1:]
class Directory:
def __init__(self, name):
self.name = name
self.files = {}
self.directories = {}
self.nodes = {}
self.is_directory = True
+ self.size = 1
def add_directory(self, name):
- if self.is_available(name):
+ if self.is_available_name(name):
self.directories.update({name: Directory(name)})
self.__update_nodes()
else:
- raise Exception
+ raise DestinationNodeExistsError
def add_file(self, name, content):
- if self.is_available(name):
+ if self.is_available_name(name):
self.files.update({name: File(name, content)})
self.__update_nodes()
else:
- raise Exception
+ raise DestinationNodeExistsError
def remove_file(self, name):
self.files.pop(name, None)
+ self.__update_nodes()
+ def remove_dir(self, name):
+ self.directories.pop(name, None)
+ self.__update_nodes()
+
+ def is_empty(self):
+ return not self.nodes
+
def __update_nodes(self):
self.nodes.update(self.directories)
self.nodes.update(self.files)
- def is_available(self, name):
+ def is_available_name(self, name):
return name not in self.nodes.keys()
class File:
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.__set_content(self.content + text)
def truncate(self, text):
self.__set_content(text)
def __set_content(self, text):
self.content = text
self.__update_size()
def __update_size(self):
self.size = len(self.content) + 1

Любослав обнови решението на 30.04.2015 16:53 (преди над 9 години)

class FileSystemError(Exception):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class FileSystem:
def __init__(self, size_in_bytes):
if size_in_bytes < 1:
raise NotEnoughSpaceError
self.__SIZE_OF_DIR = 1
self.size = size_in_bytes
self.available_size = size_in_bytes - self.__SIZE_OF_DIR
self.__root = Directory("/")
def get_node(self, path):
if len(path) > 0 and path[0] is not "/":
raise NodeDoesNotExistError()
elif path == "/":
return self.__root
else:
node = self.__root
current_path = path[1:]
while "/" in current_path:
index = current_path.find("/")
current_dir = current_path[:index]
current_path = current_path[index + 1:]
node = FileSystem.__find_node(current_dir, node)
return self.__find_node(current_path, node)
def create(self, path, directory=False, content=""):
parent_path = path[:path.rfind("/")]
try:
parent = self.get_node(parent_path if parent_path != "" else "/")
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
if parent.is_directory:
file_size = (len(content) if not directory else 0) + 1
new_file_name = path[path.rfind("/") + 1:]
if new_file_name in parent.nodes:
raise DestinationNodeExistsError
if self.available_size < file_size:
raise NotEnoughSpaceError
try:
self.__find_node(new_file_name, parent)
except NodeDoesNotExistError:
if directory:
parent.add_directory(new_file_name)
else:
parent.add_file(new_file_name, content)
self.available_size -= file_size # check if not negative
else:
raise DestinationNodeExistsError
else:
raise DestinationNodeDoesNotExistError("The path to the file "
"does not exist.")
def remove(self, path, directory=False, force=True):
parent_path = FileSystem.__get_parent(path)
parent = self.get_node(parent_path)
node = self.get_node(FileSystem.__get_parent(path))
name = FileSystem.__get_filename(path)
if node.is_directory:
if directory:
try:
if self.__is_empty_dir(path):
self.available_size += node.size
parent.remove_dir(name)
else:
if force:
for key in node.nodes:
- print(path)
self.remove(path + "/" + key,
directory=True)
else:
raise NonEmptyDirectoryDeletionError
except FileSystemError as e:
pass # the node is said to be a directory but it is a file
else:
raise NonExplicitDirectoryDeletionError
else:
self.available_size += node.size
node.remove_file(name)
def remove_old(self, path, directory=False, force=True):
node = self.get_node(FileSystem.__get_parent(path))
name = FileSystem.__get_filename(node)
if not directory:
if not node.is_directory:
self.available_size += node.size
node.remove_file(name)
else:
pass # the node is said not to be directory but it is a
# directory
else:
try:
if self.__is_empty_dir(node):
pass # delete dir
else:
if force:
pass # delete recursively
else:
raise NonEmptyDirectoryDeletionError
except FileSystemError as e:
pass # the node is said to be a directory but it is a file
def move(self, source, destination):
pass
def link(self, source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pass
def unmount(self, path):
pass
@staticmethod
def __find_node(node_name, directory):
if node_name in directory.nodes:
return directory.nodes[node_name]
raise NodeDoesNotExistError
def __is_empty_dir(self, path):
node = self.get_node(path)
if node.is_directory:
return not node.nodes
else:
raise FileSystemError
@staticmethod
def __get_parent(path):
return path[:path.rfind("/")]
@staticmethod
def __get_filename(path):
return path[path.rfind("/") + 1:]
class Directory:
def __init__(self, name):
self.name = name
self.files = {}
self.directories = {}
self.nodes = {}
self.is_directory = True
self.size = 1
def add_directory(self, name):
if self.is_available_name(name):
self.directories.update({name: Directory(name)})
self.__update_nodes()
else:
raise DestinationNodeExistsError
def add_file(self, name, content):
if self.is_available_name(name):
self.files.update({name: File(name, content)})
self.__update_nodes()
else:
raise DestinationNodeExistsError
def remove_file(self, name):
self.files.pop(name, None)
self.__update_nodes()
def remove_dir(self, name):
self.directories.pop(name, None)
self.__update_nodes()
def is_empty(self):
return not self.nodes
def __update_nodes(self):
self.nodes.update(self.directories)
self.nodes.update(self.files)
def is_available_name(self, name):
return name not in self.nodes.keys()
class File:
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.__set_content(self.content + text)
def truncate(self, text):
self.__set_content(text)
def __set_content(self, text):
self.content = text
self.__update_size()
def __update_size(self):
self.size = len(self.content) + 1