Решение на In-memory файлова система от Стоян Иванов

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

Към профила на Стоян Иванов

Резултати

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

Код

class File:
def __init__(self, content='', name=''):
self.__is_directory = False
self.__content = content
self.name = name
def append(self, text):
self.__content += text
def truncate(self, text):
self.__content = text
def size(self):
return len(self.__content) + 1
@property
def content(self):
return self.__content
@property
def is_directory(self):
return self.__is_directory
class Directory:
def __init__(self, directory=[], files=[], name=''):
self.directory = directory
self.files = files
self.nodes = directory + files
self.is_directory = True
self.name = name
def refresh_directory(self):
result = []
for i in self.nodes:
if i.is_directory:
result.append(i)
self.directory = result
def refresh_files(self):
result = []
for i in self.nodes:
if not i.is_directory:
result.append(i)
self.files = result
class FileSystemError(SystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class NonEmptyDirectoryDeletionError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(FileSystemError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class FileSystem:
def __init__(self, bytes):
self.__size = bytes
self.__available_size = bytes-1
self.__root = Directory()
@property
def size(self):
return self.__size
@property
def available_size(self):
return self.__available_size
def get_node(self, path):
path = path.split('/')[1:]
if len(path) == 1:
return self.__root
for i in range(0, len(self.__root.nodes)):
if path[1] == self.__root.nodes[i].name:
if len(path) == 2:
print (type(self.__root.nodes[i]))
return self.__root.nodes[i]
else:
return self.node_helper(path[2:], self.__root.nodes[i])
raise NodeDoesNotExistError
def node_helper(self, path, directory):
for i in range(0, len(directory.nodes)):
if path[0] == directory.nodes[i].name:
if len[path] == 1:
return directory.nodes[i]
else:
return self.node_helper(path[1:], directory.nodes[i])
raise NodeDoesNotExistError
def to_string(self, path):
result = ""
for i in path:
result += i + '/'
return result
def create(self, path, directory=False, content=''):
try:
previos_path = path.split('/')
get_directory = self.get_node(self.to_string(previos_path[:-1]))
if len(content) + 1 > self.__available_size:
raise NotEnoughSpaceError
else:
for i in range(0, len(get_directory.nodes)):
if path[-1] == get_directory.nodes[i].name:
raise DestinationNodeExistsError
if directory:
directories = Directory(name=path[-1])
get_directory.nodes.append(directories)
get_directory.directory.append(directories)
else:
file = File(content=content, name=path[-1])
get_directory.nodes.append(file)
get_directory.files.append(file)
except NodeDoesNotExistError:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
get_directory = self.get_node(path[:-1])
for i in range(0, len(get_directory.nodes)):
if path[:-1] == get_directory.nodes[i]. name:
regular_directory = get_directory.nodes[i]
if regular_directory.is_directory:
if len(regular_directory.nodes) > 0 and force:
get_directory.nodes.pop(i)
get_directory.refresh_directory()
elif not directory:
raise NonExplicitDirectoryDeletionError
else:
raise NonEmptyDirectoryDeletionError
else:
get_directory.nodes.pop(i)
get_directory.refresh_files()
raise NodeDoesNotExistError
def move(self, source, destination):
try:
source_node = self.get_node(source)
self.remove(source)
path = destination + source_node.name
destionation_node = self.get_node(source)
if not destionation_node.is_directory:
raise DestinationNotADirectoryError
if source_node.is_directory:
self.create(path, directory=True)
else:
self.create(path, directory=False, content=source_node.content)
except NodeDoesNotExistError:
raise SourceNodeDoesNotExistError
def link(self, source, destination, symbolic=True):
pass
def mount(self, file_system, path):
pass
def unmount(self, path):
pass

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

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

======================================================================
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_out_of_space (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 30.136s

FAILED (errors=15)

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

Стоян обнови решението на 30.04.2015 10:03 (преди над 9 години)

+class File:
+ def __init__(self, content='', name=''):
+ self.__is_directory = False
+ self.__content = content
+ self.name = name
+
+ def append(self, text):
+ self.__content += text
+
+ def truncate(self, text):
+ self.__content = text
+
+ def size(self):
+ return len(self.__content) + 1
+
+ @property
+ def content(self):
+ return self.__content
+
+ @property
+ def is_directory(self):
+ return self.__is_directory
+
+
+class Directory:
+ def __init__(self, directory=[], files=[], name=''):
+ self.directory = directory
+ self.files = files
+ self.nodes = directory + files
+ self.is_directory = True
+ self.name = name
+
+ def refresh_directory(self):
+ result = []
+ for i in self.nodes:
+ if i.is_directory:
+ result.append(i)
+ self.directory = result
+
+ def refresh_files(self):
+ result = []
+ for i in self.nodes:
+ if not i.is_directory:
+ result.append(i)
+ self.files = result
+
+
+class FileSystemError(SystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class FileSystem:
+ def __init__(self, bytes):
+ self.__size = bytes
+ self.__available_size = bytes-1
+ self.__root = Directory()
+
+ @property
+ def size(self):
+ return self.__size
+
+ @property
+ def available_size(self):
+ return self.__available_size
+
+ def get_node(self, path):
+ path = path.split('/')[1:]
+ if len(path) == 1:
+ return self.__root
+
+ for i in range(0, len(self.__root.nodes)):
+ if path[1] == self.__root.nodes[i].name:
+ if len(path) == 2:
+ print (type(self.__root.nodes[i]))
+ return self.__root.nodes[i]
+ else:
+ return self.node_helper(path[2:], self.__root.nodes[i])
+ raise NodeDoesNotExistError
+
+ def node_helper(self, path, directory):
+ for i in range(0, len(directory.nodes)):
+ if path[0] == directory.nodes[i].name:
+ if len[path] == 1:
+ return directory.nodes[i]
+ else:
+ return self.node_helper(path[1:], directory.nodes[i])
+ raise NodeDoesNotExistError
+
+ def to_string(self, path):
+ result = ""
+ for i in path:
+ result += i + '/'
+ return result
+
+ def create(self, path, directory=False, content=''):
+ try:
+ previos_path = path.split('/')
+ get_directory = self.get_node(self.to_string(previos_path[:-1]))
+ if len(content) + 1 > self.__available_size:
+ raise NotEnoughSpaceError
+ else:
+ for i in range(0, len(get_directory.nodes)):
+ if path[-1] == get_directory.nodes[i].name:
+ raise DestinationNodeExistsError
+ if directory:
+ directories = Directory(name=path[-1])
+ get_directory.nodes.append(directories)
+ get_directory.directory.append(directories)
+ else:
+ file = File(content=content, name=path[-1])
+ get_directory.nodes.append(file)
+ get_directory.files.append(file)
+
+ except NodeDoesNotExistError:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ get_directory = self.get_node(path[:-1])
+ for i in range(0, len(get_directory.nodes)):
+ if path[:-1] == get_directory.nodes[i]. name:
+ regular_directory = get_directory.nodes[i]
+ if regular_directory.is_directory:
+ if len(regular_directory.nodes) > 0 and force:
+ get_directory.nodes.pop(i)
+ get_directory.refresh_directory()
+ elif not directory:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ get_directory.nodes.pop(i)
+ get_directory.refresh_files()
+ raise NodeDoesNotExistError
+
+ def move(self, source, destination):
+ try:
+ source_node = self.get_node(source)
+ self.remove(source)
+ path = destination + source_node.name
+ destionation_node = self.get_node(source)
+ if not destionation_node.is_directory:
+ raise DestinationNotADirectoryError
+ if source_node.is_directory:
+ self.create(path, directory=True)
+ else:
+ self.create(path, directory=False, content=source_node.content)
+ except NodeDoesNotExistError:
+ raise SourceNodeDoesNotExistError
+
+ def link(self, source, destination, symbolic=True):
+ pass
+
+ def mount(self, file_system, path):
+ pass
+
+ def unmount(self, path):
+ pass