Mercurial > p > roundup > code
annotate roundup/cgi/PageTemplates/PythonExpr.py @ 5096:e74c3611b138
- issue2550636, issue2550909: Added support for Whoosh indexer.
Also adds new config.ini setting called indexer to select
indexer. See ``doc/upgrading.txt`` for details. Initial patch
done by David Wolever. Patch modified (see ticket or below for
changes), docs updated and committed.
I have an outstanding issue with test/test_indexer.py. I have to
comment out all imports and tests for indexers I don't have (i.e.
mysql, postgres) otherwise no tests run.
With that change made, dbm, sqlite (rdbms), xapian and whoosh indexes
are all passing the indexer tests.
Changes summary:
1) support native back ends dbm and rdbms. (original patch only fell
through to dbm)
2) Developed whoosh stopfilter to not index stopwords or words outside
the the maxlength and minlength limits defined in index_common.py.
Required to pass the extremewords test_indexer test. Also I
removed a call to .lower on the input text as the tokenizer I chose
automatically does the lowercase.
3) Added support for max/min length to find. This was needed to pass
extremewords test.
4) Added back a call to save_index in add_text. This allowed all but
two tests to pass.
5) Fixed a call to:
results = searcher.search(query.Term("identifier", identifier))
which had an extra parameter that is an error under current whoosh.
6) Set limit=None in search call for find() otherwise it only return
10 items. This allowed it to pass manyresults test
Also due to changes in the roundup code removed the call in
indexer_whoosh to
from roundup.anypy.sets_ import set
since we use the python builtin set.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 25 Jun 2016 20:10:03 -0400 |
| parents | 6e3e4f24c753 |
| children | 35ea9b1efc14 |
| rev | line source |
|---|---|
| 1049 | 1 ############################################################################## |
| 2 # | |
| 3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
4 # |
| 1049 | 5 # This software is subject to the provisions of the Zope Public License, |
| 6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. | |
| 7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | |
| 8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | |
| 10 # FOR A PARTICULAR PURPOSE | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
11 # |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
12 ############################################################################## |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
13 # Modified for Roundup: |
| 1049 | 14 # |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
15 # 1. more informative traceback info |
| 1049 | 16 |
| 17 """Generic Python Expression Handler | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
18 """ |
|
1071
c08b3820edd1
Adhering to ZPL
Richard Jones <richard@users.sourceforge.net>
parents:
1065
diff
changeset
|
19 |
| 1049 | 20 from TALES import CompilerError |
| 21 from sys import exc_info | |
| 22 | |
| 23 class getSecurityManager: | |
| 24 '''Null security manager''' | |
| 25 def validate(self, *args, **kwargs): | |
| 26 return 1 | |
| 27 addContext = removeContext = validateValue = validate | |
| 28 | |
| 29 class PythonExpr: | |
| 30 def __init__(self, name, expr, engine): | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
31 self.expr = expr = expr.strip().replace('\n', ' ') |
| 1049 | 32 try: |
| 33 d = {} | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
34 exec 'def f():\n return %s\n' % expr.strip() in d |
| 1049 | 35 self._f = d['f'] |
| 36 except: | |
| 37 raise CompilerError, ('Python expression error:\n' | |
| 38 '%s: %s') % exc_info()[:2] | |
| 39 self._get_used_names() | |
| 40 | |
| 41 def _get_used_names(self): | |
| 42 self._f_varnames = vnames = [] | |
| 43 for vname in self._f.func_code.co_names: | |
| 44 if vname[0] not in '$_': | |
| 45 vnames.append(vname) | |
| 46 | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
47 def _bind_used_names(self, econtext, _marker=[]): |
| 1049 | 48 # Bind template variables |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
49 names = {'CONTEXTS': econtext.contexts} |
| 1049 | 50 vars = econtext.vars |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
51 getType = econtext.getCompiler().getTypes().get |
| 1049 | 52 for vname in self._f_varnames: |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
53 val = vars.get(vname, _marker) |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
54 if val is _marker: |
| 1049 | 55 has = val = getType(vname) |
| 56 if has: | |
| 57 val = ExprTypeProxy(vname, val, econtext) | |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
58 names[vname] = val |
|
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
59 else: |
| 1049 | 60 names[vname] = val |
| 61 return names | |
| 62 | |
| 63 def __call__(self, econtext): | |
|
1065
0f9aa62917bd
much nicer error messages when there's a templating error
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
64 __traceback_info__ = 'python expression "%s"'%self.expr |
| 1049 | 65 f = self._f |
|
2349
b43efe461b3e
update PageTemplates to latest Zope codebase
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
66 f.func_globals.update(self._bind_used_names(econtext)) |
| 1049 | 67 return f() |
| 68 | |
| 69 def __str__(self): | |
| 70 return 'Python expression "%s"' % self.expr | |
| 71 def __repr__(self): | |
| 72 return '<PythonExpr %s>' % self.expr | |
| 73 | |
| 74 class ExprTypeProxy: | |
| 75 '''Class that proxies access to an expression type handler''' | |
| 76 def __init__(self, name, handler, econtext): | |
| 77 self._name = name | |
| 78 self._handler = handler | |
| 79 self._econtext = econtext | |
| 80 def __call__(self, text): | |
| 81 return self._handler(self._name, text, | |
|
1257
93b80ad11ca8
merged Zope Collector #372 fix from ZPT CVS trunk
Richard Jones <richard@users.sourceforge.net>
parents:
1071
diff
changeset
|
82 self._econtext.getCompiler())(self._econtext) |
| 1049 | 83 |
