class View: """View class to display informations. Attributes: _term (Terminal): blessings.Terminal object. _n_lines (int): Number of line currently displayed. """ def __init__(self): """Create terminal object and set nb of line displayed to 0.""" self._term = Terminal() self._n_lines = 0 def clear(self): """Clear the last view output.""" for i in range(self._n_lines): self._term.stream.write(self._term.clear_eol) self._term.stream.write(self._term.move_up) self._term.stream.write(self._term.clear_eol) self._n_lines = 0 def display(self, sessions: list): """Display informations from a list of session object. Args: sessions (list): The list of session object. """ output = "" for sess in sessions: sess_str = str(sess).format(t=self._term) + "\n" if sess.error: sess_str += "\t└── {}\n".format(sess.error) for i, gpu in enumerate(sess.gpus): is_last_gpu = i == len(sess.gpus) - 1 gpu_prefix = "└── " if is_last_gpu else "├── " sess_str += ("\t" + self._term.bright_black(gpu_prefix) + str(gpu).format(t=self._term) + "\n") for i, process in enumerate(gpu.processes): proc_prefix = "├── " if i < len( gpu.processes) - 1 else "└── " tab_prefix = "\t\t" if is_last_gpu else "\t│\t" sess_str += ( self._term.bright_black(tab_prefix + proc_prefix) + str(process).format(t=self._term) + "\n") output += sess_str self._term.stream.write(output) self._n_lines = output.count("\n")
def upload_torrents(): parser = ArgumentParser(description='Upload .torrent files to a Transmission server.') parser.add_argument('host', type=str, help='Transmission host[:port] (port defaults to 9091)') parser.add_argument('-u', '--user', type=str, default=getuser(), help='Transmission username (defaults to current user)') parser.add_argument('-d', '--directory', type=str, default='~/Downloads', help='directory to search for .torrent files (defaults to ~/Downloads)') parser.add_argument('-k', '--keep', action='store_true', help='do not trash .torrent files after uploading') args = parser.parse_args() t = Terminal() directory = Path(normpath(expanduser(expandvars(args.directory)))).resolve() print('\nScanning {} for {} files...'.format(t.bold(str(directory)), t.bold('.torrent')), end='') torrent_files = sorted(directory.glob('*.torrent')) if torrent_files: print(t.bold_bright_green(' Found {}'.format(len(torrent_files)))) else: print(t.bold_bright_red(' None found')) return password = getpass('\n{}\'s password: '******':' in args.host: hostname, port = args.host.split(':') client = Client(address=hostname, port=port, user=args.user, password=password) else: client = Client(address=args.host, user=args.user, password=password) print(t.bold_bright_green('Connected')) except TransmissionError as e: print(t.bold_bright_red('Connection failed') + ' to Transmission at ' + t.bold(args.host)) return uploaded, failed = [], [] # pad the index so that the brackets are all vertically aligned width = len(str(len(torrent_files))) for i, f in enumerate(torrent_files): prefix = ('[{:>{width}}]').format(i + 1, width=width) print('\n' + t.bold(prefix + ' Uploading') + '\t' + f.name) try: torrent = client.add_torrent('file://' + str(f)) uploaded.append(f) print(t.bold_bright_green(prefix + ' Started') + '\t' + t.bright_cyan(torrent.name)) except TransmissionError as e: failed.append(f) print(t.bold_bright_red(prefix + ' Error') + '\t' + t.bright_black(str(e))) if not args.keep: # convert to list to force iteration trash = lambda f: send2trash(str(f)) list(map(trash, uploaded)) print('') if uploaded: print('{} file{} uploaded successfully{}'.format( t.bold_bright_green(str(len(uploaded))), 's' if len(uploaded) != 1 else '', ' and moved to trash' if not args.keep else '' )) if failed: print('{} file{} failed to upload'.format( t.bold_bright_red(str(len(failed))), 's' if len(failed) != 1 else '' ))