def convertToMappedPath(self, localPath): # takes a path like "/home/user/Download" # returns a mapped path like "X:/TDDOWNLOAD/" if localPath[-1] != "/": localPath += "/" bestMatchCount = -1 bestMatchDriveIndex = -1 localParts = pathSplit(localPath) for i, path in enumerate(self.mounts): # i -> 0, 1, 2... # path -> "/home/user/Download" parts = pathSplit(path) for m, (local, against) in enumerate(zip(localParts, parts)): if local == against: if m > bestMatchCount: bestMatchCount = m bestMatchDriveIndex = i else: break path = self.mounts[bestMatchDriveIndex] # not end with "/" if bestMatchDriveIndex >= 0: return "".join([ self.driveIndexToLetter(bestMatchDriveIndex), # "C:" "/TDDOWNLOAD", localPath[len(path):-1] # "may/be/sub/dir" ]) + "/" # always end with a / else: return None
def name(self): if not self._files: raise ValueError() if len(self._files) == 1: return os.path.basename(self._files[0]["path"]) else: if self._bittorrent: return self._bittorrent["info"]["name"] else: # when the torrent file/aria2 control file is gone # need to calculate fullpath of the base dir. pathParts = pathSplit(self._path) # "home", "user", "Downloads" fileParts = pathSplit(self._files[0]["path"]) # "home", "user", "Downloads", "foo" return fileParts[len(pathParts)]
def name(self): if not self._files: raise ValueError() if len(self._files) == 1: return os.path.basename(self._files[0]["path"]) else: if self._bittorrent: return self._bittorrent["info"]["name"] else: # when the torrent file/aria2 control file is gone # need to calculate fullpath of the base dir. pathParts = pathSplit( self._path) # "home", "user", "Downloads" fileParts = pathSplit( self._files[0] ["path"]) # "home", "user", "Downloads", "foo" return fileParts[len(pathParts)]
def fromCreation(self, creation: TaskCreation) -> bool: assert isinstance(creation, TaskCreation) # Clear any existing data. self.clear() assert not self._root assert self.rowCount() == 0 if not creation.isValid: return False, "Creation invalid." self._creation = creation try: if creation.kind == TaskCreationType.Emule: resolutions = resolveEd2k(creation.parsed) elif creation.kind in (TaskCreationType.RemoteTorrent, TaskCreationType.Normal): resolutions = resolveNormal(creation.parsed) elif creation.kind == TaskCreationType.LocalTorrent: with open(creation.url, "rb") as f: resolutions = resolveTorrentFile(f.read()) if not resolutions: return False, "Failed to resolve the torrent file." elif creation.kind == TaskCreationType.Magnet: resolutions = resolveMagnet(creation.parsed) if not resolutions: return False, "Failed to resolve magnet link." else: return False, "Creation type not implemented." except ValueError: return False, "Error resolving name and size." from .TaskTreeItem import TaskTreeItem root = TaskTreeItem() for i, resol in enumerate(resolutions): if len(resolutions) > 1 and \ creation.kind in (TaskCreationType.LocalTorrent, TaskCreationType.RemoteTorrent, TaskCreationType.Magnet): if pathSplit(resol.name)[-1].startswith("_____padding_file_"): continue root.addSubTask( name=resol.name, size=resol.size, index=i, selected=True, ) self.beginResetModel() self._root = root self.endResetModel() return True, None
def fromCreation(self, creation: TaskCreation) -> bool: assert isinstance(creation, TaskCreation) # Clear any existing data. self.clear() assert not self._root assert self.rowCount() == 0 if not creation.isValid: return False, "Creation invalid." self._creation = creation try: if creation.kind == TaskCreationType.Emule: resolutions = resolveEd2k(creation.parsed) elif creation.kind in (TaskCreationType.RemoteTorrent, TaskCreationType.Normal): resolutions = resolveNormal(creation.parsed) elif creation.kind == TaskCreationType.LocalTorrent: with open(creation.url, "rb") as f: resolutions = resolveTorrentFile(f.read()) if not resolutions: return False, "Failed to resolve the torrent file." elif creation.kind == TaskCreationType.Magnet: resolutions = resolveMagnet(creation.parsed) if not resolutions: return False, "Failed to resolve magnet link." else: return False, "Creation type not implemented." except ValueError: return False, "Error resolving name and size." from .TaskTreeItem import TaskTreeItem root = TaskTreeItem() for i, resol in enumerate(resolutions): if len(resolutions) > 1 and \ creation.kind in (TaskCreationType.LocalTorrent, TaskCreationType.RemoteTorrent, TaskCreationType.Magnet): if pathSplit(resol.name)[-1].startswith("_____padding_file_"): continue root.addSubTask( name = resol.name, size = resol.size, index = i, selected = True, ) self.beginResetModel() self._root = root self.endResetModel() return True, None
def _mountBootstrap(localPath): # local/path is the path that user sets # after bootstrapping, return the path to PROFILE/mnt/local\path # the filter(bool) part is to remove the "/" at the beginning backslashed = "\\".join(pathSplit(localPath)) mntDir = os.path.join(constants.PROFILE_DIR, "mnt", backslashed) tddownloadDir = os.path.join(mntDir, "TDDOWNLOAD") thunderdbDir = os.path.join(mntDir, "ThunderDB") tryMkdir(thunderdbDir) trySymlink(localPath, tddownloadDir) return mntDir