diff --git a/weboob/frontends/videoob/videoob.py b/weboob/frontends/videoob/videoob.py index 345cfb44..e4655598 100644 --- a/weboob/frontends/videoob/videoob.py +++ b/weboob/frontends/videoob/videoob.py @@ -49,6 +49,6 @@ class Videoob(ConsoleApplication): @ConsoleApplication.command('Search videos') def command_search(self, pattern=None): self.load_modules(ICapVideo) - print (u'Search pattern: %s' % pattern if pattern else u'Last videos').encode('utf-8') + self.set_header(u'Search pattern: %s' % pattern if pattern else u'Last videos') for backend, video in self.weboob.do('iter_search_results', pattern=pattern, nsfw=self.options.nsfw): self.format(video, backend.name) diff --git a/weboob/frontends/weboorrents/weboorrents.py b/weboob/frontends/weboorrents/weboorrents.py index fffa47b5..0f27d053 100644 --- a/weboob/frontends/weboorrents/weboorrents.py +++ b/weboob/frontends/weboorrents/weboorrents.py @@ -81,6 +81,6 @@ class Weboorrents(ConsoleApplication): @ConsoleApplication.command('Search torrents') def command_search(self, pattern=None): - print u'Search pattern: %s' % pattern if pattern else u'Last torrents' + self.set_header(u'Search pattern: %s' % pattern if pattern else u'Last torrents') for backend, torrent in self.weboob.do('iter_torrents', pattern=pattern): self.format(torrent, backend.name) diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index babdbbf6..691add77 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -200,6 +200,9 @@ class ConsoleApplication(BaseApplication): def command(doc_string, f=register_command): return partial(f, doc_string=doc_string) + def set_header(self, string): + self.formatter.set_header(string) + def format(self, result, backend_name): try: self.formatter.format(result, backend_name, selected_fields=self.selected_fields, condition=self.condition) diff --git a/weboob/tools/application/formatters/__init__.py b/weboob/tools/application/formatters/__init__.py index da4beb75..07c46cf2 100644 --- a/weboob/tools/application/formatters/__init__.py +++ b/weboob/tools/application/formatters/__init__.py @@ -25,7 +25,7 @@ __all__ = ['formatters'] formatters = dict( multiline=MultilineFormatter(), simple=SimpleFormatter(), -) + ) try: from .table import TableFormatter @@ -33,5 +33,12 @@ try: table=TableFormatter(), htmltable=TableFormatter(result_funcname='get_html_string'), )) + try: + from .webkitgtk import WebkitGtkFormatter + formatters.update(dict( + webkit=WebkitGtkFormatter(), + )) + except ImportError: + pass except ImportError: pass diff --git a/weboob/tools/application/formatters/iformatter.py b/weboob/tools/application/formatters/iformatter.py index a9330cbf..bfa042ba 100644 --- a/weboob/tools/application/formatters/iformatter.py +++ b/weboob/tools/application/formatters/iformatter.py @@ -23,8 +23,9 @@ __all__ = ['IFormatter'] class IFormatter(object): - def __init__(self, display_keys=True): + def __init__(self, display_keys=True, return_only=False): self.display_keys = display_keys + self.return_only = return_only def after_format(self, formatted): raise NotImplementedError() @@ -32,7 +33,7 @@ class IFormatter(object): def flush(self): raise NotImplementedError() - def format(self, obj, backend_name, selected_fields=None, condition=None, return_only=False): + def format(self, obj, backend_name, selected_fields=None, condition=None): """ Format an object to be human-readable. An object has fields which can be selected, and the objects @@ -49,7 +50,7 @@ class IFormatter(object): if item is None: return None formatted = self.format_dict(item=item) - if not return_only and formatted: + if formatted: self.after_format(formatted) return formatted @@ -72,6 +73,9 @@ class IFormatter(object): if not isinstance(attribute, types.MethodType): yield attribute_name, attribute + def set_header(self, string): + raise NotImplementedError() + def to_dict(self, obj, backend_name, condition=None, selected_fields=None): def iter_select_and_decorate(d): if hasattr(obj, '__id__'): diff --git a/weboob/tools/application/formatters/multiline.py b/weboob/tools/application/formatters/multiline.py index 499b5a22..094c913d 100644 --- a/weboob/tools/application/formatters/multiline.py +++ b/weboob/tools/application/formatters/multiline.py @@ -36,3 +36,6 @@ class MultilineFormatter(IFormatter): def format_dict(self, item): return u'\n'.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v) for k, v in item.iteritems()) + self.after_item + + def set_header(self, string): + print string.encode('utf-8') diff --git a/weboob/tools/application/formatters/simple.py b/weboob/tools/application/formatters/simple.py index 3f8e32cc..299ad5da 100644 --- a/weboob/tools/application/formatters/simple.py +++ b/weboob/tools/application/formatters/simple.py @@ -36,3 +36,6 @@ class SimpleFormatter(IFormatter): def format_dict(self, item): return self.field_separator.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v) for k, v in item.iteritems()) + + def set_header(self, string): + print string.encode('utf-8') diff --git a/weboob/tools/application/formatters/table.py b/weboob/tools/application/formatters/table.py index 9b96316b..01e77db7 100644 --- a/weboob/tools/application/formatters/table.py +++ b/weboob/tools/application/formatters/table.py @@ -27,8 +27,10 @@ __all__ = ['TableFormatter'] class TableFormatter(IFormatter): column_headers = None queue = [] + header = None - def __init__(self, result_funcname='get_string'): + def __init__(self, display_keys=True, return_only=False, result_funcname='get_string'): + IFormatter.__init__(self, display_keys, return_only) self.result_funcname = result_funcname def after_format(self, formatted): @@ -39,13 +41,27 @@ class TableFormatter(IFormatter): def flush(self): if self.column_headers is None: return None + s = '' + if self.header: + if self.result_funcname == 'get_string': + s += self.header + elif self.result_funcname == 'get_html_string': + s+= '

%s

' % self.header + s += "\n" table = PrettyTable(self.column_headers) for column_header in self.column_headers: table.set_field_align(column_header, 'l') for line in self.queue: table.add_row(line) - print getattr(table, self.result_funcname)().encode('utf-8') + s += getattr(table, self.result_funcname)() + if self.return_only: + return s + else: + print s.encode('utf-8') def format_dict(self, item): # format is done in self.flush() by prettytable return item + + def set_header(self, string): + self.header = string diff --git a/weboob/tools/application/formatters/webkitgtk.py b/weboob/tools/application/formatters/webkitgtk.py new file mode 100644 index 00000000..589c914a --- /dev/null +++ b/weboob/tools/application/formatters/webkitgtk.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Christophe Benz +# +# This program 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, version 3 of the License. +# +# This program 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +import os + +import gtk +import webkit + +from ..javascript import get_javascript +from .table import TableFormatter + + +__all__ = ['WebkitGtkFormatter'] + + +class WebBrowser(gtk.Window): + def __init__(self): + gtk.Window.__init__(self) + self.connect('destroy', gtk.main_quit) + self.set_default_size(800, 600) + self.web_view = webkit.WebView() + sw = gtk.ScrolledWindow() + sw.add(self.web_view) + self.add(sw) + self.show_all() + + +class WebkitGtkFormatter(TableFormatter): + def __init__(self): + TableFormatter.__init__(self, return_only=True, result_funcname='get_html_string') + + def flush(self): + table_string = TableFormatter.flush(self) + js_filepaths = [] + js_filepaths.append(get_javascript('jquery')) + js_filepaths.append(get_javascript('tablesorter')) + scripts = ['' % js_filepath for js_filepath in js_filepaths] + html_string_params = dict(table=table_string) + if scripts: + html_string_params['scripts'] = ''.join(scripts) + html_string = """ + + + + + %(scripts)s + + + + + %(table)s + + +""" % html_string_params + web_browser = WebBrowser() + web_browser.web_view.load_html_string(html_string, 'file://%s' % os.path.abspath(os.getcwd())) + gtk.main() diff --git a/weboob/tools/application/javascript.py b/weboob/tools/application/javascript.py new file mode 100644 index 00000000..ecea679e --- /dev/null +++ b/weboob/tools/application/javascript.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2010 Christophe Benz +# +# This program 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, version 3 of the License. +# +# This program 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +import os + + +__all__ = ['get_javascript'] + + +def get_javascript(name, load_order=('local', 'web'), minified=True): + if name == 'jquery': + for src in load_order: + if src == 'local': + # try Debian paths + if minified: + filepath = '/usr/share/javascript/jquery/jquery.min.js' + else: + filepath = '/usr/share/javascript/jquery/jquery.js' + if os.path.exists(filepath): + return filepath + elif src == 'web': + # return Google-hosted URLs + if minified: + return 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' + else: + return 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js' + elif name == 'tablesorter': + if 'web' in load_order: + if minified: + return 'http://tablesorter.com/jquery.tablesorter.min.js' + else: + return 'http://tablesorter.com/jquery.tablesorter.js' + return None