def copy_to(self, other, overwrite=False, dereference_links=True, which_attributes={}): """ Copies the contents and attributes of this file or directory to the specified file. An exception will be thrown if the specified file already exists and overwrite is False. If dereference_links is True (the default), symbolic links encountered during copying will be dereferenced and their targets copied in their place. If dereference_links is False, such links will be recreated purely as symbolic links. Note that this could render some symbolic links broken. which_attributes is a dictionary indicating which attributes are to be copied, in the same format as that given to :obj:`copy_attributes_to`\ . """ # If self.is_folder is True, requires isinstance(self, Listable) and # isinstance(other, Hierarchy) when we implement support for folders. # Requires isinstance(other, Writable) always. # TODO: Use (and catch or pass on) proper exceptions here, EAFP style if other.exists: if overwrite: other.delete() else: raise generate(exceptions.FileExistsError, other) if dereference_links: source = self.dereference(True) else: source = self file_type = source.type if file_type is FILE: with other.open_for_writing() as write_to: for block in self.read_blocks(): write_to.write(block) elif file_type is FOLDER: other.create_folder() for child in self.children: child.copy_into(other, dereference_links=dereference_links, which_attributes=which_attributes) elif file_type is LINK: other.link_to(self.link_target) elif file_type is None: # TODO: This will happen when we're dereferencing links and we # find one that doesn't exist. Should we just ignore such a # situation and not copy anything, or should we copy the broken # link itself? raise generate(exceptions.FileNotFoundError, source) else: raise NotImplementedError(str(self)) source.copy_attributes_to(other, which_attributes=which_attributes)
def create_folder(self, ignore_existing=False, recursive=False): """ Creates the folder referred to by this File object. If it already exists but is not a folder, an exception will be thrown. If it already exists and is a folder, an exception will be thrown if ignore_existing is False (the default); if ignore_existing is True, no exception will be thrown. If the to-be-created folder's parent does not exist and recursive is False, an exception will be thrown. If recursive is True, the folder's parent, its parent's parent, and so on will be created automatically. """ # See if we're already a folder if self.is_folder: # We are. If ignore_existing is True, then just return. if ignore_existing: return # If it's not, raise an exception. TODO: EAFP else: raise generate(exceptions.FileExistsError, self) else: # We're not a folder. (We don't need to see if we already exist as # e.g. a file as the call to os.mkdir will take care of raising an # exception for us in such a case.) Now create our parent if it # doesn't exist and recursive is True. if recursive and self.parent and not self.parent.exists: self.parent.create_folder(recursive=True) # Now turn ourselves into a folder. os.mkdir(self.path)
def check_file(self): """ Checks to see whether this File refers to a file. If it doesn't, an exception will be thrown. """ if not self.is_file: raise generate(exceptions.FileNotFoundError, self)
def check_folder(self): """ Checks to see whether this File refers to a folder. If it doesn't, an exception will be thrown. """ if not self.is_folder: raise generate(exceptions.NotADirectoryError, self)
def delete(self, contents=False, ignore_missing=False): # If it's a mount point, unmount it before trying to delete it while self.is_mount: self.mountpoint.unmount(force=True) if not self.exists: if not ignore_missing: raise generate(exceptions.FileNotFoundError, self) elif self.is_folder and not self.is_link: for child in self.children: child.delete() os.rmdir(self.path) else: os.remove(self._path)