def plugins_summary(manager, options): if options.table_type == 'porcelain': disable_colors() header = ['Keyword', 'Interfaces', 'Phases', 'Flags'] table = TerminalTable(*header, table_type=options.table_type) for plugin in sorted( get_plugins(phase=options.phase, interface=options.interface)): if options.builtins and not plugin.builtin: continue flags = [] if plugin.instance.__doc__: flags.append('doc') if plugin.builtin: flags.append('builtin') if plugin.debug: if not options.debug: continue flags.append('developers') handlers = plugin.phase_handlers roles = [] for phase in handlers: priority = handlers[phase].priority roles.append('{0}({1})'.format(phase, priority)) name = colorize('green', plugin.name) if 'builtin' in flags else plugin.name table.add_row(name, ', '.join(plugin.interfaces), ', '.join(roles), ', '.join(flags)) table.caption = colorize('green', ' Built-in plugins') table.caption_justify = 'left' console(table)
def action_status(options, irc_manager): connection = options.irc_connection try: status = irc_manager.status(connection) except ValueError as e: console('ERROR: %s' % e.args[0]) return header = ['Name', 'Alive', 'Channels', 'Server'] table_data = [] for connection in status: for name, info in connection.items(): alive = colorize('green', 'Yes') if info['alive'] else colorize( 'red', 'No') channels = [] for channel in info['channels']: for channel_name, channel_status in channel.items(): channels.append(channel_name) if channel_status == IRCChannelStatus.CONNECTED: channels[-1] = colorize('green', '* ' + channels[-1]) table_data.append([ name, alive, ', '.join(channels), '%s:%s' % (info['server'], info['port']) ]) table = TerminalTable(*header, table_type=options.table_type) for row in table_data: table.add_row(*row) console(table) console(colorize('green', ' * Connected channel'))
def list_file_templates(manager, options): header = [ 'Name', 'Use with', TerminalTable.Column('Full Path', overflow='fold'), TerminalTable.Column('Contents', overflow='ignore'), ] table = TerminalTable(*header, table_type=options.table_type, show_lines=True) console('Fetching all file templates, stand by...') for template_name in list_templates(extensions=['template']): if options.name and not options.name in template_name: continue template = get_template(template_name) if 'entries' in template_name: plugin = 'notify_entries' elif 'task' in template_name: plugin = 'notify_task' else: plugin = '-' name = template_name.replace('.template', '').split('/')[-1] with open(template.filename) as contents: table.add_row(name, plugin, template.filename, contents.read().strip()) console(table)
def action_list(options): with Session() as session: if not options.account: # Print all accounts accounts = session.query(db.TraktUserAuth).all() if not accounts: console('No trakt authorizations stored in database.') return header = ['Account', 'Created', 'Expires'] table = TerminalTable(*header, table_type=options.table_type) for auth in accounts: table.add_row( auth.account, auth.created.strftime('%Y-%m-%d'), auth.expires.strftime('%Y-%m-%d'), ) console(table) return # Show a specific account acc = (session.query(db.TraktUserAuth).filter( db.TraktUserAuth.account == options.account).first()) if acc: console('Authorization expires on %s' % acc.expires) else: console('Flexget has not been authorized to access your account.')
def entry_list_show(options): with Session() as session: try: entry_list = db.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format( options.list_name)) return try: entry = db.get_entry_by_id(entry_list.id, int(options.entry), session=session) except NoResultFound: console( 'Could not find matching entry with ID {} in list `{}`'.format( int(options.entry), options.list_name)) return except ValueError: entry = db.get_entry_by_title(entry_list.id, options.entry, session=session) if not entry: console( 'Could not find matching entry with title `{}` in list `{}`' .format(options.entry, options.list_name)) return table = TerminalTable('Field name', 'Value', table_type=options.table_type) for k, v in sorted(entry.entry.items()): table.add_row(k, str(v)) console(table)
def cli_search(options): search_term = ' '.join(options.keywords) tags = options.tags sources = options.sources query = re.sub(r'[ \(\)\:]+', ' ', search_term).strip() table_data = [] with Session() as session: for archived_entry in flexget.components.archive.db.search( session, query, tags=tags, sources=sources): days_ago = (datetime.now() - archived_entry.added).days source_names = ', '.join([s.name for s in archived_entry.sources]) tag_names = ', '.join([t.name for t in archived_entry.tags]) table_data.append(['ID', str(archived_entry.id)]) table_data.append(['Title', archived_entry.title]) table_data.append(['Added', str(days_ago) + ' days ago']) table_data.append(['URL', archived_entry.url]) table_data.append(['Source(s)', source_names or 'N/A']) table_data.append(['Tag(s)', tag_names or 'N/A']) if archived_entry.description: table_data.append( ['Description', strip_html(archived_entry.description)]) table_data.append([]) if not table_data: console('No results found for search') return table = TerminalTable('Field', 'Value', table_type=options.table_type) for row in table_data: table.add_row(*row) console(table)
def entry_list_lists(options): """Show all entry lists""" with Session() as session: lists = db.get_entry_lists(session=session) table = TerminalTable('#', 'List Name', table_type=options.table_type) for entry_list in lists: table.add_row(str(entry_list.id), entry_list.name) console(table)
def action_all(options): """Show all regexp lists""" lists = db.get_regexp_lists() header = ['#', 'List Name'] table = TerminalTable(*header, table_type=options.table_type) for regexp_list in lists: table.add_row(str(regexp_list.id), regexp_list.name) console(table)
def pending_list_lists(options): """Show all pending lists""" with Session() as session: lists = db.get_pending_lists(session=session) header = ['#', 'List Name'] table = TerminalTable(*header, table_type=options.table_type) for entry_list in lists: table.add_row(str(entry_list.id), entry_list.name) console(table)
def do_cli(manager, options): header = ['Name', 'Description'] table = TerminalTable(*header, table_type=options.table_type, show_lines=True) for filter_name, filter in get_filters().items(): if options.name and not options.name in filter_name: continue filter_doc = inspect.getdoc(filter) or '' table.add_row(filter_name, filter_doc) console(table)
def movie_list_lists(options): """Show all movie lists""" lists = db.get_movie_lists() header = ['#', 'List Name'] table = TerminalTable(*header, table_type=options.table_type) for movie_list in lists: table.add_row(str(movie_list.id), movie_list.name) console(table)
def list_rejected(options): with Session() as session: results = session.query(db.RememberEntry).all() header = ['#', 'Title', 'Task', 'Rejected by', 'Reason'] table = TerminalTable(*header, table_type=options.table_type) for entry in results: table.add_row(str(entry.id), entry.title, entry.task.name, entry.rejected_by, entry.reason or '') console(table)
def do_cli_summary(manager, options): header = [ 'Task', 'Last execution', 'Last success', 'Entries', 'Accepted', 'Rejected', 'Failed', 'Duration', ] table = TerminalTable(*header, table_type=options.table_type) with Session() as session: for task in session.query(db.StatusTask).all(): ok = (session.query(db.TaskExecution).filter( db.TaskExecution.task_id == task.id).filter( db.TaskExecution.succeeded == True).filter( db.TaskExecution.produced > 0).order_by( db.TaskExecution.start.desc()).first()) if ok is None: duration = None last_success = '-' else: duration = ok.end - ok.start last_success = ok.start.strftime('%Y-%m-%d %H:%M') age = datetime.datetime.utcnow() - ok.start if age > timedelta(days=7): last_success = colorize('red', last_success) elif age < timedelta(minutes=10): last_success = colorize('green', last_success) # Fix weird issue that a task registers StatusTask but without an execution. GH #2022 last_exec = (task.last_execution_time.strftime('%Y-%m-%d %H:%M') if task.last_execution_time else '-') table.add_row( task.name, last_exec, last_success, str(ok.produced) if ok is not None else '-', str(ok.accepted) if ok is not None else '-', str(ok.rejected) if ok is not None else '-', str(ok.failed) if ok is not None else '-', '%1.fs' % duration.total_seconds() if duration is not None else '-', ) console(table)
def seen_search(options, session=None): search_term = options.search_term if is_imdb_url(search_term): console('IMDB url detected, parsing ID') imdb_id = extract_id(search_term) if imdb_id: search_term = imdb_id else: console("Could not parse IMDB ID") else: search_term = '%' + options.search_term + '%' seen_entries = db.search(value=search_term, status=None, session=session) table = TerminalTable('Field', 'Value', table_type=options.table_type) for se in seen_entries.all(): table.add_row('Title', se.title) for sf in se.fields: if sf.field.lower() == 'title': continue table.add_row('{}'.format(sf.field.upper()), str(sf.value)) table.add_row('Task', se.task) table.add_row('Added', se.added.strftime('%Y-%m-%d %H:%M'), end_section=True) if not table.rows: console('No results found for search') return console(table)
def action_list(options): """List regexp list""" with Session() as session: regexp_list = db.get_list_by_exact_name(options.list_name) if not regexp_list: console('Could not find regexp list with name {}'.format( options.list_name)) return table = TerminalTable('Regexp', table_type=options.table_type) regexps = db.get_regexps_by_list_id(regexp_list.id, order_by='added', descending=True, session=session) for regexp in regexps: table.add_row(regexp.regexp or '') console(table)
def do_cli(manager, options): if options.action == 'clear': num = db.clear_entries(options.task, all=True) console('%s entries cleared from backlog.' % num) else: header = ['Title', 'Task', 'Expires'] table_data = [] with Session() as session: entries = db.get_entries(options.task, session=session) for entry in entries: table_data.append([ entry.title, entry.task, entry.expire.strftime('%Y-%m-%d %H:%M') ]) table = TerminalTable(*header, table_type=options.table_type) for row in table_data: table.add_row(*row) console(table)
def entry_list_list(options): """List entry list""" with Session() as session: try: entry_list = db.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find entry list with name {}'.format( options.list_name)) return header = ['#', 'Title', '# of fields'] table = TerminalTable(*header, table_type=options.table_type) for entry in db.get_entries_by_list_id(entry_list.id, order_by='added', descending=True, session=session): table.add_row(str(entry.id), entry.title, str(len(entry.entry))) console(table)
def list_failed(options): with Session() as session: results = session.query(db.FailedEntry).all() header = [ TerminalTable.Column('#', justify='center'), 'Title', 'Fail count', 'Reason', 'Failure time', ] table = TerminalTable(*header, table_type=options.table_type) for entry in results: table.add_row( str(entry.id), entry.title, str(entry.count), '' if entry.reason == 'None' else entry.reason, entry.tof.strftime('%Y-%m-%d %H:%M'), ) console(table)
def list_entries(options): """List pending entries""" approved = options.approved task_name = options.task_name with Session() as session: entries = db.list_pending_entries(session=session, task_name=task_name, approved=approved) header = ['#', 'Task Name', 'Title', 'URL', 'Approved', 'Added'] table = TerminalTable(*header, table_type=options.table_type) for entry in entries: table.add_row( str(entry.id), entry.task_name, entry.title, entry.url, colorize('green', 'Yes') if entry.approved else 'No', entry.added.strftime("%c"), ) console(table)
def pending_list_list(options): """List pending list entries""" with Session() as session: try: pending_list = db.get_list_by_exact_name(options.list_name, session=session) except NoResultFound: console('Could not find pending list with name `{}`'.format( options.list_name)) return header = ['#', 'Title', '# of fields', 'Approved'] table = TerminalTable(*header, table_type=options.table_type) for entry in db.get_entries_by_list_id(pending_list.id, order_by='added', descending=True, session=session): approved = colorize( 'green', entry.approved) if entry.approved else entry.approved table.add_row(str(entry.id), entry.title, str(len(entry.entry)), approved) console(table)
def do_cli_task(manager, options): header = [ 'Start', 'Duration', 'Entries', 'Accepted', 'Rejected', 'Failed', 'Abort Reason' ] table = TerminalTable(*header, table_type=options.table_type) with Session() as session: try: task = session.query(db.StatusTask).filter( db.StatusTask.name == options.task).one() except NoResultFound: console( 'Task name `%s` does not exists or does not have any records' % options.task) return else: query = task.executions.order_by(desc( db.TaskExecution.start))[:options.limit] for ex in reversed(query): start = ex.start.strftime('%Y-%m-%d %H:%M') start = colorize('green', start) if ex.succeeded else colorize( 'red', start) if ex.end is not None and ex.start is not None: delta = ex.end - ex.start duration = '%1.fs' % delta.total_seconds() else: duration = '?' table.add_row( start, duration, str(ex.produced), str(ex.accepted), str(ex.rejected), str(ex.failed), ex.abort_reason if ex.abort_reason is not None else '', ) console(table)
def seen_search(manager: Manager, options, session=None): search_term = options.search_term if is_imdb_url(search_term): console('IMDB url detected, parsing ID') imdb_id = extract_id(search_term) if imdb_id: search_term = imdb_id else: console("Could not parse IMDB ID") else: search_term = search_term.replace("%", "\\%").replace("_", "\\_") search_term = search_term.replace("*", "%").replace("?", "_") tasks = None if options.tasks: tasks = [] for task in options.tasks: try: tasks.extend(m for m in manager.matching_tasks(task) if m not in tasks) except ValueError as e: console(e) continue seen_entries = db.search(value=search_term, status=None, tasks=tasks, session=session) table = TerminalTable('Field', 'Value', table_type=options.table_type) for se in seen_entries.all(): table.add_row('Title', se.title) for sf in se.fields: if sf.field.lower() == 'title': continue table.add_row('{}'.format(sf.field.upper()), str(sf.value)) table.add_row('Task', se.task) if se.local: table.add_row('Local', 'Yes') table.add_row('Added', se.added.strftime('%Y-%m-%d %H:%M'), end_section=True) if not table.rows: console('No results found for search') return console(table)
def movie_list_list(options): """List movie list""" with Session() as session: try: movie_list = db.get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find movie list with name {}'.format( options.list_name)) return header = ['#', 'Movie Name', 'Movie year'] header += db.MovieListBase().supported_ids movies = db.get_movies_by_list_id(movie_list.id, order_by='added', descending=True, session=session) title = '{} Movies in movie list: `{}`'.format(len(movies), options.list_name) table = TerminalTable(*header, table_type=options.table_type, title=title) for movie in movies: movie_row = [str(movie.id), movie.title, str(movie.year) or ''] for identifier in db.MovieListBase().supported_ids: movie_row.append(str(movie.identifiers.get(identifier, ''))) table.add_row(*movie_row) console(table)
def do_cli(manager, options): with Session() as session: query = session.query(db.History) if options.search: search_term = options.search.replace(' ', '%').replace('.', '%') query = query.filter(db.History.title.like('%' + search_term + '%')) if options.task: query = query.filter(db.History.task.like('%' + options.task + '%')) query = query.order_by(desc(db.History.time)).limit(options.limit) if options.short: headers = ['Time', 'Title'] else: headers = ['Field', 'Value'] title = 'Showing {} entries from History'.format(query.count()) table = TerminalTable(*headers, table_type=options.table_type, title=title) for item in reversed(query.all()): if not options.short: table.add_row('Task', item.task) table.add_row('Title', item.title) table.add_row('URL', item.url) table.add_row('Time', item.time.strftime("%c")) table.add_row('Details', item.details) if item.filename: table.add_row('Stored', item.filename) table.rows[-1].end_section = True else: table.add_row(item.time.strftime("%c"), item.title) if not table.row_count: console('No history to display') return console(table)
def display_details(options): """Display detailed series information, ie. series show NAME""" name = options.series_name sort_by = options.sort_by or os.environ.get(ENV_SHOW_SORTBY_FIELD, 'age') if options.order is not None: reverse = True if options.order == 'desc' else False else: reverse = True if os.environ.get( ENV_SHOW_SORTBY_ORDER) == 'desc' else False with Session() as session: name = flexget.components.series.utils.normalize_series_name(name) # Sort by length of name, so that partial matches always show shortest matching title matches = db.shows_by_name(name, session=session) if not matches: console(colorize(ERROR_COLOR, 'ERROR: Unknown series `%s`' % name)) return # Pick the best matching series series = matches[0] table_title = series.name if len(matches) > 1: warning = ( colorize('red', ' WARNING: ') + 'Multiple series match to `{}`.\n ' 'Be more specific to see the results of other matches:\n\n' ' {}'.format(name, ', '.join(s.name for s in matches[1:]))) if not options.table_type == 'porcelain': console(warning) header = [ 'Identifier', 'Last seen', 'Release titles', 'Quality', 'Proper' ] table_data = [] entities = db.get_all_entities(series, session=session, sort_by=sort_by, reverse=reverse) for entity in entities: if not entity.releases: continue if entity.identifier is None: identifier = colorize(ERROR_COLOR, 'MISSING') age = '' else: identifier = entity.identifier age = entity.age entity_data = [identifier, age] release_titles = [] release_qualities = [] release_propers = [] for release in entity.releases: title = release.title quality = release.quality.name if not release.downloaded: title = colorize(UNDOWNLOADED_RELEASE_COLOR, title) quality = quality else: title += ' *' title = colorize(DOWNLOADED_RELEASE_COLOR, title) quality = quality release_titles.append(title) release_qualities.append(quality) release_propers.append( 'Yes' if release.proper_count > 0 else '') entity_data.append('\n'.join(release_titles)) entity_data.append('\n'.join(release_qualities)) entity_data.append('\n'.join(release_propers)) table_data.append(entity_data) footer = ' %s \n' % (colorize(DOWNLOADED_RELEASE_COLOR, '* Downloaded')) if not series.identified_by: footer += ( '\n Series plugin is still learning which episode numbering mode is \n' ' correct for this series (identified_by: auto).\n' ' Few duplicate downloads can happen with different numbering schemes\n' ' during this time.') else: footer += '\n `%s` uses `%s` mode to identify episode numbering.' % ( series.name, series.identified_by, ) begin_text = 'option' if series.begin: footer += ' \n Begin for `%s` is set to `%s`.' % ( series.name, series.begin.identifier) begin_text = 'and `begin` options' footer += ' \n See `identified_by` %s for more information.' % begin_text table = TerminalTable(*header, table_type=options.table_type, title=table_title) for row in table_data: table.add_row(*row) console(table) if not options.table_type == 'porcelain': console(footer)
def display_summary(options): """ Display series summary. :param options: argparse options from the CLI """ porcelain = options.table_type == 'porcelain' configured = options.configured or os.environ.get(ENV_LIST_CONFIGURED, 'configured') premieres = (True if (os.environ.get(ENV_LIST_PREMIERES) == 'yes' or options.premieres) else False) sort_by = options.sort_by or os.environ.get(ENV_LIST_SORTBY_FIELD, 'name') if options.order is not None: descending = True if options.order == 'desc' else False else: descending = True if os.environ.get( ENV_LIST_SORTBY_ORDER) == 'desc' else False with Session() as session: kwargs = { 'configured': configured, 'premieres': premieres, 'session': session, 'sort_by': sort_by, 'descending': descending, } if sort_by == 'name': kwargs['sort_by'] = 'show_name' else: kwargs['sort_by'] = 'last_download_date' query = db.get_series_summary(**kwargs) header = [ 'Name', 'Begin', 'Last Encountered', 'Age', 'Downloaded', 'Identified By' ] for index, value in enumerate(header): if value.lower() == options.sort_by: header[index] = colorize(SORT_COLUMN_COLOR, value) table = TerminalTable(*header, table_type=options.table_type) for series in query: name_column = series.name behind = (0, ) begin = series.begin.identifier if series.begin else '-' latest_release = '-' age_col = '-' episode_id = '-' latest = db.get_latest_release(series) identifier_type = series.identified_by if identifier_type == 'auto': identifier_type = colorize('yellow', 'auto') if latest: behind = db.new_entities_after(latest) latest_release = get_latest_status(latest) # colorize age age_col = latest.age if latest.age_timedelta is not None: if latest.age_timedelta < timedelta(days=1): age_col = colorize(NEW_EP_COLOR, latest.age) elif latest.age_timedelta < timedelta(days=3): age_col = colorize(FRESH_EP_COLOR, latest.age) elif latest.age_timedelta > timedelta(days=400): age_col = colorize(OLD_EP_COLOR, latest.age) episode_id = latest.identifier if not porcelain: if behind[0] > 0: name_column += colorize( BEHIND_EP_COLOR, ' {} {} behind'.format(behind[0], behind[1])) table.add_row(name_column, begin, episode_id, age_col, latest_release, identifier_type) console(table) if not porcelain: if not query.count(): console('Use `flexget series list all` to view all known series.') else: console( 'Use `flexget series show NAME` to get detailed information.')
def dump(entries, debug=False, eval_lazy=False, trace=False, title_only=False): """ Dump *entries* to stdout :param list entries: Entries to be dumped. :param bool debug: Print non printable fields as well. :param bool eval_lazy: Evaluate lazy fields. :param bool trace: Display trace information. :param bool title_only: Display only title field """ def sort_key(field): # Sort certain fields above the rest if field == 'title': return (0, ) if field == 'url': return (1, ) if field == 'original_url': return (2, ) return 3, field highlighter = ReprHighlighter() for entry in entries: entry_table = TerminalTable( 'field', ':', 'value', show_header=False, show_edge=False, pad_edge=False, collapse_padding=True, box=None, padding=0, ) for field in sorted(entry, key=sort_key): if field.startswith('_') and not debug: continue if title_only and field != 'title': continue if entry.is_lazy(field) and not eval_lazy: renderable = ( '[italic]<LazyField - value will be determined when it is accessed>[/italic]' ) else: try: value = entry[field] except KeyError: renderable = '[italic]<LazyField - lazy lookup failed>[/italic]' else: if field.rsplit('_', maxsplit=1)[-1] == 'url': renderable = f'[link={value}][repr.url]{value}[/repr.url][/link]' elif isinstance(value, str): renderable = value.replace('\r', '').replace('\n', '') elif is_expandable(value): renderable = Pretty(value) else: try: renderable = highlighter(str(value)) except Exception: renderable = f'[[i]not printable[/i]] ({repr(value)})' entry_table.add_row(f'{field}', ': ', renderable) console(entry_table) if trace: console('── Processing trace:', style='italic') trace_table = TerminalTable( 'Plugin', 'Operation', 'Message', show_edge=False, pad_edge=False, ) for item in entry.traces: trace_table.add_row(item[0], '' if item[1] is None else item[1], item[2]) console(trace_table) if not title_only: console('')