Решение на In-memory файлова система от Елица Христова

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

Към профила на Елица Христова

Резултати

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

Код

class Fil:
def __init__(self, name, content):
self.name = name
self.content = content
self.size = len(content) + 1
self.is_directory = False
def append(self, text):
self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(text) + 1
class Direc:
def __init__(self, directory, files=list()):
self.name = directory
self.nodes = list()
self.directories = list()
self.files = files
self.is_directory = True
def add_directory(self, directory):
new_directories = list()
new_directories.extend(self.directories)
new_directories.append(directory)
self.directories = new_directories
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(directory)
self.nodes = new_nodes
def add_file(self, filee):
new_files = list()
new_files.extend(self.files)
new_files.append(filee)
self.files = new_files
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(filee)
self.nodes = new_nodes
def delete_nodes(self, name):
for d in self.directories:
if name == d.name:
new_dirs = list()
new_dirs.extend(self.directories)
new_dirs.remove(d)
self.directories = new_dirs
for f in self.files:
if name == f.name:
new_files = list()
new_files.extend(self.files)
new_files.remove(f)
self.files = new_files
for n in self.nodes:
if name == n.name:
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.remove(n)
self.nodes = new_nodes
def get_names(self):
names = list()
for n in self.nodes:
names.append(n.name)
return names
class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError (FileSystemError):
pass
class DestinationNodeExistsError (FileSystemError):
pass
class NonEmptyDirectoryDeletionError (FileSystemError):
pass
class NonExplicitDirectoryDeletionError (FileSystemError):
pass
class DestinationNotADirectoryError (FileSystemError):
pass
class SourceNodeDoesNotExistError (NodeDoesNotExistError):
pass
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.root = Direc('/')
def get_paths(self, directory, last_dir, path):
if last_dir is '':
new_name = directory.name
elif last_dir is '/':
new_name = last_dir + directory.name
else:
new_name = last_dir + '/' + directory.name
if new_name not in path:
path[new_name] = directory
for f in directory.files:
if last_dir == '':
path['/' + f.name] = f
elif last_dir == '/':
path['/' + directory.name + '/' + f.name] = f
else:
path[last_dir + '/' + directory.name + '/' + f.name] = f
for d in directory.directories:
self.get_paths(d, new_name, path)
return path
def get_node(self, path):
paths = self.get_paths(self.root, '', dict())
if path in paths:
return paths[path]
else:
raise NodeDoesNotExistError()
def create_file(self, content, path, paths, node, current_dir, sub_dirs):
if self.available_size >= len(content) + 1:
if path not in paths:
if node is None:
node = Fil(sub_dirs[len(sub_dirs) - 1], content)
paths[current_dir].add_file(node)
self.available_size -= len(content) + 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
def create_dir(self, path, paths, node, sub_dirs, current_dir):
if self.available_size >= 1:
if path not in paths:
if node is None:
node = Direc(sub_dirs[len(sub_dirs) - 1])
paths[current_dir].add_directory(node)
self.available_size -= 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
def create(self, path, directory=False, content='', node=None):
paths = self.get_paths(self.root, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if current_dir in paths:
if type(paths[current_dir]) is Direc:
if directory:
self.create_dir(path, paths, node, sub_dirs, current_dir)
else:
self.create_file(content, path, paths, node,
current_dir, sub_dirs)
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
paths = self.get_paths(self.root, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if path in paths:
if type(paths[path]) is Direc and directory:
if len(paths[path].nodes) == 0 or force is True:
paths[current_dir].delete_nodes(
sub_dirs[len(sub_dirs) - 1])
else:
raise NonEmptyDirectoryDeletionError()
elif type(paths[path]) is Fil:
paths[current_dir].delete_nodes(sub_dirs[len(sub_dirs) - 1])
else:
raise NonExplicitDirectoryDeletionError()
else:
raise NodeDoesNotExistError()
def create_and_remove(self, sub_dirs, paths, current_dir, source,
destination):
if sub_dirs[len(sub_dirs) - 1] not in paths[destination].get_names():
new_destination = destination + '/' + paths[source].name
if type(paths[source]) is Direc:
self.create(
new_destination, directory=True, node=paths[source])
self.remove(source, directory=True)
else:
self.create(
new_destination, directory=False, node=paths[source])
self.remove(source, directory=False)
else:
raise DestinationNodeExistsError()
def move(self, source, destination):
paths = self.get_paths(self.root, '', dict())
sub_dirs = source.split('/')
destination_splitted = destination.split('/')
current_dir = destination[0:len(destination) - len(
destination_splitted[len(destination_splitted) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if source in paths:
if destination in paths:
if type(paths[destination]) is Direc:
self.create_and_remove(sub_dirs, paths, current_dir,
source, destination)
else:
raise DestinationNotADirectoryError()
else:
raise DestinationNodeDoesNotExistError()
else:
raise SourceNodeDoesNotExistError()

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

..EEEEE.E....E.EE.
======================================================================
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_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 18.173s

FAILED (errors=9)

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

Елица обнови решението на 30.04.2015 02:19 (преди над 9 години)

+class Filee:
+
+ def __init__(self, name, content):
+ self.name = name
+ self.content = content
+ self.size = len(content) + 1
+ self.is_directory = False
+
+ def append(self, text):
+ self.content.append(text)
+ self.size += len(text)
+
+ def truncate(self, text):
+ self.content = text
+ self.size = len(size) + 1
+
+
+class Direc:
+
+ def __init__(self, directory, files=list()):
+ self.name = directory
+ self.nodes = list()
+ self.directories = list()
+ self.files = files
+ self.is_directory = True
+
+ def addDirectory(self, directory):
+ new_directories = list()
+ new_directories.extend(self.directories)
+ new_directories.append(directory)
+ self.directories = new_directories
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.append(directory)
+ self.nodes = new_nodes
+
+ def addFile(self, filee):
+ new_files = list()
+ new_files.extend(self.files)
+ new_files.append(filee)
+ self.files = new_files
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.append(filee)
+ self.nodes = new_nodes
+
+ def deleteNodes(self, name):
+ for d in self.directories:
+ if name == d.name:
+ new_dirs = list()
+ new_dirs.extend(self.directories)
+ new_dirs.remove(d)
+ self.directories = new_dirs
+ for f in self.files:
+ if name == f.name:
+ new_files = list()
+ new_files.extend(self.files)
+ new_files.remove(f)
+ self.files = new_files
+ for n in self.nodes:
+ if name == n.name:
+ new_nodes = list()
+ new_nodes.extend(self.nodes)
+ new_nodes.remove(n)
+ self.nodes = new_nodes
+
+ def getAllNames(self):
+ names = list()
+ for n in self.nodes:
+ names.append(n.name)
+ return names
+
+
+class FileSystemError(Exception):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class NodeDoesNotExistError(FileSystemError):
+ pass
+
+
+class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
+ pass
+
+
+class NotEnoughSpaceError (FileSystemError):
+ pass
+
+
+class DestinationNodeExistsError (FileSystemError):
+ pass
+
+
+class NonEmptyDirectoryDeletionError (FileSystemError):
+ pass
+
+
+class NonExplicitDirectoryDeletionError (FileSystemError):
+ pass
+
+
+class DestinationNotADirectoryError (FileSystemError):
+ pass
+
+
+class SourceNodeDoesNotExistError (NodeDoesNotExistError):
+ pass
+
+
+class FileSystem:
+
+ def __init__(self, size):
+ self.size = size
+ self.available_size = size - 1
+ self.direc = Direc('/')
+
+ def get_paths(self, directory, last_dir, path):
+ if last_dir is '':
+ new_name = directory.name
+ elif last_dir is '/':
+ new_name = last_dir + directory.name
+ else:
+ new_name = last_dir + '/' + directory.name
+ if new_name not in path:
+ path[new_name] = directory
+ for f in directory.files:
+ if last_dir == '':
+ path['/' + f.name] = f
+ elif last_dir == '/':
+ path['/' + directory.name + '/' + f.name] = f
+ else:
+ path[last_dir + '/' + directory.name + '/' + f.name] = f
+ for d in directory.directories:
+ self.get_paths(d, new_name, path)
+ return path
+
+ def get_node(self, path):
+ paths = self.get_paths(self.direc, '', dict())
+ if path in paths:
+ return paths[path]
+ else:
+ raise NodeDoesNotExistError()
+
+ def createFile(self, content, path, paths, node, current_dir, sub_dirs):
+ if self.available_size >= len(content) + 1:
+ if path not in paths:
+ if node is None:
+ node = Filee(sub_dirs[len(sub_dirs) - 1], content)
+ paths[current_dir].addFile(node)
+ self.available_size -= len(content) + 1
+ else:
+ raise DestinationNodeExistsError()
+ else:
+ raise NotEnoughSpaceError()
+
+ def createDir(self, path, paths, node, sub_dirs, current_dir):
+ if self.available_size >= 1:
+ if path not in paths:
+ if node is None:
+ node = Direc(sub_dirs[len(sub_dirs) - 1])
+ paths[current_dir].addDirectory(node)
+ self.available_size -= 1
+ else:
+ raise DestinationNodeExistsError()
+ else:
+ raise NotEnoughSpaceError()
+
+ def create(self, path, directory=False, content='', node=None):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = path.split('/')
+ current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if current_dir in paths:
+ if type(paths[current_dir]) is Direc:
+ if directory:
+ self.createDir(path, paths, node, sub_dirs, current_dir)
+ else:
+ self.createFile(content, path, paths, node,
+ current_dir, sub_dirs)
+ else:
+ raise DestinationNodeDoesNotExistError
+
+ def remove(self, path, directory=False, force=True):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = path.split('/')
+ current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if path in paths:
+ if type(paths[path]) is Direc and directory:
+ if len(paths[path].nodes) == 0 or force is False:
+ paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ else:
+ raise NonEmptyDirectoryDeletionError()
+ elif type(paths[path]) is Filee:
+ paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ else:
+ raise NonExplicitDirectoryDeletionError()
+ else:
+ raise NodeDoesNotExistError()
+
+ def createAndRemove(self, sub_dirs, paths, current_dir, source, destination):
+ if sub_dirs[len(sub_dirs) - 1] not in paths[current_dir].getAllNames():
+ if type(paths[source]) is Direc:
+ self.create(destination, directory=True, node=paths[source])
+ self.remove(source, directory=True)
+ else:
+ self.create(destination, directory=False, node=paths[source])
+ self.remove(source, directory=False)
+ else:
+ raise DestinationNodeExistsError()
+
+ def move(self, source, destination):
+ paths = self.get_paths(self.direc, '', dict())
+ sub_dirs = source.split('/')
+ destination_splitted = destination.split('/')
+ current_dir = destination[0:len(destination) - len(
+ destination_splitted[len(destination_splitted) - 1])]
+ if current_dir != '/':
+ current_dir = current_dir[0:len(current_dir) - 1]
+ if source in paths:
+ if destination in paths:
+ if type(paths[destination]) is Direc:
+ self.createAndRemove(sub_dirs, paths, current_dir, source,
+ destination)
+ else:
+ raise DestinationNotADirectoryError()
+ else:
+ raise DestinationNodeDoesNotExistError()
+ else:
+ raise SourceNodeDoesNotExistError()

Елица обнови решението на 30.04.2015 02:20 (преди над 9 години)

class Filee:
def __init__(self, name, content):
self.name = name
self.content = content
self.size = len(content) + 1
self.is_directory = False
def append(self, text):
self.content.append(text)
self.size += len(text)
def truncate(self, text):
self.content = text
self.size = len(size) + 1
class Direc:
def __init__(self, directory, files=list()):
self.name = directory
self.nodes = list()
self.directories = list()
self.files = files
self.is_directory = True
def addDirectory(self, directory):
new_directories = list()
new_directories.extend(self.directories)
new_directories.append(directory)
self.directories = new_directories
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(directory)
self.nodes = new_nodes
def addFile(self, filee):
new_files = list()
new_files.extend(self.files)
new_files.append(filee)
self.files = new_files
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(filee)
self.nodes = new_nodes
def deleteNodes(self, name):
for d in self.directories:
if name == d.name:
new_dirs = list()
new_dirs.extend(self.directories)
new_dirs.remove(d)
self.directories = new_dirs
for f in self.files:
if name == f.name:
new_files = list()
new_files.extend(self.files)
new_files.remove(f)
self.files = new_files
for n in self.nodes:
if name == n.name:
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.remove(n)
self.nodes = new_nodes
def getAllNames(self):
names = list()
for n in self.nodes:
names.append(n.name)
return names
class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError (FileSystemError):
pass
class DestinationNodeExistsError (FileSystemError):
pass
class NonEmptyDirectoryDeletionError (FileSystemError):
pass
class NonExplicitDirectoryDeletionError (FileSystemError):
pass
class DestinationNotADirectoryError (FileSystemError):
pass
class SourceNodeDoesNotExistError (NodeDoesNotExistError):
pass
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
self.direc = Direc('/')
def get_paths(self, directory, last_dir, path):
if last_dir is '':
new_name = directory.name
elif last_dir is '/':
new_name = last_dir + directory.name
else:
new_name = last_dir + '/' + directory.name
if new_name not in path:
path[new_name] = directory
for f in directory.files:
if last_dir == '':
path['/' + f.name] = f
elif last_dir == '/':
path['/' + directory.name + '/' + f.name] = f
else:
path[last_dir + '/' + directory.name + '/' + f.name] = f
for d in directory.directories:
self.get_paths(d, new_name, path)
return path
def get_node(self, path):
paths = self.get_paths(self.direc, '', dict())
if path in paths:
return paths[path]
else:
raise NodeDoesNotExistError()
def createFile(self, content, path, paths, node, current_dir, sub_dirs):
if self.available_size >= len(content) + 1:
if path not in paths:
if node is None:
node = Filee(sub_dirs[len(sub_dirs) - 1], content)
paths[current_dir].addFile(node)
self.available_size -= len(content) + 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
def createDir(self, path, paths, node, sub_dirs, current_dir):
if self.available_size >= 1:
if path not in paths:
if node is None:
node = Direc(sub_dirs[len(sub_dirs) - 1])
paths[current_dir].addDirectory(node)
self.available_size -= 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
def create(self, path, directory=False, content='', node=None):
paths = self.get_paths(self.direc, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if current_dir in paths:
if type(paths[current_dir]) is Direc:
if directory:
self.createDir(path, paths, node, sub_dirs, current_dir)
else:
self.createFile(content, path, paths, node,
current_dir, sub_dirs)
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
paths = self.get_paths(self.direc, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if path in paths:
if type(paths[path]) is Direc and directory:
if len(paths[path].nodes) == 0 or force is False:
paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
else:
raise NonEmptyDirectoryDeletionError()
elif type(paths[path]) is Filee:
paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
else:
raise NonExplicitDirectoryDeletionError()
else:
raise NodeDoesNotExistError()
- def createAndRemove(self, sub_dirs, paths, current_dir, source, destination):
+ def createAndRemove(self, sub_dirs, paths, current_dir, source,
+ destination):
if sub_dirs[len(sub_dirs) - 1] not in paths[current_dir].getAllNames():
if type(paths[source]) is Direc:
self.create(destination, directory=True, node=paths[source])
self.remove(source, directory=True)
else:
self.create(destination, directory=False, node=paths[source])
self.remove(source, directory=False)
else:
raise DestinationNodeExistsError()
def move(self, source, destination):
paths = self.get_paths(self.direc, '', dict())
sub_dirs = source.split('/')
destination_splitted = destination.split('/')
current_dir = destination[0:len(destination) - len(
destination_splitted[len(destination_splitted) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if source in paths:
if destination in paths:
if type(paths[destination]) is Direc:
self.createAndRemove(sub_dirs, paths, current_dir, source,
destination)
else:
raise DestinationNotADirectoryError()
else:
raise DestinationNodeDoesNotExistError()
else:
raise SourceNodeDoesNotExistError()

Елица обнови решението на 30.04.2015 14:53 (преди над 9 години)

-class Filee:
+class Fil:
def __init__(self, name, content):
self.name = name
self.content = content
self.size = len(content) + 1
self.is_directory = False
def append(self, text):
- self.content.append(text)
+ self.content += text
self.size += len(text)
def truncate(self, text):
self.content = text
- self.size = len(size) + 1
+ self.size = len(text) + 1
class Direc:
def __init__(self, directory, files=list()):
self.name = directory
self.nodes = list()
self.directories = list()
self.files = files
self.is_directory = True
- def addDirectory(self, directory):
+ def add_directory(self, directory):
new_directories = list()
new_directories.extend(self.directories)
new_directories.append(directory)
self.directories = new_directories
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(directory)
self.nodes = new_nodes
- def addFile(self, filee):
+ def add_file(self, filee):
new_files = list()
new_files.extend(self.files)
new_files.append(filee)
self.files = new_files
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.append(filee)
self.nodes = new_nodes
- def deleteNodes(self, name):
+ def delete_nodes(self, name):
for d in self.directories:
if name == d.name:
new_dirs = list()
new_dirs.extend(self.directories)
new_dirs.remove(d)
self.directories = new_dirs
for f in self.files:
if name == f.name:
new_files = list()
new_files.extend(self.files)
new_files.remove(f)
self.files = new_files
for n in self.nodes:
if name == n.name:
new_nodes = list()
new_nodes.extend(self.nodes)
new_nodes.remove(n)
self.nodes = new_nodes
- def getAllNames(self):
+ def get_names(self):
names = list()
for n in self.nodes:
names.append(n.name)
return names
class FileSystemError(Exception):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class NodeDoesNotExistError(FileSystemError):
pass
class DestinationNodeDoesNotExistError(NodeDoesNotExistError):
pass
class NotEnoughSpaceError (FileSystemError):
pass
class DestinationNodeExistsError (FileSystemError):
pass
class NonEmptyDirectoryDeletionError (FileSystemError):
pass
class NonExplicitDirectoryDeletionError (FileSystemError):
pass
class DestinationNotADirectoryError (FileSystemError):
pass
class SourceNodeDoesNotExistError (NodeDoesNotExistError):
pass
class FileSystem:
def __init__(self, size):
self.size = size
self.available_size = size - 1
- self.direc = Direc('/')
+ self.root = Direc('/')
def get_paths(self, directory, last_dir, path):
if last_dir is '':
new_name = directory.name
elif last_dir is '/':
new_name = last_dir + directory.name
else:
new_name = last_dir + '/' + directory.name
if new_name not in path:
path[new_name] = directory
for f in directory.files:
if last_dir == '':
path['/' + f.name] = f
elif last_dir == '/':
path['/' + directory.name + '/' + f.name] = f
else:
path[last_dir + '/' + directory.name + '/' + f.name] = f
for d in directory.directories:
self.get_paths(d, new_name, path)
return path
def get_node(self, path):
- paths = self.get_paths(self.direc, '', dict())
+ paths = self.get_paths(self.root, '', dict())
if path in paths:
return paths[path]
else:
raise NodeDoesNotExistError()
- def createFile(self, content, path, paths, node, current_dir, sub_dirs):
+ def create_file(self, content, path, paths, node, current_dir, sub_dirs):
if self.available_size >= len(content) + 1:
if path not in paths:
if node is None:
- node = Filee(sub_dirs[len(sub_dirs) - 1], content)
- paths[current_dir].addFile(node)
+ node = Fil(sub_dirs[len(sub_dirs) - 1], content)
+ paths[current_dir].add_file(node)
self.available_size -= len(content) + 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
- def createDir(self, path, paths, node, sub_dirs, current_dir):
+ def create_dir(self, path, paths, node, sub_dirs, current_dir):
if self.available_size >= 1:
if path not in paths:
if node is None:
node = Direc(sub_dirs[len(sub_dirs) - 1])
- paths[current_dir].addDirectory(node)
+ paths[current_dir].add_directory(node)
self.available_size -= 1
else:
raise DestinationNodeExistsError()
else:
raise NotEnoughSpaceError()
def create(self, path, directory=False, content='', node=None):
- paths = self.get_paths(self.direc, '', dict())
+ paths = self.get_paths(self.root, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if current_dir in paths:
if type(paths[current_dir]) is Direc:
if directory:
- self.createDir(path, paths, node, sub_dirs, current_dir)
+ self.create_dir(path, paths, node, sub_dirs, current_dir)
else:
- self.createFile(content, path, paths, node,
- current_dir, sub_dirs)
+ self.create_file(content, path, paths, node,
+ current_dir, sub_dirs)
else:
raise DestinationNodeDoesNotExistError
def remove(self, path, directory=False, force=True):
- paths = self.get_paths(self.direc, '', dict())
+ paths = self.get_paths(self.root, '', dict())
sub_dirs = path.split('/')
current_dir = path[0:len(path) - len(sub_dirs[len(sub_dirs) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if path in paths:
if type(paths[path]) is Direc and directory:
- if len(paths[path].nodes) == 0 or force is False:
- paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ if len(paths[path].nodes) == 0 or force is True:
+ paths[current_dir].delete_nodes(
+ sub_dirs[len(sub_dirs) - 1])
else:
raise NonEmptyDirectoryDeletionError()
- elif type(paths[path]) is Filee:
- paths[current_dir].deleteNodes(sub_dirs[len(sub_dirs) - 1])
+ elif type(paths[path]) is Fil:
+ paths[current_dir].delete_nodes(sub_dirs[len(sub_dirs) - 1])
else:
raise NonExplicitDirectoryDeletionError()
else:
raise NodeDoesNotExistError()
- def createAndRemove(self, sub_dirs, paths, current_dir, source,
- destination):
- if sub_dirs[len(sub_dirs) - 1] not in paths[current_dir].getAllNames():
+ def create_and_remove(self, sub_dirs, paths, current_dir, source,
+ destination):
+ if sub_dirs[len(sub_dirs) - 1] not in paths[destination].get_names():
+ new_destination = destination + '/' + paths[source].name
if type(paths[source]) is Direc:
- self.create(destination, directory=True, node=paths[source])
+ self.create(
+ new_destination, directory=True, node=paths[source])
self.remove(source, directory=True)
else:
- self.create(destination, directory=False, node=paths[source])
+ self.create(
+ new_destination, directory=False, node=paths[source])
self.remove(source, directory=False)
else:
raise DestinationNodeExistsError()
def move(self, source, destination):
- paths = self.get_paths(self.direc, '', dict())
+ paths = self.get_paths(self.root, '', dict())
sub_dirs = source.split('/')
destination_splitted = destination.split('/')
current_dir = destination[0:len(destination) - len(
destination_splitted[len(destination_splitted) - 1])]
if current_dir != '/':
current_dir = current_dir[0:len(current_dir) - 1]
if source in paths:
if destination in paths:
if type(paths[destination]) is Direc:
- self.createAndRemove(sub_dirs, paths, current_dir, source,
- destination)
+ self.create_and_remove(sub_dirs, paths, current_dir,
+ source, destination)
else:
raise DestinationNotADirectoryError()
else:
raise DestinationNodeDoesNotExistError()
else:
raise SourceNodeDoesNotExistError()