new HTML formatter using webkit
This commit is contained in:
parent
b6cbd074b2
commit
5bbe266c8c
10 changed files with 178 additions and 8 deletions
|
|
@ -49,6 +49,6 @@ class Videoob(ConsoleApplication):
|
||||||
@ConsoleApplication.command('Search videos')
|
@ConsoleApplication.command('Search videos')
|
||||||
def command_search(self, pattern=None):
|
def command_search(self, pattern=None):
|
||||||
self.load_modules(ICapVideo)
|
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):
|
for backend, video in self.weboob.do('iter_search_results', pattern=pattern, nsfw=self.options.nsfw):
|
||||||
self.format(video, backend.name)
|
self.format(video, backend.name)
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,6 @@ class Weboorrents(ConsoleApplication):
|
||||||
|
|
||||||
@ConsoleApplication.command('Search torrents')
|
@ConsoleApplication.command('Search torrents')
|
||||||
def command_search(self, pattern=None):
|
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):
|
for backend, torrent in self.weboob.do('iter_torrents', pattern=pattern):
|
||||||
self.format(torrent, backend.name)
|
self.format(torrent, backend.name)
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,9 @@ class ConsoleApplication(BaseApplication):
|
||||||
def command(doc_string, f=register_command):
|
def command(doc_string, f=register_command):
|
||||||
return partial(f, doc_string=doc_string)
|
return partial(f, doc_string=doc_string)
|
||||||
|
|
||||||
|
def set_header(self, string):
|
||||||
|
self.formatter.set_header(string)
|
||||||
|
|
||||||
def format(self, result, backend_name):
|
def format(self, result, backend_name):
|
||||||
try:
|
try:
|
||||||
self.formatter.format(result, backend_name, selected_fields=self.selected_fields, condition=self.condition)
|
self.formatter.format(result, backend_name, selected_fields=self.selected_fields, condition=self.condition)
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ __all__ = ['formatters']
|
||||||
formatters = dict(
|
formatters = dict(
|
||||||
multiline=MultilineFormatter(),
|
multiline=MultilineFormatter(),
|
||||||
simple=SimpleFormatter(),
|
simple=SimpleFormatter(),
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .table import TableFormatter
|
from .table import TableFormatter
|
||||||
|
|
@ -33,5 +33,12 @@ try:
|
||||||
table=TableFormatter(),
|
table=TableFormatter(),
|
||||||
htmltable=TableFormatter(result_funcname='get_html_string'),
|
htmltable=TableFormatter(result_funcname='get_html_string'),
|
||||||
))
|
))
|
||||||
|
try:
|
||||||
|
from .webkitgtk import WebkitGtkFormatter
|
||||||
|
formatters.update(dict(
|
||||||
|
webkit=WebkitGtkFormatter(),
|
||||||
|
))
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,9 @@ __all__ = ['IFormatter']
|
||||||
|
|
||||||
|
|
||||||
class IFormatter(object):
|
class IFormatter(object):
|
||||||
def __init__(self, display_keys=True):
|
def __init__(self, display_keys=True, return_only=False):
|
||||||
self.display_keys = display_keys
|
self.display_keys = display_keys
|
||||||
|
self.return_only = return_only
|
||||||
|
|
||||||
def after_format(self, formatted):
|
def after_format(self, formatted):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
@ -32,7 +33,7 @@ class IFormatter(object):
|
||||||
def flush(self):
|
def flush(self):
|
||||||
raise NotImplementedError()
|
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.
|
Format an object to be human-readable.
|
||||||
An object has fields which can be selected, and the objects
|
An object has fields which can be selected, and the objects
|
||||||
|
|
@ -49,7 +50,7 @@ class IFormatter(object):
|
||||||
if item is None:
|
if item is None:
|
||||||
return None
|
return None
|
||||||
formatted = self.format_dict(item=item)
|
formatted = self.format_dict(item=item)
|
||||||
if not return_only and formatted:
|
if formatted:
|
||||||
self.after_format(formatted)
|
self.after_format(formatted)
|
||||||
return formatted
|
return formatted
|
||||||
|
|
||||||
|
|
@ -72,6 +73,9 @@ class IFormatter(object):
|
||||||
if not isinstance(attribute, types.MethodType):
|
if not isinstance(attribute, types.MethodType):
|
||||||
yield attribute_name, attribute
|
yield attribute_name, attribute
|
||||||
|
|
||||||
|
def set_header(self, string):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def to_dict(self, obj, backend_name, condition=None, selected_fields=None):
|
def to_dict(self, obj, backend_name, condition=None, selected_fields=None):
|
||||||
def iter_select_and_decorate(d):
|
def iter_select_and_decorate(d):
|
||||||
if hasattr(obj, '__id__'):
|
if hasattr(obj, '__id__'):
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,6 @@ class MultilineFormatter(IFormatter):
|
||||||
|
|
||||||
def format_dict(self, item):
|
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
|
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')
|
||||||
|
|
|
||||||
|
|
@ -36,3 +36,6 @@ class SimpleFormatter(IFormatter):
|
||||||
|
|
||||||
def format_dict(self, item):
|
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())
|
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')
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,10 @@ __all__ = ['TableFormatter']
|
||||||
class TableFormatter(IFormatter):
|
class TableFormatter(IFormatter):
|
||||||
column_headers = None
|
column_headers = None
|
||||||
queue = []
|
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
|
self.result_funcname = result_funcname
|
||||||
|
|
||||||
def after_format(self, formatted):
|
def after_format(self, formatted):
|
||||||
|
|
@ -39,13 +41,27 @@ class TableFormatter(IFormatter):
|
||||||
def flush(self):
|
def flush(self):
|
||||||
if self.column_headers is None:
|
if self.column_headers is None:
|
||||||
return None
|
return None
|
||||||
|
s = ''
|
||||||
|
if self.header:
|
||||||
|
if self.result_funcname == 'get_string':
|
||||||
|
s += self.header
|
||||||
|
elif self.result_funcname == 'get_html_string':
|
||||||
|
s+= '<p>%s</p>' % self.header
|
||||||
|
s += "\n"
|
||||||
table = PrettyTable(self.column_headers)
|
table = PrettyTable(self.column_headers)
|
||||||
for column_header in self.column_headers:
|
for column_header in self.column_headers:
|
||||||
table.set_field_align(column_header, 'l')
|
table.set_field_align(column_header, 'l')
|
||||||
for line in self.queue:
|
for line in self.queue:
|
||||||
table.add_row(line)
|
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):
|
def format_dict(self, item):
|
||||||
# format is done in self.flush() by prettytable
|
# format is done in self.flush() by prettytable
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
def set_header(self, string):
|
||||||
|
self.header = string
|
||||||
|
|
|
||||||
86
weboob/tools/application/formatters/webkitgtk.py
Normal file
86
weboob/tools/application/formatters/webkitgtk.py
Normal file
|
|
@ -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 = ['<script type="text/javascript" src="%s"></script>' % 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 = """
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
|
%(scripts)s
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<style type="text/css">
|
||||||
|
*
|
||||||
|
{
|
||||||
|
font-size: 10pt;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(function() {
|
||||||
|
$.each($("table"), function() {
|
||||||
|
var $this = $(this);
|
||||||
|
var thead = $('<thead>').append($this.find('tr:first'));
|
||||||
|
$this.append(thead);
|
||||||
|
$this.tablesorter();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
%(table)s
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
""" % html_string_params
|
||||||
|
web_browser = WebBrowser()
|
||||||
|
web_browser.web_view.load_html_string(html_string, 'file://%s' % os.path.abspath(os.getcwd()))
|
||||||
|
gtk.main()
|
||||||
48
weboob/tools/application/javascript.py
Normal file
48
weboob/tools/application/javascript.py
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue