From 79a60b3aa027f033e0844c3f43cfa8b290f0c313 Mon Sep 17 00:00:00 2001 From: Paul Goins Date: Tue, 7 Dec 2010 23:58:27 +0900 Subject: [PATCH] Updated kd2.py and helpers.py to allow for unicode objects as search input. --- jblite/helpers.py | 17 +++++++++++++++++ jblite/kd2.py | 12 ++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/jblite/helpers.py b/jblite/helpers.py index 92ab7c9..cc0319c 100644 --- a/jblite/helpers.py +++ b/jblite/helpers.py @@ -40,3 +40,20 @@ def do_time(fn, *args, **kwargs): end = time.time() print("do_time: Fn=%s, Time=%f" % (repr(fn), end-start)) return result + +# This method of getting the encoding might not be the best... +# but it works for now, and avoids hacks with +# setdefaultencoding. +# +# If there's a better way without doing Python siteconfig stuff, +# please let me know! --Paul +import sys +get_encoding = sys.getfilesystemencoding + +def convert_query_to_unicode(query): + if isinstance(query, unicode): + return query + encoding = get_encoding() + wrapped_query = "%%%s%%" % query # Wrap in wildcards + unicode_query = wrapped_query.decode(encoding) + return unicode_query diff --git a/jblite/kd2.py b/jblite/kd2.py index 85cc6d9..ea5b8bd 100644 --- a/jblite/kd2.py +++ b/jblite/kd2.py @@ -5,7 +5,7 @@ from __future__ import with_statement import os, sys, re, sqlite3, time from cStringIO import StringIO from xml.etree.cElementTree import ElementTree -from helpers import gzread +from helpers import gzread, get_encoding, convert_query_to_unicode from db import Database as BaseDatabase from table import Table, ChildTable, KeyValueTable @@ -15,12 +15,6 @@ import gettext gettext.install("jblite") -# This method of getting the encoding might not be the best... -# but it works for now, and avoids hacks with -# setdefaultencoding. -get_encoding = sys.getfilesystemencoding - - def convert_kunyomi(coded_str): """Converts a kunyomi string with ./- to a user-friendly format. @@ -169,9 +163,7 @@ class Database(BaseDatabase): self.conn.commit() def search(self, query, lang=None, options=None): - encoding = get_encoding() - wrapped_query = "%%%s%%" % query # Wrap in wildcards - unicode_query = wrapped_query.decode(encoding) + unicode_query = convert_query_to_unicode(query) verbose = (options is not None) and (options.verbose == True) -- 2.11.4.GIT