From 93685faa2ec0033b9092b54568c64adf6ea323d5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 24 Sep 2010 11:52:16 -0600 Subject: [PATCH] Plugin loading. Handle encoding declarations correctly --- src/calibre/customize/ui.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index 265b42bad2..231cc0e225 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -67,10 +67,17 @@ def load_plugin(path_to_zip_file): # {{{ if name.lower().endswith('plugin.py'): locals = {} raw = zf.read(name) - match = re.search(r'coding[:=]\s*([-\w.]+)', raw[:300]) - encoding = 'utf-8' - if match is not None: - encoding = match.group(1) + lines, encoding = raw.splitlines(), 'utf-8' + cr = re.compile(r'coding[:=]\s*([-\w.]+)') + raw = [] + for l in lines[:2]: + match = cr.search(l) + if match is not None: + encoding = match.group(1) + else: + raw.append(l) + raw += lines[2:] + raw = '\n'.join(raw) raw = raw.decode(encoding) raw = re.sub('\r\n', '\n', raw) exec raw in locals