From d2b9a0a2d245571ee624bd6f7e811177e009ac3f Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Thu, 16 Jan 2014 23:56:42 +0100 Subject: [PATCH] move obj_to_filename() into ReplApplication and use it in weboorrents --- weboob/applications/videoob/videoob.py | 18 ----------- .../applications/weboorrents/weboorrents.py | 10 +++--- weboob/tools/application/repl.py | 32 +++++++++++++++++++ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/weboob/applications/videoob/videoob.py b/weboob/applications/videoob/videoob.py index 7fe26a2d..8a8ea719 100644 --- a/weboob/applications/videoob/videoob.py +++ b/weboob/applications/videoob/videoob.py @@ -22,7 +22,6 @@ import subprocess import sys import os -import re from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.base import empty @@ -75,22 +74,6 @@ class Videoob(ReplApplication): self.load_config() return ReplApplication.main(self, argv) - def obj_to_filename(self, obj, dest, default=None): - if default is None: - default = '{id}-{title}.{ext}' - if dest is None: - dest = '.' - if os.path.isdir(dest): - dest = os.path.join(dest, default) - - def repl(m): - field = m.group(1) - if hasattr(obj, field): - return re.sub('[?:/]', '-', '%s' % getattr(obj, field)) - else: - return m.group(0) - return re.sub(r'\{(.+?)\}', repl, dest) - def download(self, video, dest, default=None): if not video.url: print >>sys.stderr, 'Error: the direct URL is not available.' @@ -124,7 +107,6 @@ class Videoob(ReplApplication): os.spawnlp(os.P_WAIT, args[0], *args) - def complete_download(self, text, line, *ignored): args = line.split(' ') if len(args) == 2: diff --git a/weboob/applications/weboorrents/weboorrents.py b/weboob/applications/weboorrents/weboorrents.py index 6b1cf0e2..8e003e2f 100644 --- a/weboob/applications/weboorrents/weboorrents.py +++ b/weboob/applications/weboorrents/weboorrents.py @@ -140,13 +140,15 @@ class Weboorrents(ReplApplication): """ id, dest = self.parse_command_args(line, 2, 1) - _id, backend_name = self.parse_id(id) + torrent = self.get_object(id, 'get_torrent', ('description', 'files')) + if not torrent: + print >>sys.stderr, 'Torrent not found: %s' % id + return 3 - if dest is None: - dest = '%s.torrent' % _id + dest = self.obj_to_filename(torrent, dest, '{id}-{name}.torrent') try: - for backend, buf in self.do('get_torrent_file', _id, backends=backend_name): + for backend, buf in self.do('get_torrent_file', torrent.id, backends=torrent.backend): if buf: if dest == '-': print buf diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 1d9380fd..cf3c7513 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -22,6 +22,7 @@ import atexit from cmd import Cmd import logging import locale +import re from optparse import OptionGroup, OptionParser, IndentedHelpFormatter import os import sys @@ -1097,6 +1098,37 @@ class ReplApplication(Cmd, ConsoleApplication): obj_collections = [obj for obj in self.objects if isinstance(obj, BaseCollection)] return obj_collections + self.collections + def obj_to_filename(self, obj, dest=None, default=None): + """ + This method can be used to get a filename from an object, using a mask + filled by information of this object. + + All patterns are braces-enclosed, and are name of available fields in + the object. + + :param obj: object + :type obj: CapBaseObject + :param dest: dest given by user (default None) + :type dest: str + :param default: default file mask (if not given, this is '{id}-{title}.{ext}') + :type default: str + :rtype: str + """ + if default is None: + default = '{id}-{title}.{ext}' + if dest is None: + dest = '.' + if os.path.isdir(dest): + dest = os.path.join(dest, default) + + def repl(m): + field = m.group(1) + if hasattr(obj, field): + return re.sub('[?:/]', '-', '%s' % getattr(obj, field)) + else: + return m.group(0) + return re.sub(r'\{(.+?)\}', repl, dest) + # for cd & ls def complete_path(self, text, line, begidx, endidx): directories = set()