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

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

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

Резултати

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

Код

class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DirectoryDeletionError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(DirectoryDeletionError):
pass
class NonEmptyDirectoryDeletionError(DirectoryDeletionError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class File():
def __init__(self, path, content):
self.path = path
self.content = content
self.is_directory = False
self.size = len(content) + 1
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
class Directory():
def __init__(self, path):
self.path = path
self.is_directory = True
self.size = 1
self.directories = []
self.files = []
class FileSystem():
def __init__(self, full_size):
self.full_size = full_size
self.available_size = full_size
root = Directory('/')
self.nodes = [root]
self.available_size -= root.size
def get_node(self, path):
try:
exists = False
for item in self.nodes:
if item.path == path:
exists = True
node = item
if exists:
return node
else:
raise NodeDoesNotExistError
except NodeDoesNotExistError:
print ('File/Directory Does Not Exist')
def get_actual_dir(self, path):
dirs = path.split('/')
dirs.pop(-1)
actual_dir = '/'.join(dirs)
return actual_dir or '/'
def get_file_or_folder_name(self, path):
if '.' in path:
return path.split('/')[-1]
return '/' + path.split('/')[-1]
def create(self, path, directory=False, content=''):
try:
actual_dir = self.get_actual_dir(path)
dir_exists = False
for item in self.nodes:
if item.path == path:
raise DestinationNodeExistsError
break
if item.path == actual_dir:
dir_exists = True
if dir_exists is False:
raise DestinationNodeDoesNotExistError
if ((directory is False and len(content) + 1 > self.available_size)
or (directory is True and self.available_size < 1)):
raise NotEnoughSpaceError
if directory is False:
new_file = File(path, content=content)
self.nodes.append(new_file)
self.available_size -= new_file.size
current_dir = self.get_node(actual_dir)
# current_dir.files.append(self.get_file_or_folder_name(new_file.path).replace('/', ''))
current_dir.files.append(new_file)
else:
new_dir = Directory(path)
self.nodes.append(new_dir)
self.available_size -= new_dir.size
current_dir = self.get_node(actual_dir)
# current_dir.directories.append(self.get_file_or_folder_name(new_dir.path))
current_dir.directories.append(new_dir)
except DestinationNodeExistsError:
print('File/Directory Already Exists')
except DestinationNodeDoesNotExistError:
print('File/Directory Does Not Exist')
except NotEnoughSpaceError:
print('Not Enough Space')
def remove(self, path, directory=False, force=False):
to_delete = self.get_node(path)
try:
if to_delete:
if isinstance(to_delete, File):
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).files.remove(to_delete)
elif isinstance(to_delete, Directory) and directory is True:
if not to_delete.directories and not to_delete.files:
if force is True:
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
else:
raise NonEmptyDirectoryDeletionError
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
else:
raise NonExplicitDirectoryDeletionError
except NonExplicitDirectoryDeletionError:
print('Non Explicit Directory Deletion')
except NonEmptyDirectoryDeletionError:
print('Directory Is Not Empty')
def move(self, source, destination):
try:
src = self.get_node(source)
dest = self.get_node(destination)
if not src:
raise SourceNodeDoesNotExistError
if not dest:
raise DestinationNodeDoesNotExistError
if not isinstance(dest, Directory):
raise DestinationNotADirectoryError
if (self.get_file_or_folder_name(source) in dest.directories or
self.get_file_or_folder_name(source) in dest.files):
raise DestinationNodeExistsError
if isinstance(src, File):
self.get_node(self.get_actual_dir(src.path)).files.remove(src)
self.get_node(self.get_actual_dir(dest.path)).files.append(src)
else:
self.get_node(self.get_actual_dir(src.path)).directories.remove(src)
self.get_node(self.get_actual_dir(dest.path)).directories.append(src)
except SourceNodeDoesNotExistError:
print('Source Does Not Exist')
except DestinationNodeDoesNotExistError:
print('Destination Does Not Exist')
except DestinationNotADirectoryError:
print('Destination Is Not A Directory')
except DestinationNodeExistsError:
print('Destination Already Exists')

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

..EEEEEEEEEEEEEEEE
======================================================================
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_minimal (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_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 32.145s

FAILED (errors=16)

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

Светлин обнови решението на 30.04.2015 16:30 (преди почти 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(DirectoryDeletionError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(DirectoryDeletionError):
+ pass
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class File():
+ def __init__(self, path, content):
+ self.path = path
+ self.content = content
+ self.is_directory = False
+ self.size = len(content) + 1
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+
+class Directory():
+ def __init__(self, path):
+ self.path = path
+ self.is_directory = True
+ self.size = 1
+ self.directories = []
+ self.files = []
+
+
+class FileSystem():
+ def __init__(self, full_size):
+ self.full_size = full_size
+ self.available_size = full_size
+ root = Directory('/')
+ self.nodes = [root]
+ self.available_size -= root.size
+
+ def get_node(self, path):
+ try:
+ exists = False
+ for item in self.nodes:
+ if item.path == path:
+ exists = True
+ node = item
+ if exists:
+ return node
+ else:
+ raise NodeDoesNotExistError
+ except NodeDoesNotExistError:
+ print ('File/Directory Does Not Exist')
+
+ def get_actual_dir(self, path):
+ dirs = path.split('/')
+ dirs.pop(-1)
+ actual_dir = '/'.join(dirs)
+ return actual_dir or '/'
+
+ def get_file_or_folder_name(self, path):
+ if '.' in path:
+ return path.split('/')[-1]
+ return '/' + path.split('/')[-1]
+
+
+ def create(self, path, directory=False, content=''):
+ try:
+ actual_dir = self.get_actual_dir(path)
+ dir_exists = False
+ for item in self.nodes:
+ if item.path == path:
+ raise DestinationNodeExistsError
+ break
+ if item.path == actual_dir:
+ dir_exists = True
+ if dir_exists is False:
+ raise DestinationNodeDoesNotExistError
+ if ((directory is False and len(content) + 1 > self.available_size)
+ or (directory is True and self.available_size < 1)):
+ raise NotEnoughSpaceError
+ if directory is False:
+ new_file = File(path, content=content)
+ self.nodes.append(new_file)
+ self.available_size -= new_file.size
+ current_dir = self.get_node(actual_dir)
+ # current_dir.files.append(self.get_file_or_folder_name(new_file.path).replace('/', ''))
+ current_dir.files.append(new_file)
+ else:
+ new_dir = Directory(path)
+ self.nodes.append(new_dir)
+ self.available_size -= new_dir.size
+ current_dir = self.get_node(actual_dir)
+ # current_dir.directories.append(self.get_file_or_folder_name(new_dir.path))
+ current_dir.directories.append(new_dir)
+
+ except DestinationNodeExistsError:
+ print('File/Directory Already Exists')
+ except DestinationNodeDoesNotExistError:
+ print('File/Directory Does Not Exist')
+ except NotEnoughSpaceError:
+ print('Not Enough Space')
+
+ def remove(self, path, directory=False, force=False):
+ to_delete = self.get_node(path)
+ try:
+ if to_delete:
+ if isinstance(to_delete, File):
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).files.remove(to_delete)
+ elif isinstance(to_delete, Directory) and directory is True:
+ if not to_delete.directories and not to_delete.files:
+ if force is True:
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
+ else:
+ raise NonEmptyDirectoryDeletionError
+ self.nodes.remove(to_delete)
+ self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
+ else:
+ raise NonExplicitDirectoryDeletionError
+
+ except NonExplicitDirectoryDeletionError:
+ print('Non Explicit Directory Deletion')
+ except NonEmptyDirectoryDeletionError:
+ print('Directory Is Not Empty')
+
+
+ def move(self, source, destination):
+ try:
+ src = self.get_node(source)
+ dest = self.get_node(destination)
+ if not src:
+ raise SourceNodeDoesNotExistError
+ if not dest:
+ raise DestinationNodeDoesNotExistError
+ if not isinstance(dest, Directory):
+ raise DestinationNotADirectoryError
+ if (self.get_file_or_folder_name(source) in dest.directories or
+ self.get_file_or_folder_name(source) in dest.files):
+ raise DestinationNodeExistsError
+
+ except SourceNodeDoesNotExistError:
+ print('Source Does Not Exist')
+ except DestinationNodeDoesNotExistError:
+ print('Destination Does Not Exist')
+ except DestinationNotADirectoryError:
+ print('Destination Is Not A Directory')
+ except DestinationNodeExistsError:
+ print('Destination Already Exists')

Светлин обнови решението на 30.04.2015 16:59 (преди почти 9 години)

class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DirectoryDeletionError(FileSystemError):
pass
class NonExplicitDirectoryDeletionError(DirectoryDeletionError):
pass
class NonEmptyDirectoryDeletionError(DirectoryDeletionError):
pass
class DestinationNotADirectoryError(FileSystemError):
pass
class File():
def __init__(self, path, content):
self.path = path
self.content = content
self.is_directory = False
self.size = len(content) + 1
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
class Directory():
def __init__(self, path):
self.path = path
self.is_directory = True
self.size = 1
self.directories = []
self.files = []
class FileSystem():
def __init__(self, full_size):
self.full_size = full_size
self.available_size = full_size
root = Directory('/')
self.nodes = [root]
self.available_size -= root.size
def get_node(self, path):
try:
exists = False
for item in self.nodes:
if item.path == path:
exists = True
node = item
if exists:
return node
else:
raise NodeDoesNotExistError
except NodeDoesNotExistError:
print ('File/Directory Does Not Exist')
def get_actual_dir(self, path):
dirs = path.split('/')
dirs.pop(-1)
actual_dir = '/'.join(dirs)
return actual_dir or '/'
def get_file_or_folder_name(self, path):
if '.' in path:
return path.split('/')[-1]
return '/' + path.split('/')[-1]
def create(self, path, directory=False, content=''):
try:
actual_dir = self.get_actual_dir(path)
dir_exists = False
for item in self.nodes:
if item.path == path:
raise DestinationNodeExistsError
break
if item.path == actual_dir:
dir_exists = True
if dir_exists is False:
raise DestinationNodeDoesNotExistError
if ((directory is False and len(content) + 1 > self.available_size)
or (directory is True and self.available_size < 1)):
raise NotEnoughSpaceError
if directory is False:
new_file = File(path, content=content)
self.nodes.append(new_file)
self.available_size -= new_file.size
current_dir = self.get_node(actual_dir)
# current_dir.files.append(self.get_file_or_folder_name(new_file.path).replace('/', ''))
current_dir.files.append(new_file)
else:
new_dir = Directory(path)
self.nodes.append(new_dir)
self.available_size -= new_dir.size
current_dir = self.get_node(actual_dir)
# current_dir.directories.append(self.get_file_or_folder_name(new_dir.path))
current_dir.directories.append(new_dir)
except DestinationNodeExistsError:
print('File/Directory Already Exists')
except DestinationNodeDoesNotExistError:
print('File/Directory Does Not Exist')
except NotEnoughSpaceError:
print('Not Enough Space')
def remove(self, path, directory=False, force=False):
to_delete = self.get_node(path)
try:
if to_delete:
if isinstance(to_delete, File):
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).files.remove(to_delete)
elif isinstance(to_delete, Directory) and directory is True:
if not to_delete.directories and not to_delete.files:
if force is True:
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
else:
raise NonEmptyDirectoryDeletionError
self.nodes.remove(to_delete)
self.get_node(self.get_actual_dir(to_delete.path)).directories.remove(to_delete)
else:
raise NonExplicitDirectoryDeletionError
except NonExplicitDirectoryDeletionError:
print('Non Explicit Directory Deletion')
except NonEmptyDirectoryDeletionError:
print('Directory Is Not Empty')
def move(self, source, destination):
try:
src = self.get_node(source)
dest = self.get_node(destination)
if not src:
raise SourceNodeDoesNotExistError
if not dest:
raise DestinationNodeDoesNotExistError
if not isinstance(dest, Directory):
raise DestinationNotADirectoryError
if (self.get_file_or_folder_name(source) in dest.directories or
self.get_file_or_folder_name(source) in dest.files):
raise DestinationNodeExistsError
+ if isinstance(src, File):
+ self.get_node(self.get_actual_dir(src.path)).files.remove(src)
+ self.get_node(self.get_actual_dir(dest.path)).files.append(src)
+ else:
+ self.get_node(self.get_actual_dir(src.path)).directories.remove(src)
+ self.get_node(self.get_actual_dir(dest.path)).directories.append(src)
+
except SourceNodeDoesNotExistError:
print('Source Does Not Exist')
except DestinationNodeDoesNotExistError:
print('Destination Does Not Exist')
except DestinationNotADirectoryError:
print('Destination Is Not A Directory')
except DestinationNodeExistsError:
print('Destination Already Exists')