Решение на In-memory файлова система от Божидар Григоров

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

Към профила на Божидар Григоров

Резултати

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

Код

class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class FileSystem:
def __init__(self, size):
self.root = Directory('/')
self.size = size
self.available_size = self.size - 1
def size(self):
return self.size
def available_size(self):
return self.available_size
def __validate_existence_of_dir(self, dirs):
size = len(dirs) - 1
if size is -1:
return True
root = self.root
for i in range(0, size):
current_dir = (r for r in root.directories if r.path == dirs[i])
current_dir = list(current_dir)
if len(current_dir) is 0:
return False
index = root.directories.index((current_dir)[0])
root = root.directories[index]
return True
def get_node(self, path):
dirs = path.split('/')
dirs = list(filter(None, dirs))
if self.__validate_existence_of_dir(dirs):
current = self.root
for i in range(0, len(dirs)):
temp = (x for x in current.nodes if x.path == dirs[i])
current = list(temp)[0]
return current
else:
raise NodeDoesNotExistError
def create(self, path, directory=False, content=''):
dirs = list(filter(None, path.split('/')))
if self.available_size - len(content) - 1 < 0:
raise NotEnoughSpaceError
elif not self.__validate_existence_of_dir(dirs):
raise DestinationNodeDoesNotExistError
elif directory is True:
if self.__validate_existence_of_dir(dirs):
root_dir = self.get_node('/'.join(dirs[0:len(dirs)-1]))
child_dir = Directory(dirs[-1])
root_dir.directories.append(child_dir)
self.available_size -= 1
else:
if self.__validate_existence_of_dir(dirs):
root_dir = self.get_node('/'.join(dirs[0:len(dirs) - 1]))
child_file = File(dirs[-1], content)
root_dir.files.append(child_file)
self.available_size -= len(content) + 1
else:
pass
# TODO error
def remove(self, path, directory=False, force=True):
dirs = path.split('/')
dirs = list(filter(None, dirs))
# TODO validate path existing
item = self.get_node(path)
if not item.is_directory:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.files.index(item)
parent.files.pop(index)
self.size -= len(item.content()) + 1
elif directory is False:
raise NonExplicitDirectoryDeletionError
pass
elif directory is True and len(item.nodes) is 0:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.directories.index(item)
parent.directories.pop(index)
self.available_size += 1
elif (directory is True or item.is_directory) and force is False:
raise NonEmptyDirectoryDeletionError
pass
elif directory is True and force is True:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.directories.index(item)
parent.directories.pop(index)
self.available_size += 1
return
def move(self, source, destination):
return
def link(self, source, destination, symbolic=True):
return
def mount(self, file_system, path):
return
def unmount(self, path):
return

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

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

======================================================================
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_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 36.133s

FAILED (errors=18)

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

Божидар обнови решението на 30.04.2015 16:51 (преди над 9 години)

+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class FileSystem:
+ def __init__(self, size):
+ self.root = Directory('/')
+ self.size = size
+ self.available_size = self.size - 1
+
+ def size(self):
+ return self.size
+
+ def available_size(self):
+ return self.available_size
+
+ def __validate_existence_of_dir(self, dirs):
+ size = len(dirs) - 1
+ if size is -1:
+ return True
+ root = self.root
+ for i in range(0, size):
+ current_dir = (r for r in root.directories if r.path == dirs[i])
+ current_dir = list(current_dir)
+ if len(current_dir) is 0:
+ return False
+ index = root.directories.index((current_dir)[0])
+ root = root.directories[index]
+ return True
+
+ def get_node(self, path):
+ dirs = path.split('/')
+ dirs = list(filter(None, dirs))
+ if self.__validate_existence_of_dir(dirs):
+ current = self.root
+ for i in range(0, len(dirs)):
+ temp = (x for x in current.nodes if x.path == dirs[i])
+ current = list(temp)[0]
+ return current
+ else:
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=''):
+ dirs = list(filter(None, path.split('/')))
+ if self.available_size - len(content) - 1 < 0:
+ raise NotEnoughSpaceError
+ elif not self.__validate_existence_of_dir(dirs):
+ raise DestinationNodeDoesNotExistError
+ elif directory is True:
+ if self.__validate_existence_of_dir(dirs):
+ root_dir = self.get_node('/'.join(dirs[0:len(dirs)-1]))
+ child_dir = Directory(dirs[-1])
+ root_dir.directories.append(child_dir)
+ self.available_size -= 1
+ else:
+ if self.__validate_existence_of_dir(dirs):
+ root_dir = self.get_node('/'.join(dirs[0:len(dirs) - 1]))
+ child_file = File(dirs[-1], content)
+ root_dir.files.append(child_file)
+ self.available_size -= len(content) + 1
+ else:
+ pass
+ # TODO error
+
+ def remove(self, path, directory=False, force=True):
+ dirs = path.split('/')
+ dirs = list(filter(None, dirs))
+ # TODO validate path existing
+ item = self.get_node(path)
+ if not item.is_directory:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.files.index(item)
+ parent.files.pop(index)
+ self.size -= len(item.content()) + 1
+ elif directory is False:
+ # NonExplicitDirectoryDeletionError
+ pass
+ elif directory is True and len(item.nodes) is 0:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.directories.index(item)
+ parent.directories.pop(index)
+ self.available_size += 1
+ elif (directory is True or item.is_directory) and force is False:
+ # NonEmptyDirectoryDeletionError
+ pass
+ elif directory is True and force is True:
+ parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
+ index = parent.directories.index(item)
+ parent.directories.pop(index)
+ self.available_size += 1
+ return
+
+ def move(self, source, destination):
+ return
+
+ def link(self, source, destination, symbolic=True):
+ return
+
+ def mount(self, file_system, path):
+ return
+
+ def unmount(self, path):
+ return
+
+
+class File:
+ def __init__(self, path, content=''):
+ self.path = path
+ self.size = len(content) + 1
+ self.content = content
+ self.is_directory = False
+
+ def content(self):
+ return self.content
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+ def size(self):
+ return self.size
+
+
+class Directory:
+ def __init__(self, path):
+ self.path = path
+ self.is_directory = True
+ self.directories = []
+ self.files = []
+
+ def directories(self):
+ return self.directories
+
+ def files(self):
+ return self.files
+
+ @property
+ def nodes(self):
+ return self.directories + self.files
+
+ def __iter__(self):
+ return iter(self.nodes)

Божидар обнови решението на 30.04.2015 16:52 (преди над 9 години)

class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class FileSystemMountError(FileSystemError):
pass
class MountPointDoesNotExistError(FileSystemMountError):
pass
class MountPointNotADirectoryError(FileSystemMountError):
pass
class MountPointNotEmptyError(FileSystemMountError):
pass
class NotEnoughSpaceError(FileSystemError):
pass
class DestinationNodeExistsError(FileSystemError):
pass
class FileSystem:
def __init__(self, size):
self.root = Directory('/')
self.size = size
self.available_size = self.size - 1
def size(self):
return self.size
def available_size(self):
return self.available_size
def __validate_existence_of_dir(self, dirs):
size = len(dirs) - 1
if size is -1:
return True
root = self.root
for i in range(0, size):
current_dir = (r for r in root.directories if r.path == dirs[i])
current_dir = list(current_dir)
if len(current_dir) is 0:
return False
index = root.directories.index((current_dir)[0])
root = root.directories[index]
return True
def get_node(self, path):
dirs = path.split('/')
dirs = list(filter(None, dirs))
if self.__validate_existence_of_dir(dirs):
current = self.root
for i in range(0, len(dirs)):
temp = (x for x in current.nodes if x.path == dirs[i])
current = list(temp)[0]
return current
else:
raise NodeDoesNotExistError
def create(self, path, directory=False, content=''):
dirs = list(filter(None, path.split('/')))
if self.available_size - len(content) - 1 < 0:
raise NotEnoughSpaceError
elif not self.__validate_existence_of_dir(dirs):
raise DestinationNodeDoesNotExistError
elif directory is True:
if self.__validate_existence_of_dir(dirs):
root_dir = self.get_node('/'.join(dirs[0:len(dirs)-1]))
child_dir = Directory(dirs[-1])
root_dir.directories.append(child_dir)
self.available_size -= 1
else:
if self.__validate_existence_of_dir(dirs):
root_dir = self.get_node('/'.join(dirs[0:len(dirs) - 1]))
child_file = File(dirs[-1], content)
root_dir.files.append(child_file)
self.available_size -= len(content) + 1
else:
pass
# TODO error
def remove(self, path, directory=False, force=True):
dirs = path.split('/')
dirs = list(filter(None, dirs))
# TODO validate path existing
item = self.get_node(path)
if not item.is_directory:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.files.index(item)
parent.files.pop(index)
self.size -= len(item.content()) + 1
elif directory is False:
- # NonExplicitDirectoryDeletionError
+ raise NonExplicitDirectoryDeletionError
pass
elif directory is True and len(item.nodes) is 0:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.directories.index(item)
parent.directories.pop(index)
self.available_size += 1
elif (directory is True or item.is_directory) and force is False:
- # NonEmptyDirectoryDeletionError
+ raise NonEmptyDirectoryDeletionError
pass
elif directory is True and force is True:
parent = self.get_node('/'.join(dirs[:len(dirs)-1]))
index = parent.directories.index(item)
parent.directories.pop(index)
self.available_size += 1
return
def move(self, source, destination):
return
def link(self, source, destination, symbolic=True):
return
def mount(self, file_system, path):
return
def unmount(self, path):
- return
-
+ return
-
-class File:
- def __init__(self, path, content=''):
- self.path = path
- self.size = len(content) + 1
- self.content = content
- self.is_directory = False
-
- def content(self):
- return self.content
-
- def append(self, text):
- self.content += text
- self.size += len(text)
-
- def truncate(self, text):
- self.content = text
- self.size = len(text) + 1
-
- def size(self):
- return self.size
-
-
-class Directory:
- def __init__(self, path):
- self.path = path
- self.is_directory = True
- self.directories = []
- self.files = []
-
- def directories(self):
- return self.directories
-
- def files(self):
- return self.files
-
- @property
- def nodes(self):
- return self.directories + self.files
-
- def __iter__(self):
- return iter(self.nodes)