Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sort_orders(clsname):
"""
Names of sort orders
`clsname` must be the name of a SorterBase derivative.
"""
cls = _utils.get_sorter_cls(clsname)
cands = (Candidate(name,
in_parens=', '.join(spec.aliases),
description=spec.description)
for name,spec in cls.SORTSPECS.items())
return Candidates(cands,
curarg_seps=(objects.localcfg['sort.torrents'].sep.strip(),),
label=_utils.sorters_labels[clsname])
if subtree is not None:
path_points_to_file = subtree.nodetype == 'leaf'
if path_points_to_file:
subtree = _utils.find_subtree(torrent, level_up(path))
if only == 'files':
cands = _utils.find_files(subtree)
return Candidates(cands, curarg_seps=('/',), label='Files in %s' % (subtree.path,))
elif only == 'directories':
cands = _utils.find_dirs(subtree)
return Candidates(cands, curarg_seps=('/',), label='Directories in %s' % (subtree.path,))
elif only == 'any':
return Candidates(subtree, curarg_seps=('/',), label=subtree.path)
elif only == 'auto':
if path_points_to_file:
return get_cands(torrent, level_up(path), 'files')
else:
return get_cands(torrent, level_up(path), 'directories')
else:
raise TypeError('Invalid value for argument "only": %r' % (only,))
def setting_names():
"""Names of settings"""
local_cands = (Candidate(name,
description=objects.localcfg.description(name),
default=str(objects.localcfg.default(name)))
for name in objects.localcfg)
remote_cands = (Candidate('srv.' + name,
description=objects.remotecfg.description(name),
default=str(objects.remotecfg.default(name)))
for name in objects.remotecfg)
return Candidates(itertools.chain(local_cands, remote_cands),
label='Settings')
def column_names(list_type):
"""
Columns names for a certain type of list
When `list_type` is appended to "columns.", it must resolve to a proper
setting.
"""
setting = objects.localcfg['columns.%s' % list_type]
cands = (Candidate(colname,
in_parens=', '.join(setting.aliases_inverse.get(colname, '')))
for colname in setting.options)
return Candidates(cands,
curarg_seps=(setting.sep.strip(),),
label=_utils.columns_labels[list_type])
def tab_titles():
"""Titles (strings) of TUI tabs"""
from ..tui.tuiobjects import tabs
return Candidates((widget.original_widget.text
for widget in tabs.titles), label='Tab Titles')
def flatten(iters):
# Only yield Candidates instances
for item in iters:
if item is None:
pass
elif isinstance(item, Candidates):
yield item
elif isinstance(item, abc.Iterable) and not isinstance(item, str):
yield from flatten(item)
else:
raise RuntimeError('Not a Candidates object or iterable of Candidates objects: %r' % (item,))
cats = tuple(flatten(cands))
The default implementation should work for all commands and it shouldn't
be necessary for subclasses to override this method.
"""
# '--' turns all arguments to the right into positional arguments
if '--' not in args.before_curarg:
if args.curarg.startswith('-'):
log.debug('Completing long options: %r', cls.long_options)
# Remove any options from candidates that are already in `args`
options = tuple(opt for opt in cls.long_options if opt not in args)
if options:
cands = (Candidate(cand,
in_parens=', '.join(cls.long_options[cand]),
description=_get_argspec(cls, cand)['description'])
for cand in options)
return Candidates(cands, label='%s options' % cls.name)
# Check if any argument left of the current argument is an option that
# wants another parameter
for i,arg in enumerate(reversed(args.before_curarg)):
if _is_option(cls, arg) and _option_wants_arg(cls, option=arg, roffset=i):
option_name = cls.short_options.get(arg, arg)
log.debug('Completing parameters for %r', option_name)
return cls.completion_candidates_params(option_name, args)
def get_candidates(args):
log.debug('Getting candidates for %r', args)
if args.curarg_index == 0:
log.debug('Completing command: %r', args[0])
cands = (Candidate(cmdcls.name,
description=cmdcls.description,
in_parens='%s' % (', '.join(cmdcls.aliases),))
for cmdcls in objects.cmdmgr.active_commands)
return Candidates(cands, label='Commands')
else:
cmdcls = objects.cmdmgr.get_cmdcls(args[0])
if cmdcls is not None:
log.debug(' Completing argument for %r', cmdcls.__name__)
return cmdcls.completion_candidates(args)
def get_cands(torrent, path, only):
subtree = _utils.find_subtree(torrent, path)
if subtree is not None:
path_points_to_file = subtree.nodetype == 'leaf'
if path_points_to_file:
subtree = _utils.find_subtree(torrent, level_up(path))
if only == 'files':
cands = _utils.find_files(subtree)
return Candidates(cands, curarg_seps=('/',), label='Files in %s' % (subtree.path,))
elif only == 'directories':
cands = _utils.find_dirs(subtree)
return Candidates(cands, curarg_seps=('/',), label='Directories in %s' % (subtree.path,))
elif only == 'any':
return Candidates(subtree, curarg_seps=('/',), label=subtree.path)
elif only == 'auto':
if path_points_to_file:
return get_cands(torrent, level_up(path), 'files')
else:
return get_cands(torrent, level_up(path), 'directories')
else:
raise TypeError('Invalid value for argument "only": %r' % (only,))