new HTML formatter using webkit
This commit is contained in:
parent
b6cbd074b2
commit
5bbe266c8c
10 changed files with 178 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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__'):
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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+= '<p>%s</p>' % 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
|
||||
|
|
|
|||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue