[kupfer: 3/6] Real escape function for search queries in tracker
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer: 3/6] Real escape function for search queries in tracker
- Date: Tue, 4 May 2010 11:49:07 +0000 (UTC)
commit 1e1d581377159a515ebc12b385b0cd35dd8edbb6
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Tue May 4 12:31:41 2010 +0200
Real escape function for search queries in tracker
kupfer/plugin/tracker1.py | 23 +++++++++++++++++------
1 files changed, 17 insertions(+), 6 deletions(-)
---
diff --git a/kupfer/plugin/tracker1.py b/kupfer/plugin/tracker1.py
index e7ff818..25d9bea 100644
--- a/kupfer/plugin/tracker1.py
+++ b/kupfer/plugin/tracker1.py
@@ -58,13 +58,23 @@ class TrackerSearchHere (Action):
def item_types(self):
yield TextLeaf
-def is_ok_char(c):
- return c.isalnum() or c == " "
+def sparql_escape(ustr):
+ """Escape unicode string @ustr for insertion into a SPARQL query
+
+ Implemented to behave like tracker_sparql_escape in libtracker-client
+ """
+ sparql_escape_table = {
+ ord(u'\t'): ur'\t',
+ ord(u'\n'): ur'\n',
+ ord(u'\b'): ur'\b',
+ ord(u'\f'): ur'\f',
+ ord(u'"') : ur'\"',
+ ord(u'\\'): u'\\\\',
+ }
+ return ustr.translate(sparql_escape_table)
def get_file_results_sparql(searchobj, query, max_items):
- # We don't have any real escape function for queries
- # so we instead strip everything not alphanumeric
- clean_query = u"".join([c for c in query if is_ok_char(c)])
+ clean_query = sparql_escape(query)
sql = u"""SELECT tracker:coalesce (nie:url (?s), ?s)
WHERE { ?s fts:match "%s*" . ?s tracker:available true . }
ORDER BY tracker:weight(?s)
@@ -170,7 +180,8 @@ class TrackerQuerySource (Source):
try:
et = ElementTree(file=leaf.object)
query = et.getroot().find("text").text
- return cls(query)
+ us_query = kupferstring.tounicode(query)
+ return cls(us_query)
except Exception:
return None
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]