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

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

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

Резултати

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

Код

class FileSystemError(Exception):
def __init__(self):
self.message = 'File system error!'
def __str__(self):
return repr(self.message)
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'The node does not exist!'
def __str__(self):
return repr(self.message)
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not enough space in your file system!'
def __str__(self):
return repr(self.message)
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'The file system does not contain such source node!'
def __str__(self):
return repr(self.message)
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'The destination node does not exist!'
def __str__(self):
return repr(self.message)
class DestinationNodeExistsError(NodeDoesNotExistError):
def __init__(self):
self.message = 'The destination node already exist!'
def __str__(self):
return repr(self.message)
class FileSystemMountError(FileSystemError):
def __init__(self):
self.message = 'Mount error!'
def __str__(self):
return repr(self.message)
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mount error!'
def __str__(self):
return repr(self.message)
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mount error!'
def __str__(self):
return repr(self.message)
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mount error!'
def __str__(self):
return repr(self.message)
class DestinationNotADirectoryError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination is not a directory!'
def __str__(self):
return repr(self.message)
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
self.message = 'Cannot create hard link for a directory!!!'
def __str__(self):
return repr(self.message)
def getlast(path):
if '/' in path:
help_str = ''
for i in path[::-1]:
help_str = help_str + i
if i == '/': break
return help_str[::-1]
def split(path):
if path[0] == '/':
list_of_dir = []
tmp_str = ''
count = 0
for i in path[1::]:
count += 1
if not i == '/':
tmp_str = tmp_str + i
elif not '/' in path[count::]:
list_of_dir.append(tmp_str)
list_of_dir.append(getlast(tmp_str))
tmp_str = ''
else:
list_of_dir.append('/' + tmp_str)
tmp_str = tmp_str + '/'
list_of_dir.append('/' + tmp_str)
return list_of_dir
def getdir(path):
return path[:len(path) - len(getlast(path))]
def getfilename(path):
return getlast(path)[1::]
class File:
def __init__(self, content):
self.content = content
def append(self, text):
self.content = self.content + text
def truncate(self, text):
self.content = text
def size(self):
return len(self.content) + 1
def __getattr__(self, content):
return self.content
class Directory:
def __init__(self, path):
self.path = path
def __getattr__(self, path):
return path
def directories(self):
#list_of_dir = split(self.path)
list_of_subdir = split(self.path)
#for i in list_of_dir:
#if path in i:
#list_of_subdir.append(i)
return list_of_subdir
class FileSystem(File, Directory):
def __init__ (self, size):
self.size = size
self.available_size = size - 1
self.nodes = {'/': {}}
def __getattr__(self, size):
return self.size
def __getattr__(self, available_size):
return self.available_size
def __getattr__(self, nodes):
return self.nodes
def create (self, path, directory = False, content = ''):
if directory == False:
new_file = File(content)
if path.count('/') > 1:
file_name = getlast(path)
path = path[:len(path) - len(file_name)]
elif path.count('/') == 1:
file_name = path
path = '/'
if path in self.nodes and not file_name[1::] in self.nodes[path]:
self.nodes[path][file_name[1::]] = content
elif not path in self.nodes:
raise DestinationNodeDoesNotExistError
if self.available_size - len(new_file.content) < 1:
raise NotEnoughSpaceError
self.available_size -= len(new_file.content) + 1
return new_file
else:
if path in self.nodes:
raise DestinationNodeExistsError
elif getdir(path) in self.nodes:
new_directory = Directory(path)
#new_list_of_directories = split(path)
#for i in new_list_of_directories:
#self.catalog[i] = {}
if self.available_size - path.count('/') < 1:
raise NotEnoughSpaceError
self.nodes[path] = {}
self.available_size -= path.count('/')
return new_directory
elif path.count('/') == 1 and not path == '/':
new_directory = Directory(path)
self.nodes[path] = {}
if self.available_size - path.count('/') < 1:
raise NotEnoughSpaceError
self.available_size -= path.count('/')
return new_directory
elif getdir(path) in self.nodes and getlast(path)[1::] in self.nodes[getlast(path)]:
return DestinationNodeExistsError
def get_node(self, path):
if path in self.nodes:
new_dir = Directory(path)
return new_dir
elif path[:len(path) - len(getlast(path))] in self.nodes and getlast(path)[1::] in self.nodes[path[:len(path) - len(getlast(path))]]:
file_content = self.nodes[getdir(path)][getfilename(path)]
new_file = File(file_content)
return new_file
else:
raise NodeDoesNotExistError
def remove(self, path, directory = False, force = True):
if path in self.nodes and directory == False:
raise NonExplicitDirectoryDeletionError
if directory == False:
file_name = getfilename(path)
tmp_dir = getdir(path)
if not self.nodes[tmp_dir] == {} and file_name in self.nodes[tmp_dir]:
self.available_size += len(self.nodes[tmp_dir][file_name])
tmp = self.nodes[tmp_dir].pop(file_name)
else:
raise DestinationNodeExistsError
else:
if path in self.nodes:
if force == True:
for i in list(self.nodes.keys()):
if path in i:
del self.nodes[i]
elif force == False and not self.nodes[path] == {}:
raise NonEmptyDirectoryDeletionError
elif self.nodes[path] == {}:
for i in list(self.nodes.keys()):
if path in i and self.nodes[i] == {}:
del self.nodes[i]
else:
raise NonExplicitDirectoryDeletionError
def move(self, source, destination):
new_dir = getdir(destination)
new_file = getfilename(destination)
if new_dir in self.nodes and new_file in self.nodes[new_dir]:
raise DestinationNotADirectoryError
elif not destination in self.nodes and not(new_dir in self.nodes or new_file in self.nodes[new_dir]):
raise DestinationNodeDoesNotExistError
elif not source in self.nodes and not getdir(source) in self.nodes:
raise SourceNodeDoesNotExistError
elif getlast(source) in self.cnodes[destination]:
raise DestinationNodeExistsError
else:
d_name = getdir(source)
name = getlast(source)[1::]
if d_name in self.nodes and name in self.nodes[getdir(source)]:
move_file_content = self.nodes[d_name][name]
self.nodes[destination][name] = move_file_content
del self.nodes[d_name][name]
def link(self, source, destination, symbolic = True):
if not (source in self.nodes or getdir(source) in self.nodes) and symbolic == True:
raise NodeDoesNotExistError
elif source in self.nodes and symbolic == False:
return DirectoryHardLinkError
else:
if symbolic == True:
s_link = symbolic(self, source)
self.available_size -= 1
return s_link
else:
new_file = self.get_node(source)
file_name = getfilename(source)
file_dir = getdir(source)
if file_dir in self.nodes and file_name not in self.nodes[file_dir] or file_dir not in self.nodes:
raise SourceNodeDoesNotExistError
h_link = HardLink(new_file)
if (getdir(destination) not in self.nodes):
self.create(getdir(destination), directory = True)
self.nodes[getdir(
destination)][getfilename(
destination)] = h_link.content
self.available_size -= 1
return h_link
class symbolic_link(FileSystem):
def __init__ (self, fs, link_path):
self.link_path = link_path
self.nodes = fs.nodes
def __getattr__(self, link_path):
return self.link_path
def getcontent(self):
file_name = getfilename(self.link_path)
file_dir = getdir(self.link_path)
if file_dir in self.nodes and file_name in self.nodes[file_dir]:
return self.nodes[file_dir][file_name]
elif self.link_path in self.nodes:
return self.nodes[self.link_path]
class HardLink(File):
def __init__(self, f):
self.content = f.content
def __getattr__(self, content):
return self.content

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

.EEEE.E.EEE.EE.EE.
======================================================================
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_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_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

----------------------------------------------------------------------
Ran 18 tests in 24.147s

FAILED (errors=12)

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

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

+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = 'File system error!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ self.message = 'The node does not exist!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ self.message = 'Not enough space in your file system!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'The file system does not contain such source node!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'The destination node does not exist!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class DestinationNodeExistsError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'The destination node already exist!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ self.message = 'Mount error!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount error!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount error!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount error!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class DestinationNotADirectoryError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'Destination is not a directory!'
+
+ def __str__(self):
+ return repr(self.message)
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ self.message = 'Cannot create hard link for a directory!!!'
+
+ def __str__(self):
+ return repr(self.message)
+
+def getlast(path):
+ if '/' in path:
+ help_str = ''
+ for i in path[::-1]:
+ help_str = help_str + i
+ if i == '/': break
+ return help_str[::-1]
+
+def split(path):
+ if path[0] == '/':
+ list_of_dir = []
+ tmp_str = ''
+ count = 0
+ for i in path[1::]:
+ count += 1
+ if not i == '/':
+ tmp_str = tmp_str + i
+ elif not '/' in path[count::]:
+ list_of_dir.append(tmp_str)
+ list_of_dir.append(getlast(tmp_str))
+ tmp_str = ''
+ else:
+ list_of_dir.append('/' + tmp_str)
+ tmp_str = tmp_str + '/'
+ list_of_dir.append('/' + tmp_str)
+ return list_of_dir
+
+def getdir(path):
+ return path[:len(path) - len(getlast(path))]
+
+def getfilename(path):
+ return getlast(path)[1::]
+
+class File:
+ def __init__(self, content):
+ self.content = content
+
+ def append(self, text):
+ self.content = self.content + text
+
+ def truncate(self, text):
+ self.content = text
+
+ def size(self):
+ return len(self.content) + 1
+
+ def __getattr__(self, content):
+ return self.content
+
+class Directory:
+ def __init__(self, path):
+ self.path = path
+
+ def __getattr__(self, path):
+ return path
+
+ def directories(self):
+ #list_of_dir = split(self.path)
+ list_of_subdir = split(self.path)
+ #for i in list_of_dir:
+ #if path in i:
+ #list_of_subdir.append(i)
+ return list_of_subdir
+
+
+class FileSystem(File, Directory):
+ def __init__ (self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.nodes = {'/': {}}
+
+ def __getattr__(self, size):
+ return self.size
+
+ def __getattr__(self, available_size):
+ return self.available_size
+
+ def __getattr__(self, nodes):
+ return self.nodes
+
+ def create (self, path, directory = False, content = ''):
+ if directory == False:
+ new_file = File(content)
+ if path.count('/') > 1:
+ file_name = getlast(path)
+ path = path[:len(path) - len(file_name)]
+ elif path.count('/') == 1:
+ file_name = path
+ path = '/'
+ if path in self.nodes and not file_name[1::] in self.nodes[path]:
+ self.nodes[path][file_name[1::]] = content
+ elif not path in self.nodes:
+ raise DestinationNodeDoesNotExistError
+ if self.available_size - len(new_file.content) < 1:
+ raise NotEnoughSpaceError
+ self.available_size -= len(new_file.content) + 1
+ return new_file
+ else:
+ if path in self.nodes:
+ raise DestinationNodeExistsError
+ elif getdir(path) in self.nodes:
+ new_directory = Directory(path)
+ #new_list_of_directories = split(path)
+ #for i in new_list_of_directories:
+ #self.catalog[i] = {}
+ if self.available_size - path.count('/') < 1:
+ raise NotEnoughSpaceError
+ self.nodes[path] = {}
+ self.available_size -= path.count('/')
+ return new_directory
+ elif path.count('/') == 1 and not path == '/':
+ new_directory = Directory(path)
+ self.nodes[path] = {}
+ if self.available_size - path.count('/') < 1:
+ raise NotEnoughSpaceError
+ self.available_size -= path.count('/')
+ return new_directory
+ elif getdir(path) in self.nodes and getlast(path)[1::] in self.nodes[getlast(path)]:
+ return DestinationNodeExistsError
+
+ def get_node(self, path):
+ if path in self.nodes:
+ new_dir = Directory(path)
+ return new_dir
+ elif path[:len(path) - len(getlast(path))] in self.nodes and getlast(path)[1::] in self.nodes[path[:len(path) - len(getlast(path))]]:
+ file_content = self.nodes[getdir(path)][getfilename(path)]
+ new_file = File(file_content)
+ return new_file
+ else:
+ raise NodeDoesNotExistError
+
+ def remove(self, path, directory = False, force = True):
+ if path in self.nodes and directory == False:
+ raise NonExplicitDirectoryDeletionError
+ if directory == False:
+ file_name = getfilename(path)
+ tmp_dir = getdir(path)
+ if not self.nodes[tmp_dir] == {} and file_name in self.nodes[tmp_dir]:
+ self.available_size += len(self.nodes[tmp_dir][file_name])
+ tmp = self.nodes[tmp_dir].pop(file_name)
+ else:
+ raise DestinationNodeExistsError
+ else:
+ if path in self.nodes:
+ if force == True:
+ for i in list(self.nodes.keys()):
+ if path in i:
+ del self.nodes[i]
+ elif force == False and not self.nodes[path] == {}:
+ raise NonEmptyDirectoryDeletionError
+ elif self.nodes[path] == {}:
+ for i in list(self.nodes.keys()):
+ if path in i and self.nodes[i] == {}:
+ del self.nodes[i]
+ else:
+ raise NonExplicitDirectoryDeletionError
+
+ def move(self, source, destination):
+ new_dir = getdir(destination)
+ new_file = getfilename(destination)
+ if new_dir in self.nodes and new_file in self.nodes[new_dir]:
+ raise DestinationNotADirectoryError
+ elif not destination in self.nodes and not(new_dir in self.nodes or new_file in self.nodes[new_dir]):
+ raise DestinationNodeDoesNotExistError
+ elif not source in self.nodes and not getdir(source) in self.nodes:
+ raise SourceNodeDoesNotExistError
+ elif getlast(source) in self.cnodes[destination]:
+ raise DestinationNodeExistsError
+ else:
+ d_name = getdir(source)
+ name = getlast(source)[1::]
+ if d_name in self.nodes and name in self.nodes[getdir(source)]:
+ move_file_content = self.nodes[d_name][name]
+ self.nodes[destination][name] = move_file_content
+ del self.nodes[d_name][name]
+
+ def link(self, source, destination, symbolic = True):
+ if not (source in self.nodes or getdir(source) in self.nodes) and symbolic == True:
+ raise NodeDoesNotExistError
+ elif source in self.nodes and symbolic == False:
+ return DirectoryHardLinkError
+ else:
+ if symbolic == True:
+ s_link = symbolic(self, source)
+ self.available_size -= 1
+ return s_link
+ else:
+ new_file = self.get_node(source)
+ file_name = getfilename(source)
+ file_dir = getdir(source)
+ if file_dir in self.nodes and file_name not in self.nodes[file_dir] or file_dir not in self.nodes:
+ raise SourceNodeDoesNotExistError
+ h_link = HardLink(new_file)
+ if (getdir(destination) not in self.nodes):
+ self.create(getdir(destination), directory = True)
+ self.nodes[getdir(
+ destination)][getfilename(
+ destination)] = h_link.content
+ self.available_size -= 1
+ return h_link
+
+class symbolic_link(FileSystem):
+ def __init__ (self, fs, link_path):
+ self.link_path = link_path
+ self.nodes = fs.nodes
+
+ def __getattr__(self, link_path):
+ return self.link_path
+
+ def getcontent(self):
+ file_name = getfilename(self.link_path)
+ file_dir = getdir(self.link_path)
+ if file_dir in self.nodes and file_name in self.nodes[file_dir]:
+ return self.nodes[file_dir][file_name]
+ elif self.link_path in self.nodes:
+ return self.nodes[self.link_path]
+
+class HardLink(File):
+ def __init__(self, f):
+ self.content = f.content
+
+ def __getattr__(self, content):
+ return self.content