Решение на In-memory файлова система от Радослав Аспарухов

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

Към профила на Радослав Аспарухов

Резултати

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

Код

import os
class FileSystemError(Exception):
def __init__(self):
self.message = 'Something with the file system went terribly wrong!'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
self.message = 'Can not make hard link to a directory!'
class NotAFileError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class FileSystemMountError(FileSystemError):
def __init__(self):
self.message = 'Can not mount'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not enough space'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
self.message = 'Specified destination is not a directory'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non empty directory deletion attempt'
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not empty!'
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point does not exist!'
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not a directory!'
class NotAMountpointError(FileSystemMountError):
def __init__(self):
self.message = 'This is not a mount point, bro...'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
self.message = 'Destination node exists!'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'Node does not exist!'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node does not exist!'
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Source node does not exist!'
class Element(object):
def __init__(self, path, is_directory, content=''):
self.path = path
self.is_directory = is_directory
self._content = content
self.size = len(content) + 1
self.parent = None
self.is_symbolic_link = False
self.source = None
@property
def content(self):
if self.is_symbolic_link:
return self.parent.get_node(self.source).content
else:
return self._content
@content.setter
def content(self, value):
self._content = content
def get_name(self):
return self.path.rpartition('/')[2]
def append(self, text):
if self.is_directory:
raise NotAFileError
if self.parent.available_size <= len(text):
raise NotEnoughSpaceError
else:
self.content += text
self.parent.available_size = self.parent.available_size - len(text)
def truncate(self, text):
if self.is_directory:
raise NotAFileError
size_difference = len(text) - self.size
if self.parent.available_size >= size_difference:
self.content = text
self.parent.available_size = self.parent.available_size
- size_difference
def directories(self):
if not self.is_directory:
raise NotADirectoryError
else:
directories = list()
for x in self.parent.elements:
if self.is_symbolic_link:
if x.is_directory and \
x.path.rpartition('/')[0] == self.source:
directories.append(x)
else:
if x.is_directory and \
x.path.rpartition('/')[0] == self.path:
directories.append(x)
return directories
def files(self):
if not self.is_directory:
raise NotADirectoryError
else:
files = list()
for x in self.parent.elements:
if self.is_symbolic_link:
if not x.is_directory \
and x.path.rpartition('/')[0] == self.source:
files.append(x)
else:
if not x.is_directory \
and x.path.rpartition('/')[0] == self.path:
files.append(x)
return files
def is_empty(self):
if self.is_directory:
if len(self.files()) == len(self.directories()) == 0:
return True
else:
return False
else:
if self.size == 1:
return True
else:
return False
class FileSystem(object):
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.elements = list()
self.mount_points = list()
def _get_parrent_folder(self, path):
return path.rpartition('/')[0]
def _parent_folder_exists(self, path):
for x in self.elements:
if x.path == self._get_parrent_folder(path):
return True
return False
def _element_has_parent(self, element, parent):
return element.path.starswith(parent)
def _element_exists(self, path):
for elem in self.elements:
if elem.path == path:
return True
return False
def _folder_empty(self, path):
for x in self.elements:
if path == self._get_parrent_folder(x.path):
return False
return True
def _is_mount_point(self, path):
for x in self.mount_points:
if x == path:
return True
return False
def get_node(self, path):
for element in self.elements:
if element.path == path:
return element
def create(self, path, directory=False, content=''):
if len(content) + 1 > self.available_size:
raise NotEnoughSpaceError
if self._element_exists(path):
raise DestinationNodeExistsError
if self._parent_folder_exists(path) or path.count('/') == 1:
element = Element(path, directory, content)
self.elements.append(element)
self.available_size -= element.size
element.parent = self
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
if self._element_exists(path):
if not directory:
for element in self.elements:
if element.path == path:
if element.is_directory:
raise NonExplicitDirectoryDeletionError
self.elements.remove(element)
else:
if self._folder_empty(path):
for element in self.elements:
if element.path == path:
self.elements.remove(element)
else:
if not force:
raise NonEmptyDirectoryDeletionError
else:
for element in self.elements:
if element.path == path or path == \
self._get_parrent_folder(element.path):
self.elements.remove(element)
else:
raise NodeDoesNotExistError
def move(self, source, destination):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
if not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
for x in self.elements:
if x.path == destination:
if not x.is_directory:
raise DestinationNotADirectoryError
else:
if self._element_exists(destination + '/' + source):
raise DestinationNodeExistsError
else:
if x.is_directory:
for e in self.elements:
if self._element_has_parent(e.path, x.path):
e.path = destination
+ e.path.lstrip(source)
x.path = destination + '/' + x.get_name()
def link(self, source, destination, symbolic=True):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
elif not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
elif self._element_exists(source) and not symbolic \
and self.get_node(source).is_directory:
raise DirectoryHardLinkError
else:
if symbolic:
if self.get_node(source).is_directory:
fs.create(destination, directory=True)
fs.get_node(destination).is_symbolic_linc = True
fs.get_node(destination).source = source
if not symbolic and \
self.available_size >= len(fs.get_node(source)):
fs.create(destination, content=fs.get_node(source).content)
def mount(self, file_system, path):
if self.get_node(path) is None:
raise MountPointDoesNotExistError
if not self.get_node(path).is_directory:
raise MountPointNotADirectoryError
elif not self.get_node(path).is_empty():
raise MountPointNotEmptyError
else:
for x in list(file_system.elements):
x.path = path + x.path
file_system.elements.append(x)
self.mount_points.append(path)
def unmount(self, path):
if not self._element_exists(path):
raise NodeDoesNotExistError
elif not self._is_mount_point(path):
raise NotAMountpointError
else:
for x in self.elements:
if self._get_parrent_folder(x.path) == path:
self.elements.remove(x)
self.mount_points.remove(path)

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

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

FAILED (errors=11)

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

Радослав обнови решението на 30.04.2015 01:09 (преди над 9 години)

+import os
+
+
+class FileSystemError(Exception):
+ def __init__(self):
+ self.message = 'Something with the file system went terribly wrong!'
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non explicit directory deletion attempt!'
+
+
+class DirectoryHardLinkError(FileSystemError):
+ def __init__(self):
+ self.message = 'Can not make hard link to a directory!'
+
+
+class NotAFileError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non explicit directory deletion attempt!'
+
+
+
+class FileSystemMountError(FileSystemError):
+ def __init__(self):
+ self.message = 'Can not mount'
+
+
+class NotEnoughSpaceError(FileSystemError):
+ def __init__(self):
+ self.message = 'Not enough space'
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ def __init__(self):
+ self.message = 'Specified destination is not a directory'
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ def __init__(self):
+ self.message = 'Non empty directory deletion attempt'
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point is not empty!'
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point does not exist!'
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'Mount point is not a directory!'
+
+
+class DestinationNodeExistsError(FileSystemError):
+ def __init__(self):
+ self.message = 'Destination node exists!'
+
+
+class NodeDoesNotExistError(FileSystemError):
+ def __init__(self):
+ self.message = 'Node does not exist!'
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'Destination node does not exist!'
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ def __init__(self):
+ self.message = 'Source node does not exist!'
+
+
+class Element(object):
+ def __init__(self, path, is_directory, content=''):
+ self.path = path
+ self.is_directory = is_directory
+ self.content = content
+ self.size = len(content) + 1
+ self.parent = None
+
+ def get_name(self):
+ return self.path.rpartition('/')[2]
+
+ def append(self, text):
+ if self.is_directory:
+ raise NotAFileError
+
+ if self.parent.available_size <= len(text):
+ raise NotEnoughSpaceError
+ else:
+ self.content += text
+ self.parent.available_size = self.parent.available_size - len(text)
+
+ def truncate(self, text):
+ if self.is_directory:
+ raise NotAFileError
+
+ size_difference = len(text) - self.size
+ if self.parent.available_size >= size_difference:
+ self.content = text
+ self.parent.available_size = self.parent.available_size \
+ - size_difference
+
+ def directories(self):
+ if not self.is_directory:
+ raise NotADirectoryError
+ else:
+ directories = list()
+ for x in self.parent.elements:
+ if x.is_directory and x.path.rpartition('/')[0] == self.path:
+ directories.append(x)
+ return directories
+
+ def files(self):
+ if not self.is_directory:
+ raise NotADirectoryError
+ else:
+ files = list()
+ for x in self.parent.elements:
+ if not x.is_directory \
+ and x.path.rpartition('/')[0] == self.path:
+ files.append(x)
+ return files
+
+
+class FileSystem(object):
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.elements = list()
+
+ def _get_parrent_folder(self, path):
+ return path.rpartition('/')[0]
+
+ def _parent_folder_exists(self, path):
+ for x in self.elements:
+ if x.path == self._get_parrent_folder(path):
+ return True
+ return False
+
+ def _element_has_parent(self, element, parent):
+ return element.path.starswith(parent)
+
+
+ def _element_exists(self, path):
+ for elem in self.elements:
+ if elem.path == path:
+ return True
+ return False
+
+ def _folder_empty(self, path):
+ for x in self.elements:
+ if path == self._get_parrent_folder(x.path):
+ return False
+ return True
+
+ def get_node(self, path):
+ for element in self.elements:
+ if element.path == path:
+ return element
+
+ def create(self, path, directory=False, content=''):
+ if len(content) + 1 > self.available_size:
+ raise NotEnoughSpaceError
+ if self._element_exists(path):
+ raise DestinationNodeExistsError
+ if self._parent_folder_exists(path) or path.count('/') == 1:
+ element = Element(path, directory, content)
+ self.elements.append(element)
+ self.available_size -= element.size
+ element.parent = self
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ if self._element_exists(path):
+ if not directory:
+ for element in self.elements:
+ if element.path == path:
+ if element.is_directory:
+ raise NonExplicitDirectoryDeletionError
+ self.elements.remove(element)
+ else:
+ if self._folder_empty(path):
+ for element in self.elements:
+ if element.path == path:
+ self.elements.remove(element)
+ else:
+ if not force:
+ raise NonEmptyDirectoryDeletionError
+ else:
+ for element in self.elements:
+ if element.path == path \
+ or path == self.\
+ _get_parrent_folder(element.path):
+ self.elements.remove(element)
+ else:
+ raise NodeDoesNotExistError
+
+ def move(self, source, destination):
+ if not self._element_exists(source):
+ raise SourceNodeDoesNotExistError
+ if not self._element_exists(destination):
+ raise DestinationNodeDoesNotExistError
+ for x in self.elements:
+ if x.path == destination:
+ if not x.is_directory:
+ raise DestinationNotADirectoryError
+ else:
+ if self._element_exists(destination + '/' + source):
+ raise DestinationNodeExistsError
+ else:
+ if x.is_directory:
+ for e in self.elements:
+ if self._element_has_parent(e.path, x.path):
+ e.path = destination \
+ + e.path.lstrip(source)
+ x.path = destination + '/' + x.get_name()
+
+ def link(self, source, destination, symbolic=True):
+ if not self._element_exists(source):
+ raise SourceNodeDoesNotExistError
+ elif not self._element_exists(destination):
+ raise DestinationNodeDoesNotExistError
+ elif self._element_exists(source) and not symbolic \
+ and self.get_node(source).is_directory:
+ raise DirectoryHardLinkError
+ else:
+ if not symbolic and self.available_size >= len(fs.get_node(source)):
+ fs.create(destination, content=fs.get_node(source).content)
+

Радослав обнови решението на 30.04.2015 01:17 (преди над 9 години)

+'''
import os
class FileSystemError(Exception):
def __init__(self):
self.message = 'Something with the file system went terribly wrong!'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
self.message = 'Can not make hard link to a directory!'
class NotAFileError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
-
class FileSystemMountError(FileSystemError):
def __init__(self):
self.message = 'Can not mount'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not enough space'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
self.message = 'Specified destination is not a directory'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non empty directory deletion attempt'
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not empty!'
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point does not exist!'
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not a directory!'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
self.message = 'Destination node exists!'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'Node does not exist!'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node does not exist!'
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Source node does not exist!'
class Element(object):
def __init__(self, path, is_directory, content=''):
self.path = path
self.is_directory = is_directory
self.content = content
self.size = len(content) + 1
self.parent = None
def get_name(self):
return self.path.rpartition('/')[2]
def append(self, text):
if self.is_directory:
raise NotAFileError
if self.parent.available_size <= len(text):
raise NotEnoughSpaceError
else:
self.content += text
self.parent.available_size = self.parent.available_size - len(text)
def truncate(self, text):
if self.is_directory:
raise NotAFileError
size_difference = len(text) - self.size
if self.parent.available_size >= size_difference:
self.content = text
- self.parent.available_size = self.parent.available_size \
- - size_difference
+ self.parent.available_size = self.parent.available_size
+ - size_difference
def directories(self):
if not self.is_directory:
raise NotADirectoryError
else:
directories = list()
for x in self.parent.elements:
if x.is_directory and x.path.rpartition('/')[0] == self.path:
directories.append(x)
return directories
def files(self):
if not self.is_directory:
raise NotADirectoryError
else:
files = list()
for x in self.parent.elements:
if not x.is_directory \
and x.path.rpartition('/')[0] == self.path:
files.append(x)
return files
class FileSystem(object):
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.elements = list()
def _get_parrent_folder(self, path):
return path.rpartition('/')[0]
def _parent_folder_exists(self, path):
for x in self.elements:
if x.path == self._get_parrent_folder(path):
return True
return False
def _element_has_parent(self, element, parent):
return element.path.starswith(parent)
-
def _element_exists(self, path):
for elem in self.elements:
if elem.path == path:
return True
return False
def _folder_empty(self, path):
for x in self.elements:
if path == self._get_parrent_folder(x.path):
return False
return True
def get_node(self, path):
for element in self.elements:
if element.path == path:
return element
def create(self, path, directory=False, content=''):
if len(content) + 1 > self.available_size:
raise NotEnoughSpaceError
if self._element_exists(path):
raise DestinationNodeExistsError
if self._parent_folder_exists(path) or path.count('/') == 1:
element = Element(path, directory, content)
self.elements.append(element)
self.available_size -= element.size
element.parent = self
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
if self._element_exists(path):
if not directory:
for element in self.elements:
if element.path == path:
if element.is_directory:
raise NonExplicitDirectoryDeletionError
self.elements.remove(element)
else:
if self._folder_empty(path):
for element in self.elements:
if element.path == path:
self.elements.remove(element)
else:
if not force:
raise NonEmptyDirectoryDeletionError
else:
for element in self.elements:
- if element.path == path \
- or path == self.\
- _get_parrent_folder(element.path):
+ if element.path == path or
+ path == self._get_parrent_folder(element.path):
self.elements.remove(element)
else:
raise NodeDoesNotExistError
def move(self, source, destination):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
if not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
for x in self.elements:
if x.path == destination:
if not x.is_directory:
raise DestinationNotADirectoryError
else:
if self._element_exists(destination + '/' + source):
raise DestinationNodeExistsError
else:
if x.is_directory:
for e in self.elements:
if self._element_has_parent(e.path, x.path):
- e.path = destination \
- + e.path.lstrip(source)
+ e.path = destination
+ + e.path.lstrip(source)
x.path = destination + '/' + x.get_name()
def link(self, source, destination, symbolic=True):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
elif not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
elif self._element_exists(source) and not symbolic \
and self.get_node(source).is_directory:
raise DirectoryHardLinkError
else:
- if not symbolic and self.available_size >= len(fs.get_node(source)):
+ if not symbolic and
+ self.available_size >= len(fs.get_node(source)):
fs.create(destination, content=fs.get_node(source).content)
+Не мога да прикача скрийншот, но го ъплоудвам и то се троши...
+'''

Радослав обнови решението на 30.04.2015 09:00 (преди над 9 години)

-'''
import os
class FileSystemError(Exception):
def __init__(self):
self.message = 'Something with the file system went terribly wrong!'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
self.message = 'Can not make hard link to a directory!'
class NotAFileError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class FileSystemMountError(FileSystemError):
def __init__(self):
self.message = 'Can not mount'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not enough space'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
self.message = 'Specified destination is not a directory'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non empty directory deletion attempt'
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not empty!'
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point does not exist!'
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not a directory!'
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
self.message = 'Destination node exists!'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'Node does not exist!'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node does not exist!'
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Source node does not exist!'
class Element(object):
def __init__(self, path, is_directory, content=''):
self.path = path
self.is_directory = is_directory
- self.content = content
+ self._content = content
self.size = len(content) + 1
self.parent = None
+ self.is_symbolic_link = False
+ self.source = None
+ @property
+ def content(self):
+ if self.is_symbolic_link:
+ return self.parent.get_node(self.source).content
+ else:
+ return self._content
+
+ @content.setter
+ def content(self, value):
+ self._content = content
+
def get_name(self):
return self.path.rpartition('/')[2]
def append(self, text):
if self.is_directory:
raise NotAFileError
if self.parent.available_size <= len(text):
raise NotEnoughSpaceError
else:
self.content += text
self.parent.available_size = self.parent.available_size - len(text)
def truncate(self, text):
if self.is_directory:
raise NotAFileError
size_difference = len(text) - self.size
if self.parent.available_size >= size_difference:
self.content = text
self.parent.available_size = self.parent.available_size
- size_difference
def directories(self):
if not self.is_directory:
raise NotADirectoryError
else:
directories = list()
for x in self.parent.elements:
- if x.is_directory and x.path.rpartition('/')[0] == self.path:
- directories.append(x)
+ if self.is_symbolic_link:
+ if x.is_directory and x.path.rpartition('/')[0] == self.source:
+ directories.append(x)
+ else:
+ if x.is_directory and x.path.rpartition('/')[0] == self.path:
+ directories.append(x)
return directories
def files(self):
if not self.is_directory:
raise NotADirectoryError
else:
files = list()
for x in self.parent.elements:
- if not x.is_directory \
- and x.path.rpartition('/')[0] == self.path:
- files.append(x)
+ if self.is_symbolic_link:
+ if not x.is_directory \
+ and x.path.rpartition('/')[0] == self.source:
+ files.append(x)
+ else:
+ if not x.is_directory \
+ and x.path.rpartition('/')[0] == self.path:
+ files.append(x)
return files
class FileSystem(object):
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.elements = list()
def _get_parrent_folder(self, path):
return path.rpartition('/')[0]
def _parent_folder_exists(self, path):
for x in self.elements:
if x.path == self._get_parrent_folder(path):
return True
return False
def _element_has_parent(self, element, parent):
return element.path.starswith(parent)
def _element_exists(self, path):
for elem in self.elements:
if elem.path == path:
return True
return False
def _folder_empty(self, path):
for x in self.elements:
if path == self._get_parrent_folder(x.path):
return False
return True
def get_node(self, path):
for element in self.elements:
if element.path == path:
return element
def create(self, path, directory=False, content=''):
if len(content) + 1 > self.available_size:
raise NotEnoughSpaceError
if self._element_exists(path):
raise DestinationNodeExistsError
if self._parent_folder_exists(path) or path.count('/') == 1:
element = Element(path, directory, content)
self.elements.append(element)
self.available_size -= element.size
element.parent = self
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
if self._element_exists(path):
if not directory:
for element in self.elements:
if element.path == path:
if element.is_directory:
raise NonExplicitDirectoryDeletionError
self.elements.remove(element)
else:
if self._folder_empty(path):
for element in self.elements:
if element.path == path:
self.elements.remove(element)
else:
if not force:
raise NonEmptyDirectoryDeletionError
else:
for element in self.elements:
- if element.path == path or
- path == self._get_parrent_folder(element.path):
+ if element.path == path or path == \
+ self._get_parrent_folder(element.path):
self.elements.remove(element)
else:
raise NodeDoesNotExistError
def move(self, source, destination):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
if not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
for x in self.elements:
if x.path == destination:
if not x.is_directory:
raise DestinationNotADirectoryError
else:
if self._element_exists(destination + '/' + source):
raise DestinationNodeExistsError
else:
if x.is_directory:
for e in self.elements:
if self._element_has_parent(e.path, x.path):
e.path = destination
+ e.path.lstrip(source)
x.path = destination + '/' + x.get_name()
def link(self, source, destination, symbolic=True):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
elif not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
elif self._element_exists(source) and not symbolic \
and self.get_node(source).is_directory:
raise DirectoryHardLinkError
else:
- if not symbolic and
+ if symbolic:
+ if self.get_node(source).is_directory:
+ fs.create(destination, directory=True)
+ fs.get_node(destination).is_symbolic_linc = True
+ fs.get_node(destination).source = source
+ if not symbolic and \
self.available_size >= len(fs.get_node(source)):
fs.create(destination, content=fs.get_node(source).content)
-
-Не мога да прикача скрийншот, но го ъплоудвам и то се троши...
-'''

Радослав обнови решението на 30.04.2015 09:31 (преди над 9 години)

import os
class FileSystemError(Exception):
def __init__(self):
self.message = 'Something with the file system went terribly wrong!'
class NonExplicitDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class DirectoryHardLinkError(FileSystemError):
def __init__(self):
self.message = 'Can not make hard link to a directory!'
class NotAFileError(FileSystemError):
def __init__(self):
self.message = 'Non explicit directory deletion attempt!'
class FileSystemMountError(FileSystemError):
def __init__(self):
self.message = 'Can not mount'
class NotEnoughSpaceError(FileSystemError):
def __init__(self):
self.message = 'Not enough space'
class DestinationNotADirectoryError(FileSystemError):
def __init__(self):
self.message = 'Specified destination is not a directory'
class NonEmptyDirectoryDeletionError(FileSystemError):
def __init__(self):
self.message = 'Non empty directory deletion attempt'
class MountPointNotEmptyError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not empty!'
class MountPointDoesNotExistError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point does not exist!'
class MountPointNotADirectoryError(FileSystemMountError):
def __init__(self):
self.message = 'Mount point is not a directory!'
+class NotAMountpointError(FileSystemMountError):
+ def __init__(self):
+ self.message = 'This is not a mount point, bro...'
+
+
class DestinationNodeExistsError(FileSystemError):
def __init__(self):
self.message = 'Destination node exists!'
class NodeDoesNotExistError(FileSystemError):
def __init__(self):
self.message = 'Node does not exist!'
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Destination node does not exist!'
class SourceNodeDoesNotExistError(NodeDoesNotExistError):
def __init__(self):
self.message = 'Source node does not exist!'
class Element(object):
def __init__(self, path, is_directory, content=''):
self.path = path
self.is_directory = is_directory
self._content = content
self.size = len(content) + 1
self.parent = None
self.is_symbolic_link = False
self.source = None
@property
def content(self):
if self.is_symbolic_link:
return self.parent.get_node(self.source).content
else:
return self._content
@content.setter
def content(self, value):
self._content = content
def get_name(self):
return self.path.rpartition('/')[2]
def append(self, text):
if self.is_directory:
raise NotAFileError
if self.parent.available_size <= len(text):
raise NotEnoughSpaceError
else:
self.content += text
self.parent.available_size = self.parent.available_size - len(text)
def truncate(self, text):
if self.is_directory:
raise NotAFileError
size_difference = len(text) - self.size
if self.parent.available_size >= size_difference:
self.content = text
self.parent.available_size = self.parent.available_size
- size_difference
def directories(self):
if not self.is_directory:
raise NotADirectoryError
else:
directories = list()
for x in self.parent.elements:
if self.is_symbolic_link:
- if x.is_directory and x.path.rpartition('/')[0] == self.source:
+ if x.is_directory and \
+ x.path.rpartition('/')[0] == self.source:
directories.append(x)
else:
- if x.is_directory and x.path.rpartition('/')[0] == self.path:
+ if x.is_directory and \
+ x.path.rpartition('/')[0] == self.path:
directories.append(x)
return directories
def files(self):
if not self.is_directory:
raise NotADirectoryError
else:
files = list()
for x in self.parent.elements:
if self.is_symbolic_link:
if not x.is_directory \
and x.path.rpartition('/')[0] == self.source:
files.append(x)
else:
if not x.is_directory \
and x.path.rpartition('/')[0] == self.path:
files.append(x)
return files
+ def is_empty(self):
+ if self.is_directory:
+ if len(self.files()) == len(self.directories()) == 0:
+ return True
+ else:
+ return False
+ else:
+ if self.size == 1:
+ return True
+ else:
+ return False
+
class FileSystem(object):
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.elements = list()
+ self.mount_points = list()
def _get_parrent_folder(self, path):
return path.rpartition('/')[0]
def _parent_folder_exists(self, path):
for x in self.elements:
if x.path == self._get_parrent_folder(path):
return True
return False
def _element_has_parent(self, element, parent):
return element.path.starswith(parent)
def _element_exists(self, path):
for elem in self.elements:
if elem.path == path:
return True
return False
def _folder_empty(self, path):
for x in self.elements:
if path == self._get_parrent_folder(x.path):
return False
return True
+ def _is_mount_point(self, path):
+ for x in self.mount_points:
+ if x == path:
+ return True
+ return False
+
def get_node(self, path):
for element in self.elements:
if element.path == path:
return element
def create(self, path, directory=False, content=''):
if len(content) + 1 > self.available_size:
raise NotEnoughSpaceError
if self._element_exists(path):
raise DestinationNodeExistsError
if self._parent_folder_exists(path) or path.count('/') == 1:
element = Element(path, directory, content)
self.elements.append(element)
self.available_size -= element.size
element.parent = self
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
if self._element_exists(path):
if not directory:
for element in self.elements:
if element.path == path:
if element.is_directory:
raise NonExplicitDirectoryDeletionError
self.elements.remove(element)
else:
if self._folder_empty(path):
for element in self.elements:
if element.path == path:
self.elements.remove(element)
else:
if not force:
raise NonEmptyDirectoryDeletionError
else:
for element in self.elements:
if element.path == path or path == \
- self._get_parrent_folder(element.path):
+ self._get_parrent_folder(element.path):
self.elements.remove(element)
else:
raise NodeDoesNotExistError
def move(self, source, destination):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
if not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
for x in self.elements:
if x.path == destination:
if not x.is_directory:
raise DestinationNotADirectoryError
else:
if self._element_exists(destination + '/' + source):
raise DestinationNodeExistsError
else:
if x.is_directory:
for e in self.elements:
if self._element_has_parent(e.path, x.path):
e.path = destination
+ e.path.lstrip(source)
x.path = destination + '/' + x.get_name()
def link(self, source, destination, symbolic=True):
if not self._element_exists(source):
raise SourceNodeDoesNotExistError
elif not self._element_exists(destination):
raise DestinationNodeDoesNotExistError
elif self._element_exists(source) and not symbolic \
and self.get_node(source).is_directory:
raise DirectoryHardLinkError
else:
if symbolic:
if self.get_node(source).is_directory:
- fs.create(destination, directory=True)
- fs.get_node(destination).is_symbolic_linc = True
- fs.get_node(destination).source = source
+ fs.create(destination, directory=True)
+ fs.get_node(destination).is_symbolic_linc = True
+ fs.get_node(destination).source = source
if not symbolic and \
- self.available_size >= len(fs.get_node(source)):
+ self.available_size >= len(fs.get_node(source)):
fs.create(destination, content=fs.get_node(source).content)
+
+ def mount(self, file_system, path):
+ if self.get_node(path) is None:
+ raise MountPointDoesNotExistError
+ if not self.get_node(path).is_directory:
+ raise MountPointNotADirectoryError
+ elif not self.get_node(path).is_empty():
+ raise MountPointNotEmptyError
+ else:
+ for x in list(file_system.elements):
+ x.path = path + x.path
+ file_system.elements.append(x)
+ self.mount_points.append(path)
+
+ def unmount(self, path):
+ if not self._element_exists(path):
+ raise NodeDoesNotExistError
+ elif not self._is_mount_point(path):
+ raise NotAMountpointError
+ else:
+ for x in self.elements:
+ if self._get_parrent_folder(x.path) == path:
+ self.elements.remove(x)
+ self.mount_points.remove(path)