2009-05-07 18:26:12 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#
|
|
|
|
# gPodder - A media aggregator and podcast client
|
2010-01-02 17:35:42 +01:00
|
|
|
# Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
|
2009-05-07 18:26:12 +02:00
|
|
|
#
|
|
|
|
# gPodder is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gPodder is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
# gpo - A better command-line interface to gPodder using the gPodder API
|
|
|
|
# by Thomas Perl <thp@gpodder.org>; 2009-05-07
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
Usage: gpo [COMMAND] [params...]
|
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
- Subscription management -
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
subscribe URL [TITLE] Subscribe to a new feed at URL (as TITLE)
|
|
|
|
rename URL TITLE Rename feed at URL to TITLE
|
|
|
|
unsubscribe URL Unsubscribe from feed at URL
|
|
|
|
enable URL Enable feed updates for the feed at URL
|
|
|
|
disable URL Disable feed updates for the feed at URL
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
info URL Show information about feed at URL
|
|
|
|
list List all subscribed podcasts
|
|
|
|
update [URL] Check for new episodes (all or only at URL)
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
- Episode management -
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
download [URL] Download new episodes (all or only from URL)
|
|
|
|
pending [URL] List new episodes (all or only from URL)
|
|
|
|
episodes [URL] List episodes (all or only from URL)
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2011-02-25 00:45:49 +01:00
|
|
|
importfiles Check download folders for external files
|
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
- Other commands -
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
youtube [URL] Resolve the YouTube URL to a download URL
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2010-10-10 22:44:22 +02:00
|
|
|
import re
|
2009-05-07 18:26:12 +02:00
|
|
|
import inspect
|
2011-02-25 17:04:31 +01:00
|
|
|
import readline
|
2011-02-25 17:37:50 +01:00
|
|
|
import shlex
|
2011-02-25 17:57:23 +01:00
|
|
|
import pydoc
|
|
|
|
|
|
|
|
import termios
|
|
|
|
import fcntl
|
|
|
|
import struct
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2009-05-07 21:27:00 +02:00
|
|
|
gpodder_script = sys.argv[0]
|
|
|
|
if os.path.islink(gpodder_script):
|
|
|
|
gpodder_script = os.readlink(gpodder_script)
|
|
|
|
gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
|
2009-05-07 18:26:12 +02:00
|
|
|
prefix = os.path.abspath(os.path.normpath(gpodder_dir))
|
|
|
|
|
|
|
|
src_dir = os.path.join(prefix, 'src')
|
|
|
|
data_dir = os.path.join(prefix, 'data')
|
|
|
|
|
|
|
|
if os.path.exists(src_dir) and os.path.exists(data_dir) and \
|
|
|
|
not prefix.startswith('/usr'):
|
|
|
|
# Run gPodder from local source folder (not installed)
|
|
|
|
sys.path.insert(0, src_dir)
|
|
|
|
|
|
|
|
|
|
|
|
import gpodder
|
|
|
|
_ = gpodder.gettext
|
|
|
|
|
|
|
|
# Use only the gPodder API here, so this serves both as an example
|
|
|
|
# and as a motivation to provide all functionality in the API :)
|
|
|
|
from gpodder import api
|
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
def inred(x):
|
|
|
|
return '\033[91m' + x + '\033[0m'
|
|
|
|
|
|
|
|
def ingreen(x):
|
|
|
|
return '\033[92m' + x + '\033[0m'
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
def inblue(x):
|
|
|
|
return '\033[94m' + x + '\033[0m'
|
2009-05-07 18:26:12 +02:00
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
def FirstArgumentIsPodcastURL(function):
|
|
|
|
"""Decorator for functions that take a podcast URL as first arg"""
|
|
|
|
setattr(function, '_first_arg_is_podcast', True)
|
|
|
|
return function
|
|
|
|
|
2011-02-25 17:57:23 +01:00
|
|
|
def get_terminal_size():
|
|
|
|
s = struct.pack('HHHH', 0, 0, 0, 0)
|
|
|
|
stdout = sys.stdout.fileno()
|
|
|
|
x = fcntl.ioctl(stdout, termios.TIOCGWINSZ, s)
|
|
|
|
rows, cols, xp, yp = struct.unpack('HHHH', x)
|
|
|
|
return rows, cols
|
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
class gPodderCli(object):
|
2010-10-10 22:44:22 +02:00
|
|
|
COLUMNS = 80
|
2011-02-25 17:04:31 +01:00
|
|
|
ALIASES = {
|
|
|
|
'?': 'help',
|
|
|
|
}
|
2010-10-10 22:44:22 +02:00
|
|
|
|
2009-08-24 18:11:58 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.client = api.PodcastClient()
|
2010-10-10 22:44:22 +02:00
|
|
|
self._current_action = ''
|
|
|
|
|
|
|
|
def _start_action(self, msg, *args):
|
|
|
|
line = msg % args
|
|
|
|
if len(line) > self.COLUMNS-7:
|
|
|
|
line = line[:self.COLUMNS-7-3] + '...'
|
|
|
|
else:
|
|
|
|
line = line + (' '*(self.COLUMNS-7-len(line)))
|
|
|
|
self._current_action = line
|
|
|
|
sys.stdout.write(line)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def _update_action(self, progress):
|
|
|
|
progress = '%3.0f%%' % (progress*100.,)
|
|
|
|
result = '['+inblue(progress)+']'
|
|
|
|
sys.stdout.write('\r' + self._current_action + result)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
def _finish_action(self, success=True):
|
|
|
|
result = '['+ingreen('DONE')+']' if success else '['+inred('FAIL')+']'
|
|
|
|
print '\r' + self._current_action + result
|
|
|
|
self._current_action = ''
|
2009-08-24 18:11:58 +02:00
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
# -------------------------------------------------------------------
|
|
|
|
|
|
|
|
def subscribe(self, url, title=None):
|
2009-08-24 18:11:58 +02:00
|
|
|
if self.client.get_podcast(url) is not None:
|
2009-05-07 18:26:12 +02:00
|
|
|
self._info(_('You are already subscribed to %s.' % url))
|
|
|
|
return True
|
|
|
|
|
2009-08-24 18:11:58 +02:00
|
|
|
if self.client.create_podcast(url, title) is None:
|
2009-05-07 18:26:12 +02:00
|
|
|
self._error(_('Cannot download feed for %s.') % url)
|
|
|
|
return True
|
|
|
|
|
2009-08-24 18:11:58 +02:00
|
|
|
self.client.finish()
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
self._info(_('Successfully added %s.' % url))
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2009-05-07 18:26:12 +02:00
|
|
|
def rename(self, url, title):
|
2009-08-24 18:11:58 +02:00
|
|
|
podcast = self.client.get_podcast(url)
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
if podcast is None:
|
|
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
|
|
else:
|
|
|
|
old_title = podcast.title
|
|
|
|
podcast.rename(title)
|
2009-08-24 18:11:58 +02:00
|
|
|
self.client.finish()
|
2009-05-07 18:26:12 +02:00
|
|
|
self._info(_('Renamed %s to %s.') % (old_title, title))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2009-05-07 18:26:12 +02:00
|
|
|
def unsubscribe(self, url):
|
2009-08-24 18:11:58 +02:00
|
|
|
podcast = self.client.get_podcast(url)
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
if podcast is None:
|
|
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
|
|
else:
|
|
|
|
podcast.delete()
|
2009-08-24 18:11:58 +02:00
|
|
|
self.client.finish()
|
2009-05-07 18:26:12 +02:00
|
|
|
self._error(_('Unsubscribed from %s.') % url)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2010-10-02 11:36:23 +02:00
|
|
|
def _episodesList(self, podcast):
|
|
|
|
def status_str(episode):
|
|
|
|
if episode.is_new:
|
|
|
|
return ' * '
|
|
|
|
if episode.is_downloaded:
|
|
|
|
return ' ▉ '
|
|
|
|
if episode.is_deleted:
|
|
|
|
return ' ░ '
|
|
|
|
|
|
|
|
return ' '
|
|
|
|
|
|
|
|
episodes = ('%3d. %s %s' % (i+1, status_str(e), e.title) for i, e in enumerate(podcast.get_episodes()))
|
|
|
|
return episodes
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2009-05-07 18:26:12 +02:00
|
|
|
def info(self, url):
|
2009-08-24 18:11:58 +02:00
|
|
|
podcast = self.client.get_podcast(url)
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
if podcast is None:
|
|
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
|
|
else:
|
2010-10-02 11:36:23 +02:00
|
|
|
title, url, status = podcast.title, podcast.url, podcast.feed_update_status_msg()
|
|
|
|
episodes = self._episodesList(podcast)
|
2009-05-07 18:26:12 +02:00
|
|
|
episodes = '\n '.join(episodes)
|
2011-02-25 17:57:23 +01:00
|
|
|
self._pager("""
|
2009-05-07 18:26:12 +02:00
|
|
|
Title: %(title)s
|
|
|
|
URL: %(url)s
|
2010-10-02 11:36:23 +02:00
|
|
|
Feed update is %(status)s
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
Episodes:
|
|
|
|
%(episodes)s
|
2011-02-25 17:57:23 +01:00
|
|
|
""" % locals())
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-10-02 11:36:23 +02:00
|
|
|
def episodes(self, url=None):
|
2011-02-25 19:11:46 +01:00
|
|
|
output = []
|
2010-10-02 11:36:23 +02:00
|
|
|
for podcast in self.client.get_podcasts():
|
|
|
|
podcast_printed = False
|
|
|
|
if url is None or podcast.url == url:
|
|
|
|
episodes = self._episodesList(podcast)
|
|
|
|
episodes = '\n '.join(episodes)
|
2011-02-25 19:11:46 +01:00
|
|
|
output.append("""
|
2010-10-02 11:36:23 +02:00
|
|
|
Episodes from %s:
|
|
|
|
%s
|
2011-02-25 19:11:46 +01:00
|
|
|
""" % (podcast.url, episodes))
|
|
|
|
|
|
|
|
self._pager('\n'.join(output))
|
2010-10-02 11:36:23 +02:00
|
|
|
return True
|
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
def list(self):
|
2009-08-24 18:11:58 +02:00
|
|
|
for podcast in self.client.get_podcasts():
|
2009-05-07 18:26:12 +02:00
|
|
|
print podcast.url
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-09-27 00:22:13 +02:00
|
|
|
def update(self, url=None):
|
2010-10-02 11:36:23 +02:00
|
|
|
for podcast in self.client.get_podcasts():
|
|
|
|
if url is None and podcast.update_enabled():
|
2010-10-10 22:44:22 +02:00
|
|
|
self._start_action('Updating %s', podcast.title)
|
2011-01-12 13:23:23 +01:00
|
|
|
try:
|
|
|
|
podcast.update()
|
|
|
|
self._finish_action()
|
|
|
|
except Exception, e:
|
|
|
|
self._finish_action(False)
|
2010-10-02 11:36:23 +02:00
|
|
|
elif podcast.url == url:
|
2010-10-10 22:44:22 +02:00
|
|
|
# Don't need to check for update_enabled()
|
|
|
|
self._start_action('Updating %s', podcast.title)
|
2011-01-12 13:23:23 +01:00
|
|
|
try:
|
|
|
|
podcast.update()
|
|
|
|
self._finish_action()
|
|
|
|
except Exception, e:
|
|
|
|
self._finish_action(False)
|
2009-05-10 23:16:48 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-10-02 11:36:23 +02:00
|
|
|
def pending(self, url=None):
|
2009-05-10 23:16:48 +02:00
|
|
|
count = 0
|
2009-08-24 18:11:58 +02:00
|
|
|
for podcast in self.client.get_podcasts():
|
2009-05-10 23:16:48 +02:00
|
|
|
podcast_printed = False
|
2010-10-02 11:36:23 +02:00
|
|
|
if url is None or podcast.url == url:
|
|
|
|
for episode in podcast.get_episodes():
|
|
|
|
if episode.is_new:
|
|
|
|
if not podcast_printed:
|
|
|
|
print podcast.title
|
|
|
|
podcast_printed = True
|
|
|
|
print ' ', episode.title
|
|
|
|
count += 1
|
2009-05-10 23:16:48 +02:00
|
|
|
|
|
|
|
print count, 'episodes pending.'
|
|
|
|
return True
|
|
|
|
|
2011-02-25 00:45:49 +01:00
|
|
|
def importfiles(self):
|
|
|
|
full_count = 0
|
|
|
|
|
|
|
|
for podcast in self.client.get_podcasts():
|
|
|
|
print 'Checking for files in', podcast.title
|
|
|
|
count = podcast._podcast.import_external_files()
|
|
|
|
if count:
|
|
|
|
print ' => found %d valid file(s)' % count
|
|
|
|
full_count += count
|
|
|
|
|
|
|
|
print '%d downloaded file(s) imported.' % full_count
|
|
|
|
self.client.finish()
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-10-02 11:36:23 +02:00
|
|
|
def download(self, url=None):
|
2009-05-10 23:16:48 +02:00
|
|
|
count = 0
|
2009-08-24 18:11:58 +02:00
|
|
|
for podcast in self.client.get_podcasts():
|
2009-05-10 23:16:48 +02:00
|
|
|
podcast_printed = False
|
2010-10-02 11:36:23 +02:00
|
|
|
if url is None or podcast.url == url:
|
|
|
|
for episode in podcast.get_episodes():
|
|
|
|
if episode.is_new:
|
|
|
|
if not podcast_printed:
|
2010-10-10 22:44:22 +02:00
|
|
|
print inblue(podcast.title)
|
2010-10-02 11:36:23 +02:00
|
|
|
podcast_printed = True
|
2010-10-10 22:44:22 +02:00
|
|
|
self._start_action('Downloading %s', episode.title)
|
|
|
|
episode.download(self._update_action)
|
|
|
|
self._finish_action()
|
2010-10-02 11:36:23 +02:00
|
|
|
count += 1
|
2009-05-10 23:16:48 +02:00
|
|
|
|
|
|
|
print count, 'episodes downloaded.'
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-10-02 11:36:23 +02:00
|
|
|
def disable(self, url):
|
|
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
|
|
|
|
if podcast is None:
|
|
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
|
|
else:
|
|
|
|
podcast.disable()
|
|
|
|
self.client.finish()
|
|
|
|
self._error(_('Disabling feed update from %s.') % url)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
@FirstArgumentIsPodcastURL
|
2010-10-02 11:36:23 +02:00
|
|
|
def enable(self, url):
|
|
|
|
podcast = self.client.get_podcast(url)
|
|
|
|
|
|
|
|
if podcast is None:
|
|
|
|
self._error(_('You are not subscribed to %s.') % url)
|
|
|
|
else:
|
|
|
|
podcast.enable()
|
|
|
|
self.client.finish()
|
|
|
|
self._error(_('Enabling feed update from %s.') % url)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
def youtube(self, url):
|
2010-10-02 11:36:23 +02:00
|
|
|
yurl = self.client.youtube_url_resolver(url)
|
2010-10-10 22:44:22 +02:00
|
|
|
print yurl
|
2009-08-13 21:03:01 +02:00
|
|
|
return True
|
2009-05-10 23:16:48 +02:00
|
|
|
|
2011-02-25 17:04:31 +01:00
|
|
|
def help(self):
|
|
|
|
sys.stderr.write(stylize(__doc__))
|
|
|
|
return True
|
|
|
|
|
2011-02-25 17:14:21 +01:00
|
|
|
# -------------------------------------------------------------------
|
|
|
|
|
2011-02-25 17:57:23 +01:00
|
|
|
def _pager(self, output):
|
|
|
|
if sys.stdout.isatty():
|
|
|
|
# Need two additional rows for command prompt
|
|
|
|
rows_needed = len(output.splitlines()) + 2
|
|
|
|
rows, cols = get_terminal_size()
|
|
|
|
if rows_needed < rows:
|
|
|
|
print output
|
|
|
|
else:
|
|
|
|
pydoc.pager(output)
|
|
|
|
else:
|
|
|
|
print output
|
|
|
|
|
2011-02-25 17:14:21 +01:00
|
|
|
def _shell(self):
|
2011-02-25 17:04:31 +01:00
|
|
|
print '\n'.join(x.strip() for x in ("""
|
2011-02-25 19:01:27 +01:00
|
|
|
gPodder %(__version__)s (%(__date__)s) - %(__url__)s
|
2011-02-25 17:04:31 +01:00
|
|
|
%(__copyright__)s
|
|
|
|
License: %(__licence__)s
|
|
|
|
|
2011-02-25 19:01:27 +01:00
|
|
|
Entering interactive shell. Type 'help' for help.
|
|
|
|
Press Ctrl+D (EOF) or type 'quit' to quit.
|
2011-02-25 17:04:31 +01:00
|
|
|
""" % gpodder.__dict__).splitlines())
|
2011-02-25 17:14:21 +01:00
|
|
|
|
|
|
|
readline.parse_and_bind('tab: complete')
|
|
|
|
readline.set_completer(self._tab_completion)
|
2011-02-25 17:37:50 +01:00
|
|
|
readline.set_completer_delims(' ')
|
2011-02-25 17:14:21 +01:00
|
|
|
|
2011-02-25 17:04:31 +01:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
line = raw_input('gpo> ')
|
|
|
|
except EOFError:
|
2011-02-25 18:08:01 +01:00
|
|
|
print ''
|
2011-02-25 17:04:31 +01:00
|
|
|
break
|
2011-02-25 18:08:01 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print ''
|
|
|
|
continue
|
2011-02-25 17:04:31 +01:00
|
|
|
|
|
|
|
if line in ('quit', 'exit', 'bye'):
|
|
|
|
break
|
2011-02-25 18:08:01 +01:00
|
|
|
|
|
|
|
try:
|
2011-02-25 17:37:50 +01:00
|
|
|
self._parse(shlex.split(line))
|
2011-02-25 18:08:01 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
self._error('Keyboard interrupt.')
|
2011-02-25 17:04:31 +01:00
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
def _error(self, *args):
|
2010-10-10 22:44:22 +02:00
|
|
|
print >>sys.stderr, inred(' '.join(args))
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
def _info(self, *args):
|
|
|
|
print >>sys.stdout, ' '.join(args)
|
|
|
|
|
|
|
|
def _checkargs(self, func, command_line):
|
|
|
|
args, varargs, keywords, defaults = inspect.getargspec(func)
|
|
|
|
args.pop(0) # Remove "self" from args
|
|
|
|
defaults = defaults or ()
|
|
|
|
minarg, maxarg = len(args)-len(defaults), len(args)
|
|
|
|
|
2011-02-25 17:04:31 +01:00
|
|
|
if len(command_line) < minarg or (len(command_line) > maxarg and \
|
|
|
|
varargs is None):
|
2009-05-07 18:26:12 +02:00
|
|
|
self._error('Wrong argument count for %s.' % func.__name__)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return func(*command_line)
|
|
|
|
|
2011-02-25 17:37:50 +01:00
|
|
|
def _tab_completion_podcast(self, text, count):
|
|
|
|
"""Tab completion for podcast URLs"""
|
|
|
|
urls = [p.url for p in self.client.get_podcasts() if text in p.url]
|
|
|
|
if count < len(urls):
|
|
|
|
return urls[count]
|
|
|
|
|
|
|
|
return NOne
|
|
|
|
|
|
|
|
|
2011-02-25 17:14:21 +01:00
|
|
|
def _tab_completion(self, text, count):
|
|
|
|
"""Tab completion function for readline"""
|
2011-02-25 17:37:50 +01:00
|
|
|
current_line = readline.get_line_buffer()
|
|
|
|
if text == current_line:
|
|
|
|
for name, func in inspect.getmembers(self):
|
|
|
|
if inspect.ismethod(func) and \
|
|
|
|
name.startswith(text) and \
|
|
|
|
not name.startswith('_'):
|
|
|
|
if count == 0:
|
|
|
|
return name
|
|
|
|
else:
|
|
|
|
count -= 1
|
|
|
|
else:
|
|
|
|
args = current_line.split()
|
|
|
|
command = args.pop(0)
|
|
|
|
command_function = getattr(self, command, None)
|
|
|
|
if not command_function:
|
|
|
|
return None
|
|
|
|
if getattr(command_function, '_first_arg_is_podcast', False):
|
|
|
|
if not args or (len(args) == 1 and not current_line.endswith(' ')):
|
|
|
|
return self._tab_completion_podcast(text, count)
|
2011-02-25 17:14:21 +01:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
|
|
|
|
def _parse(self, command_line):
|
|
|
|
if not command_line:
|
|
|
|
return False
|
|
|
|
|
|
|
|
command = command_line.pop(0)
|
|
|
|
if command.startswith('_'):
|
|
|
|
self._error(_('This command is not available.'))
|
|
|
|
return False
|
|
|
|
|
2011-02-25 17:04:31 +01:00
|
|
|
# Resolve command aliases
|
|
|
|
command = self.ALIASES.get(command, command)
|
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
for name, func in inspect.getmembers(self):
|
|
|
|
if inspect.ismethod(func) and name == command:
|
|
|
|
return self._checkargs(func, command_line)
|
|
|
|
|
2010-10-02 11:36:23 +02:00
|
|
|
self._error(_('The requested function is not available.'))
|
2009-05-07 18:26:12 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2010-10-10 22:44:22 +02:00
|
|
|
def stylize(s):
|
|
|
|
s = re.sub(r' .{27}', lambda m: inblue(m.group(0)), s)
|
|
|
|
s = re.sub(r' - .*', lambda m: ingreen(m.group(0)), s)
|
|
|
|
return s
|
|
|
|
|
2009-05-07 18:26:12 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
cli = gPodderCli()
|
2011-02-25 17:04:31 +01:00
|
|
|
args = sys.argv[1:]
|
|
|
|
if args:
|
|
|
|
cli._parse(args) or sys.stderr.write(stylize(__doc__))
|
2011-02-25 19:04:51 +01:00
|
|
|
elif sys.stdout.isatty():
|
2011-02-25 17:14:21 +01:00
|
|
|
cli._shell()
|
2011-02-25 19:04:51 +01:00
|
|
|
else:
|
|
|
|
sys.stdout.write(__doc__)
|
2009-05-07 18:26:12 +02:00
|
|
|
|