6599: Add regular expression option to rules using the match_substring method

svn: r21933
This commit is contained in:
Nick Hall 2013-04-09 13:51:07 +00:00
parent e3934bc72d
commit fc0c0f474e
11 changed files with 45 additions and 8 deletions

View File

@ -52,6 +52,7 @@ class HasCitation(Rule):
name = _('Citations matching parameters')
category = _('General filters')
description = _("Matches citations with particular parameters")
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -52,6 +52,7 @@ class HasMedia(Rule):
name = _('Media objects matching parameters')
description = _("Matches media objects with particular parameters")
category = _('General filters')
allow_regex = True
def prepare(self,db):
self.date = None

View File

@ -59,6 +59,7 @@ class HasPlace(Rule):
name = _('Places matching parameters')
description = _("Matches places with particular parameters")
category = _('General filters')
allow_regex = True
def apply(self, db, place):
if not self.match_substring(0, place.get_title()):

View File

@ -52,6 +52,7 @@ class HasRepo(Rule):
name = _('Repositories matching parameters')
description = _("Matches Repositories with particular parameters")
category = _('General filters')
allow_regex = True
def prepare(self, dummy_db):
if self.list[1]:

View File

@ -53,6 +53,7 @@ class HasCitationBase(Rule):
name = _('Citations matching parameters')
description = _("Matches citations with particular parameters")
category = _('Citation/source filters')
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -54,6 +54,7 @@ class HasEventBase(Rule):
name = _('Events matching parameters')
description = _("Matches events with particular parameters")
category = _('Event filters')
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -51,6 +51,7 @@ class HasSourceBase(Rule):
name = _('Sources matching parameters')
description = _("Matches sources with particular parameters")
category = _('Citation/source filters')
allow_regex = True
def apply(self,db,source):
if not self.match_substring(0,source.get_title()):

View File

@ -53,6 +53,7 @@ class Rule(object):
name = ''
category = _('Miscellaneous filters')
description = _('No description')
allow_regex = False
def __init__(self, arg, use_regex=False):
self.list = []

View File

@ -135,8 +135,8 @@ class FilterList(object):
f.write(' comment="%s"' % self.fix(comment))
f.write('>\n')
for rule in the_filter.get_rules():
f.write(' <rule class="%s">\n'
% rule.__class__.__name__)
f.write(' <rule class="%s" use_regex="%s">\n'
% (rule.__class__.__name__, rule.use_regex))
for value in rule.values():
f.write(' <arg value="%s"/>\n' % self.fix(value))
f.write(' </rule>\n')

View File

@ -52,6 +52,7 @@ class FilterParser(handler.ContentHandler):
self.a = []
self.cname = None
self.namespace = 'Person'
self.use_regex = False
def setDocumentLocator(self, locator):
self.locator = locator
@ -82,6 +83,10 @@ class FilterParser(handler.ContentHandler):
self.f.set_comment(attrs['comment'])
self.gfilter_list.add(self.namespace, self.f)
elif tag == "rule":
if attrs.has_key('use_regex'):
self.use_regex = attrs['use_regex'] == 'True'
else:
self.use_regex = False
save_name = attrs['class']
if save_name in old_names_2_class:
self.r = old_names_2_class[save_name]
@ -115,7 +120,7 @@ class FilterParser(handler.ContentHandler):
"Trying to load with subset of arguments.") %\
self.f.get_name()
nargs = len(self.r.labels)
rule = self.r(self.a[0:nargs])
rule = self.r(self.a[0:nargs], self.use_regex)
self.f.add_rule(rule)
else:
if len(self.r.labels) > len(self.a):
@ -124,7 +129,7 @@ class FilterParser(handler.ContentHandler):
"will be upgraded.") %\
self.f.get_name()
try:
rule = self.r(self.a)
rule = self.r(self.a, self.use_regex)
except AssertionError, msg:
print msg
print _("ERROR: filter %s could not be correctly loaded. "

View File

@ -482,7 +482,6 @@ class EditRule(ManagedWindow.ManagedWindow):
arglist = class_obj.labels
vallist = []
tlist = []
self.page.append((class_obj, vallist, tlist))
pos = 0
l2 = gtk.Label(class_obj.name)
l2.set_alignment(0, 0.5)
@ -557,6 +556,26 @@ class EditRule(ManagedWindow.ManagedWindow):
table.attach(l, 1, 2, pos, pos+1, gtk.FILL, 0, 5, 5)
table.attach(t, 2, 3, pos, pos+1, gtk.EXPAND|gtk.FILL, 0, 5, 5)
pos += 1
use_regex = None
if class_obj.allow_regex:
use_regex = gtk.CheckButton(_('Use regular expressions'))
tip = _('Interpret the contents of string fields as regular '
'expressions.\n'
'A decimal point will match any character. '
'A question mark will match zero or one occurences '
'of the previous character or group. '
'An asterisk will match zero or more occurences. '
'A plus sign will match one or more occurences. '
'Use parentheses to group expressions. '
'Specify alternatives using a vertical bar. '
'A caret will match the start of a line. '
'A dollar sign will match the end of a line.')
use_regex.set_tooltip_text(tip)
table.attach(use_regex, 2, 3, pos, pos+1, gtk.FILL, 0, 5, 5)
self.page.append((class_obj, vallist, tlist, use_regex))
# put the table into a scrollable area:
scrolled_win = gtk.ScrolledWindow()
scrolled_win.add_with_viewport(table)
@ -612,10 +631,12 @@ class EditRule(ManagedWindow.ManagedWindow):
page = self.class2page[self.active_rule.__class__]
self.notebook.set_current_page(page)
self.display_values(self.active_rule.__class__)
(class_obj, vallist, tlist) = self.page[page]
(class_obj, vallist, tlist, use_regex) = self.page[page]
r = self.active_rule.values()
for i in range(0, min(len(tlist), len(r))):
tlist[i].set_text(r[i])
if class_obj.allow_regex:
use_regex.set_active(self.active_rule.use_regex)
self.selection.connect('changed', self.on_node_selected)
self.rname.connect('button-press-event', self._button_press)
@ -699,9 +720,12 @@ class EditRule(ManagedWindow.ManagedWindow):
try:
page = self.notebook.get_current_page()
(class_obj, vallist, tlist) = self.page[page]
(class_obj, vallist, tlist, use_regex) = self.page[page]
value_list = [unicode(sclass.get_text()) for sclass in tlist]
new_rule = class_obj(value_list)
if class_obj.allow_regex:
new_rule = class_obj(value_list, use_regex.get_active())
else:
new_rule = class_obj(value_list)
self.update_rule(self.active_rule, new_rule)
self.close()