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

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

Към профила на Клара Кайралах

Резултати

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

Код

import tempfile
import exceptions
import os
import shutil
from os import listdir
from os.path import isfile
class NodeDoesNotExistError(Exception):
def __init__(self):
self.message = ''
class FileSystemMountError(Exception):
def __init__(self):
self.message = ''
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Source node does not exist! '
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node does not exist! '
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mouse point does not exist! '
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mouse point is not a directory! '
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mouse point is not empty!'
class NotAMountpointError(FileSystemMountError):
def __init__(self):
self.message = 'Not a mount point error'
class DestinationNodeExistsError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node exists error'
class File:
def __init__(self, path, content):
self.path = path
self.content = content
self.is_directory = False
def append(text):
self.content = self.content + text
def truncate(text):
self.content = ''
self.content += text
def size():
return len(self.content) + 1
class Directory:
def __init__(self, path):
self.path = path
self.is_directory = True
def files():
return [f for f in listdir(self.path)
if isfile(join(self.path, f))]
def directories():
return [f for f in listdir(self.path)
if not isfile(join(self.path, f))]
def nodes():
return os.listdir(self.path)
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.path = "/"
self.explorer = {}
def __getsize__(self):
return self.size
def __getavsize__(self):
return self.available_size
def _get_explorer(self):
return self.explorer
def get_node(self, path):
if self.is_existing_file(path):
file_content = self.get_content(path)
if file_content == '':
return Directory(path)
else:
return File(path, file_content)
else:
raise NodeDoesNotExistError
def is_existing_file(self, path):
for key, value in self.explorer.items():
if path == key:
return True
def get_content(self, path):
for key, value in self.explorer.items():
if path == key:
return value
def is_not_empty(self, path):
files = os.listdir(path)
if len(files) != 0:
return True
def create(self, path, *, directory=False, content=''):
if content == '':
directory is True
if directory is False:
if self.is_existing_file(path):
raise DestinationNodeExistsError
len_of_file = len(content) + 1
if self.__getavsize__() >= len_of_file:
make_file = open("./%s" % (path), 'a+')
self.available_size = self.available_size - len_of_file
make_file.write(content)
make_file.close()
self.explorer[path] = content
else:
raise NotEnoughSpaceError
else:
if self.is_existing_file(path) is not True:
making_dir = os.mkdir(path)
self.available_size += 1
self.explorer[path] = content
else:
raise DestinationNodeExistsError
def remove(self, path, *, directory=False, force=True):
if os.path.isdir(path):
if directory is True:
if force is True and is_not_empty(path) is True:
del self.explorer[path]
available_size -= 1
os.rmdir(path)
else:
raise NonEmptyDirectoryDeletionError
else:
raise NonExplicitDirectoryDeletionError
else:
if is_existing_file(path):
file_content = read()
len_of_file = len(file_content) + 1
available_size += len_of_file
del self.explorer[path]
os.remove(path)
def move(self, source, destination):
try:
value = self.explorer[source]
except KeyError:
raise SourceNodeDoesNotExistError
try:
value = self.explorer[destination]
if os.path.isdir(destination):
dirs = os.listdir(destination)
for files in dirs:
if files == source:
raise DestinationNodeExistsError
shutil.move(source, destination)
else:
raise DestinationNotADirectoryError
except KeyError:
raise DestinationNodeDoesNotExistError
def link(self, source, destination, *, symbolic=True):
if is_existing_file(path) is not True and symbolic is True:
raise NodeDoesNotExistError
if is_existing_file(path) is True:
if symbolic is False:
os.link(source, destination)
else:
os.symlink(source, destination)
else:
raise SourceNodeDoesNotExistError
if os.path.isdir(source) is True and symbolic is False:
raise DirectoryHardLinkError
def mount(self, file_system, path):
if is_not_empty(path):
raise MountPointNotEmptyError
if not os.path.isdir(path):
raise MountPointNotADirectoryError
if not is_existing_file(path):
raise MountPointDoesNotExistError
def unmount(self, path):
if is_existing_file(path) is False:
raise NodeDoesNotExistError

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

No module named 'exceptions'
  File "lib/language/python/runner.py", line 107, in main
    test = imp.load_source('test', test_module)
  File "/home/pyfmi/Python-3.4.2/Lib/imp.py", line 171, in load_source
    module = methods.load()
  File "<frozen importlib._bootstrap>", line 1220, in load
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/tmp/d20150525-4032-miihs4/test.py", line 2, in <module>
    import solution
  File "/tmp/d20150525-4032-miihs4/solution.py", line 2, in <module>
    import exceptions

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

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

+import tempfile
+import exceptions
+import os
+import shutil
+from os import listdir
+from os.path import isfile
+
+
+class NodeDoesNotExistError(Exception):
+
+ def __init__(self):
+ self.message = ''
+
+
+class FileSystemMountError(Exception):
+
+ def __init__(self):
+ self.message = ''
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'Source node does not exist! '
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'Destination node does not exist! '
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+
+ def __init__(self):
+ self.message = 'Mouse point does not exist! '
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+
+ def __init__(self):
+ self.message = 'Mouse point is not a directory! '
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+
+ def __init__(self):
+ self.message = 'Mouse point is not empty!'
+
+
+class NotAMountpointError(FileSystemMountError):
+
+ def __init__(self):
+ self.message = 'Not a mount point error'
+
+
+class DestinationNodeExistsError(NodeDoesNotExistError):
+
+ def __init__(self):
+ self.message = 'Destination node exists error'
+
+
+class File:
+
+ def __init__(self, path, content):
+ self.path = path
+ self.content = content
+ self.is_directory = False
+
+ def append(text):
+ self.content = self.content + text
+
+ def truncate(text):
+ self.content = ''
+ self.content += text
+
+ def size():
+ return len(self.content) + 1
+
+
+class Directory:
+
+ def __init__(self, path):
+ self.path = path
+ self.is_directory = True
+
+ def files():
+ return [f for f in listdir(self.path)
+ if isfile(join(self.path, f))]
+
+ def directories():
+ return [f for f in listdir(self.path)
+ if not isfile(join(self.path, f))]
+
+ def nodes():
+ return os.listdir(self.path)
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.path = "/"
+ self.explorer = {}
+
+ def __getsize__(self):
+ return self.size
+
+ def __getavsize__(self):
+ return self.available_size
+
+ def _get_explorer(self):
+ return self.explorer
+
+ def get_node(self, path):
+ if self.is_existing_file(path):
+ file_content = self.get_content(path)
+ if file_content == '':
+ return Directory(path)
+ else:
+ return File(path, file_content)
+ else:
+ raise NodeDoesNotExistError
+
+ def is_existing_file(self, path):
+ for key, value in self.explorer.items():
+ if path == key:
+ return True
+
+ def get_content(self, path):
+ for key, value in self.explorer.items():
+ if path == key:
+ return value
+
+ def is_not_empty(self, path):
+ files = os.listdir(path)
+ if len(files) != 0:
+ return True
+
+ def create(self, path, *, directory=False, content=''):
+ if content == '':
+ directory is True
+
+ if directory is False:
+ if self.is_existing_file(path):
+ raise DestinationNodeExistsError
+ len_of_file = len(content) + 1
+ if self.__getavsize__() >= len_of_file:
+ make_file = open("./%s" % (path), 'a+')
+ self.available_size = self.available_size - len_of_file
+ make_file.write(content)
+ make_file.close()
+ self.explorer[path] = content
+ else:
+ raise NotEnoughSpaceError
+ else:
+ if self.is_existing_file(path) is not True:
+ making_dir = os.mkdir(path)
+ self.available_size += 1
+ self.explorer[path] = content
+ else:
+ raise DestinationNodeExistsError
+
+ def remove(self, path, *, directory=False, force=True):
+ if os.path.isdir(path):
+ if directory is True:
+ if force is True and is_not_empty(path) is True:
+ del self.explorer[path]
+ available_size -= 1
+ os.rmdir(path)
+ else:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ raise NonExplicitDirectoryDeletionError
+ else:
+ if is_existing_file(path):
+ file_content = read()
+ len_of_file = len(file_content) + 1
+ available_size += len_of_file
+ del self.explorer[path]
+ os.remove(path)
+
+ def move(self, source, destination):
+ try:
+ value = self.explorer[source]
+ except KeyError:
+ raise SourceNodeDoesNotExistError
+ try:
+ value = self.explorer[destination]
+ if os.path.isdir(destination):
+ dirs = os.listdir(destination)
+ for files in dirs:
+ if files == source:
+ raise DestinationNodeExistsError
+ shutil.move(source, destination)
+ else:
+ raise DestinationNotADirectoryError
+ except KeyError:
+ raise DestinationNodeDoesNotExistError
+
+ def link(self, source, destination, *, symbolic=True):
+ if is_existing_file(path) is not True and symbolic is True:
+ raise NodeDoesNotExistError
+
+ if is_existing_file(path) is True:
+ if symbolic is False:
+ os.link(source, destination)
+ else:
+ os.symlink(source, destination)
+ else:
+ raise SourceNodeDoesNotExistError
+ if os.path.isdir(source) is True and symbolic is False:
+ raise DirectoryHardLinkError
+
+ def mount(self, file_system, path):
+ if is_not_empty(path):
+ raise MountPointNotEmptyError
+ if not os.path.isdir(path):
+ raise MountPointNotADirectoryError
+ if not is_existing_file(path):
+ raise MountPointDoesNotExistError
+
+ def unmount(self, path):
+
+ if is_existing_file(path) is False:
+ raise NodeDoesNotExistError