From 136d1e4a192704bed8c7669e845729e5f9c05d73 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 3 Mar 2010 20:46:44 -0700 Subject: [PATCH] Ebook-viewer: Handle non-ascii CSS files when doing font substituitions --- src/calibre/ebooks/oeb/iterator.py | 12 ++++--- src/calibre/utils/network.py | 54 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/calibre/utils/network.py diff --git a/src/calibre/ebooks/oeb/iterator.py b/src/calibre/ebooks/oeb/iterator.py index cb62774e8d..87ce8683a9 100644 --- a/src/calibre/ebooks/oeb/iterator.py +++ b/src/calibre/ebooks/oeb/iterator.py @@ -152,13 +152,17 @@ class EbookIterator(object): prints('Substituting font family: %s -> %s'%(bad, good)) return match.group().replace(bad, '"%s"'%good) + from calibre.ebooks.chardet import force_encoding for csspath in css_files: with open(csspath, 'r+b') as f: css = f.read() - css = font_family_pat.sub(prepend_embedded_font, css) - f.seek(0) - f.truncate() - f.write(css) + enc = force_encoding(css, False) + css = css.decode(enc, 'replace') + ncss = font_family_pat.sub(prepend_embedded_font, css) + if ncss != css: + f.seek(0) + f.truncate() + f.write(ncss.encode(enc)) def __enter__(self, processed=False): self.delete_on_exit = [] diff --git a/src/calibre/utils/network.py b/src/calibre/utils/network.py new file mode 100644 index 0000000000..7e840207cf --- /dev/null +++ b/src/calibre/utils/network.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai +from __future__ import with_statement + +__license__ = 'GPL v3' +__copyright__ = '2010, Kovid Goyal ' +__docformat__ = 'restructuredtext en' + +from calibre.constants import iswindows, islinux, isfreebsd + +class LinuxNetworkStatus(object): + + def __init__(self): + try: + import dbus + bus = dbus.SystemBus() + proxy = bus.get_object("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager") + self.manager = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") + except: + self.manager = None + + def __call__(self): + if self.manager is None: + return True + try: + connections = self.manager.Get("org.freedesktop.NetworkManager", + "ActiveConnections") + return len(connections) > 0 + except: + return True + +class WindowsNetworkStatus(object): + + def __init__(self): + from calibre.constants import plugins + self.winutil = plugins['winutil'][0] + + def __call__(self): + if self.winutil is None: + return True + return self.winutil.internet_connected() + +class DummyNetworkStatus(object): + + def __call__(self): + return True + +_network_status = WindowsNetworkStatus() if iswindows else \ + LinuxNetworkStatus() if (islinux or isfreebsd) else \ + DummyNetworkStatus() + +def internet_connected(): + return _network_status()