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

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

Към профила на Михаела Сейменска

Резултати

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

Код

import re
class FileSystem:
def __init__(self, sys_size):
self.size = sys_size
self.available_size = sys_size - 1
self.nodes = [['/', Directory('/')]]
def _search_node(self, path):
for node in self.nodes:
if node[0] == path:
return node[1]
def get_node(self, path):
node = self._search_node(path)
if node:
return node
else:
raise NodeDoesNotExistError
def create(self, path, directory=False, content=''):
name_ = path.split('/')[-1]
dir_ = '/'.join(path.split('/')[:-1])
if dir_ == '':
dir_ = '/'
parent_dir = self._search_node(dir_)
if not parent_dir:
raise DestinationNodeDoesNotExistError
if directory and self.available_size < 1:
raise NotEnoughSpaceError
if not directory and self.available_size < len(content) + 1:
raise NotEnoughSpaceError
if self._search_node(path):
raise DestinationNodeExistsError
if directory:
add_ = Directory(name_)
self.nodes.append([path, add_])
parent_dir.directories.append(add_)
self.available_size -= add_.size
else:
add_ = File(self, name_, content)
self.nodes.append([path, add_])
parent_dir.files.append(add_)
self.available_size -= add_.size
def _remove_node(self, pattern):
nodes_ = self.nodes
pattern_obj = re.compile(pattern)
for node in nodes_:
matched = pattern_obj.match(node[0])
if matched:
self.available_size += node[1].size
self.nodes.remove([matched.string, node[1]])
def remove(self, path, directory=False, force=True):
node = self._search_node(path)
if not node:
raise NodeDoesNotExistError
if node.is_directory and directory == False:
raise NonExplicitDirectoryDeletionError
if node.is_directory and directory and \
node.nodes is not [] and force == False:
raise NonEmptyDirectoryDeletionError
if node.is_directory:
self._remove_node(path)
else:
self.available_size += node.size
self.nodes.remove([path, node])
def _move_node(self, pattern, replacement):
nodes = self.nodes
pattern_ = re.compile(pattern)
for dir_, node in nodes_items:
if replacement == dir_:
continue
directory_ = pattern_.sub(replacement, dir_)
self.nodes.append([directory_, node])
self.nodes.remove([dir_, node])
def move(self, source, destination):
node_source = self._search_node(source)
if not node_source:
raise SourceNodeDoesNotExist
node_destination = self._search_node(destination)
if not node_destination:
raise DestinationNodeDoesNotExistError
if node_destination and not node_destination.is_directory:
raise DestinationNotADirectoryError
if node_destination.is_directory and node_source in node_destination.nodes:
raise DestinationNodeExistsError
node_path = destination + '/' + node_source.name
if node_source.is_directory:
if re.match(source, destination):
if source == destination:
raise DestinationNodeExistsError
else:
raise UnableToMoveError
self.create(node_path, True)
self.nodes.append([node_path, node_source])
self._move_node(source, node_path)
else:
rem_node = [source, node_source]
self.nodes.remove([rem_node[0], rem_node[1]])
self.create(node_path, False, rem_node[1].content)
def link(source, destination, symbolic=True):
node_source = self._search_node(source)
if not node_source and symbolic:
raise NodeDoesNotExistError
if node_source.is_directory and not symbolic:
raise DircetoryHardLinkError
if not node_source:
raise SourceNodeDoesNotExist
def mount(file_system, path):
node_path = self._search_node(path)
if node_path.nodes:
raise MountPointNotEmptyError
if not node_path.is_directory:
raise MountPointNotADirectoryError
if not node_path:
MountPointDoesNotExistError
def unmount(path):
node_path = self._search_node(path)
if not node_path:
raise NodeDoesNotExistError
class File:
def __init__(self, file_system, file_name, file_content=''):
self.system = file_system
self.name = file_name
self.content = file_content
self.size = len(self.content) + 1
self.is_directory = False
def append(self, text):
size_ = len(text)
if self.system.available_size < size_:
raise NotEnoughSpaceError
self.content += text
self.size += size_
self.system.available_size -= size_
def truncate(self, text):
size_ = len(text)
if self.system.available_size + self.size < size_:
raise NotEnoughSpaceError
self.content = text
self.file_system.available_size += self.size
self.size = size_
self.file_system.available_size -= self.size
class Directory:
def __init__(self, dir_name):
self.name = dir_name
self.size = 1
self.directories = []
self.files = []
self.nodes = []
self.is_directory = True
class FileSystemError(Exception):
def __init__(self):
self.message = 'File System Error!'
def __str__(self):
return self.message
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Not Enough Space Error!'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Destination Node Exists Error!'
class DestinationNodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Destination Node Does Not Exist Error!'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
super(),__init__()
self.message = 'Destination Not A Directory Error!'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Non Explicit Directory Deletion Error!'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Non Empty Directory Deletion Error!'
class FileSystemMountError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'File System Mount Error!'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Node does not exist!'
class UnableToMoveError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Unable To Move Error!'
class DircetoryHardLinkError(FileSystemError):
def __init__(self):
super().__init__()
self.message = 'Directory Hard Link Error!'
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = 'Mount Point Does Not Exist Error!'
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = 'Mount Point Not A Directory Error!'
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
super().__init__()
self.message = 'Mount Point Not Empty Error'
class SourceNodeDoesNotExist(NodeDoesNotExistError):
def __init__(self):
super().__init__()
self.message = 'Source Node Does Not Exist!'

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

..EEEEE.EEE..E.EEE
======================================================================
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_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 24.164s

FAILED (errors=12)

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

Михаела обнови решението на 30.04.2015 16:43 (преди над 9 години)

+import re
+
+
+class FileSystem:
+
+ def __init__(self, sys_size):
+ self.size = sys_size
+ self.available_size = sys_size - 1
+ self.nodes = [['/', Directory('/')]]
+
+ def _search_node(self, path):
+ for node in self.nodes:
+ if node[0] == path:
+ return node[1]
+
+ def get_node(self, path):
+ node = self._search_node(path)
+ if node:
+ return node
+ else:
+ raise NodeDoesNotExistError
+
+ def create(self, path, directory=False, content=''):
+ name_ = path.split('/')[-1]
+ dir_ = '/'.join(path.split('/')[:-1])
+ if dir_ == '':
+ dir_ = '/'
+ parent_dir = self._search_node(dir_)
+ if not parent_dir:
+ raise DestinationNodeDoesNotExistError
+ if directory and self.available_size < 1:
+ raise NotEnoughSpaceError
+ if not directory and self.available_size < len(content) + 1:
+ raise NotEnoughSpaceError
+ if self._search_node(path):
+ raise DestinationNodeExistsError
+ if directory:
+ add_ = Directory(name_)
+ self.nodes.append([path, add_])
+ parent_dir.directories.append(add_)
+ self.available_size -= add_.size
+ else:
+ add_ = File(self, name_, content)
+ self.nodes.append([path, add_])
+ parent_dir.files.append(add_)
+ self.available_size -= add_.size
+
+ def _remove_node(self, pattern):
+ nodes_ = self.nodes
+ pattern_obj = re.compile(pattern)
+ for node in nodes_:
+ matched = pattern_obj.match(node[0])
+ if matched:
+ self.available_size += node[1].size
+ self.nodes.remove([matched.string, node[1]])
+
+ def remove(self, path, directory=False, force=True):
+ node = self._search_node(path)
+ if not node:
+ raise NodeDoesNotExistError
+ if node.is_directory and directory == False:
+ raise NonExplicitDirectoryDeletionError
+ if node.is_directory and directory and \
+ node.nodes is not [] and force == False:
+ raise NonEmptyDirectoryDeletionError
+ if node.is_directory:
+ self._remove_node(path)
+ else:
+ self.available_size += node.size
+ self.nodes.remove([path, node])
+
+ def _move_node(self, pattern, replacement):
+ nodes = self.nodes
+ pattern_ = re.compile(pattern)
+ for dir_, node in nodes_items:
+ if replacement == dir_:
+ continue
+ directory_ = pattern_.sub(replacement, dir_)
+ self.nodes.append([directory_, node])
+ self.nodes.remove([dir_, node])
+
+ def move(self, source, destination):
+ node_source = self._search_node(source)
+ if not node_source:
+ raise SourceNodeDoesNotExist
+ node_destination = self._search_node(destination)
+ if not node_destination:
+ raise DestinationNodeDoesNotExistError
+ if node_destination and not node_destination.is_directory:
+ raise DestinationNotADirectoryError
+ if node_destination.is_directory and node_source in node_destination.nodes:
+ raise DestinationNodeExistsError
+ node_path = destination + '/' + node_source.name
+ if node_source.is_directory:
+ if re.match(source, destination):
+ if source == destination:
+ raise DestinationNodeExistsError
+ else:
+ raise UnableToMoveError
+ self.create(node_path, True)
+ self.nodes.append([node_path, node_source])
+ self._move_node(source, node_path)
+ else:
+ rem_node = [source, node_source]
+ self.nodes.remove([rem_node[0], rem_node[1]])
+ self.create(node_path, False, rem_node[1].content)
+
+ def link(source, destination, symbolic=True):
+ node_source = self._search_node(source)
+ if not node_source and symbolic:
+ raise NodeDoesNotExistError
+ if node_source.is_directory and not symbolic:
+ raise DircetoryHardLinkError
+ if not node_source:
+ raise SourceNodeDoesNotExist
+
+ def mount(file_system, path):
+ node_path = self._search_node(path)
+ if node_path.nodes:
+ raise MountPointNotEmptyError
+ if not node_path.is_directory:
+ raise MountPointNotADirectoryError
+ if not node_path:
+ MountPointDoesNotExistError
+
+ def unmount(path):
+ node_path = self._search_node(path)
+ if not node_path:
+ raise NodeDoesNotExistError
+
+
+
+
+class File:
+
+ def __init__(self, file_system, file_name, file_content=''):
+ self.system = file_system
+ self.name = file_name
+ self.content = file_content
+ self.size = len(self.content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ size_ = len(text)
+ if self.system.available_size < size_:
+ raise NotEnoughSpaceError
+ self.content += text
+ self.size += size_
+ self.system.available_size -= size_
+
+ def truncate(self, text):
+ size_ = len(text)
+ if self.system.available_size + self.size < size_:
+ raise NotEnoughSpaceError
+ self.content = text
+ self.file_system.available_size += self.size
+ self.size = size_
+ self.file_system.available_size -= self.size
+
+
+class Directory:
+
+ def __init__(self, dir_name):
+ self.name = dir_name
+ self.size = 1
+ self.directories = []
+ self.files = []
+ self.nodes = []
+ self.is_directory = True
+
+
+class FileSystemError(Exception):
+
+ def __init__(self):
+ self.message = 'File System Error!'
+
+ def __str__(self):
+ return self.message
+
+
+class NotEnoughSpaceError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Not Enough Space Error!'
+
+
+class DestinationNodeExistsError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Destination Node Exists Error!'
+
+
+class DestinationNodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Destination Node Does Not Exist Error!'
+
+class DestinationNotADirectoryError(FileSystemError):
+
+ def __init__(self):
+ super(),__init__()
+ self.message = 'Destination Not A Directory Error!'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Non Explicit Directory Deletion Error!'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Non Empty Directory Deletion Error!'
+
+
+class FileSystemMountError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'File System Mount Error!'
+
+
+class NodeDoesNotExistError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Node does not exist!'
+
+
+class UnableToMoveError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Unable To Move Error!'
+
+class DircetoryHardLinkError(FileSystemError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Directory Hard Link Error!'
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Mount Point Does Not Exist Error!'
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Mount Point Not A Directory Error!'
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Mount Point Not Empty Error'
+
+
+class SourceNodeDoesNotExist(NodeDoesNotExistError):
+
+ def __init__(self):
+ super().__init__()
+ self.message = 'Source Node Does Not Exist!'