Цвета обнови решението на 30.04.2015 01:20 (преди над 9 години)
+def get_subdirectory(path):
+ if not "/".join(path.split("/")[:len(path.split("/")) - 1:]):
+ return '/'
+ return "/".join(path.split("/")[:len(path.split("/")) - 1:])
+
+
+def get_name(path):
+ return path.split("/")[-1]
+
+
+def change_path(source, destination):
+ if destination == '/':
+ return '/' + source
+ return destination.replace(source, destination, 1)
+
+
+class LinkPathError(Exception):
+ pass
+
+
+class FileSystemError(Exception):
+ def __init__(self):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError(FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError(FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError(FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError(FileSystemError):
+ pass
+
+
+class DirectoryHardLinkError(FileSystemError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class MountPointNotEmptyError(FileSystemMountError):
+ pass
+
+
+class MountPointNotADirectoryError(FileSystemMountError):
+ pass
+
+
+class MountPointDoesNotExistError(FileSystemMountError):
+ pass
+
+
+class FileSystemMountError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class NotAMountpointError(FileSystemMountError):
+ pass
+
+
+class File():
+ def __init__(self, name, content=" "):
+ self.is_directory = False
+ self.content = content
+ self.size = len(self.content) + 1
+ self.name = name
+
+ def append(self, text):
+ self.content += text
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(text) + 1
+
+
+class SymbolicLink():
+ def __init__(self, link_path):
+ self.link_path = link_path
+
+
+class Directory():
+ def __init__(self, name):
+ self.is_directory = True
+ self.directories = []
+ self.files = []
+ self.nodes = []
+ self.size = 1
+ self.name = name
+
+ def get_directories(self):
+ return self.directories
+
+ def get_files(self):
+ return self.files
+
+ def get_nodes(self):
+ return self.nodes
+
+ def get_names(self):
+ result = []
+ for node in self.nodes:
+ result.append(node.name)
+ return result
+
+
+class HardLink():
+ pass
+
+
+class FileSystem():
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.nodes = {'/': Directory('/')}
+
+ def get_node(self, path):
+ if path not in self.nodes.keys():
+ raise NodeDoesNotExistError
+
+ return self.nodes[path]
+
+ def create(self, path, directory=False, content=''):
+ if directory:
+ node = Directory(get_name(path))
+ else:
+ node = File(get_name(path), content)
+
+ if get_subdirectory(path) not in self.nodes.keys():
+ raise DestinationNodeDoesNotExistError
+
+ if node.size > self.available_size:
+ raise NotEnoughSpaceError
+
+ if path in self.nodes.keys():
+ raise DestinationNodeExistsError
+
+ self.nodes[path] = node
+ self.available_size -= node.size
+
+ if isinstance(node, File):
+ self.nodes[get_subdirectory(path)].files.append(node)
+ else:
+ self.nodes[get_subdirectory(path)].directories.append(node)
+ self.nodes[get_subdirectory(path)].nodes.append(node)
+
+ def remove(self, path, directory=False, force=True):
+ if path not in self.nodes.keys():
+ raise NodeDoesNotExistError
+
+ if isinstance(self.nodes[path], Directory):
+ if not directory:
+ raise NonExplicitDirectoryDeletionError
+ if directory and self.nodes[path].nodes and not force:
+ raise NonEmptyDirectoryDeletionError
+
+ if isinstance(self.nodes[path], File):
+ self.nodes[get_subdirectory(path)].files.remove(self.nodes[path])
+ else:
+ self.nodes[get_subdirectory(path)].directories.remove(
+ self.nodes[path])
+ self.nodes[get_subdirectory(path)].nodes.remove(self.nodes[path])
+
+ self.available_size += self.nodes[path].size
+ self.nodes.pop(path)
+
+ def move(self, source, destination):
+ print("s/d : " + source + " d: " + destination)
+ if source not in self.nodes.keys():
+ raise SourceNodeDoesNotExistError
+
+ if destination not in self.nodes.keys():
+ raise DestinationNodeDoesNotExistError
+
+ if not isinstance(self.nodes[destination], Directory):
+ raise DestinationNotADirectoryError
+
+ if self.nodes[source].name in self.nodes[destination].get_names():
+ raise DestinationNodeExistsError
+
+ new_nodes = {}
+ for key in self.nodes.keys():
+ if key.find(source) == 0:
+ if destination == '/':
+ print('yes')
+ new_nodes[
+ key.replace(get_subdirectory(source), destination)[
+ 1::]] \
+ = self.nodes[key]
+ else:
+ new_nodes[
+ key.replace(get_subdirectory(source), destination)] \
+ = self.nodes[key]
+ else:
+ new_nodes[key] = self.nodes[key]
+
+ self.nodes[destination].nodes.append(self.nodes[source])
+ self.nodes[get_subdirectory(destination)].nodes.remove(
+ self.nodes[source])
+ if isinstance(self.nodes[source], Directory):
+ self.nodes[destination].directories.append(self.nodes[source])
+ self.nodes[get_subdirectory(destination)].directories.remove(
+ self.nodes[source])
+ if isinstance(self.nodes[source], File):
+ self.nodes[destination].files.append(self.nodes[source])
+ self.nodes[get_subdirectory(destination)].files.remove(
+ self.nodes[source])
+ self.nodes = new_nodes
+
+ def link(self, source, destination, symbolic=True):
+ if source not in self.nodes.keys() and symbolic:
+ raise NodeDoesNotExistError
+ if isinstance(self.nodes[destination] and not symbolic):
+ raise DirectoryHardLinkError
+ if isinstance(self.nodes[source],
+ HardLink) and destination not in self.nodes.keys():
+ raise SourceNodeDoesNotExistError
+
+ if isinstance(self.nodes[source], SymbolicLink):
+ if isinstance(self.nodes[destination], File):
+ pass
+ else:
+ pass
+ else:
+ pass
+
+
+ def mount(self, file_system, path):
+ if not self.nodes[path].nodes:
+ raise MountPointNotEmptyError
+ if isinstance(self.nodes[path], File):
+ raise MountPointNotADirectoryError
+ if path not in self.nodes.keys():
+ raise MountPointDoesNotExistError
+
+ def unmount(self, path):
+ if path not in self.nodes.keys():
+ raise NodeDoesNotExistError
+
+
+class MyError(Exception):
+ def __init__(self, value):
+ self.value = value
+
+ def __str__(self):
+ return repr(self.value)
+
+
+def main():
+ fs = FileSystem(22)
+
+ fs.create('/home', directory=True)
+ fs.create('/home/foo', directory=True)
+ fs.create('/home/moo', directory=True)
+ fs.create('/home/roo', directory=True)
+ fs.create('/home/roo/loo')
+ # fs.create('/home/roo/lala', directory=True)
+ # fs.create('/home/roo/lala/fafa', directory=True)
+ # fs.create('/home/roo/lala/fafa/swag', directory=True)
+ # fs.create('/home/roo/lala/fafa/yolo', directory=True)
+ # fs.create('/home/roo/lala/fafa/nigga', directory=True)
+
+ print(str(type(fs.nodes['/home/roo/loo'])))
+ fs.move('/home/roo', '/')
+
+
+if __name__ == '__main__':
+ main()