Handle changes to fnmatch.translate in Python 2.6

Define utils.python.glob2re based on the Python version being used.
Use glob2re in Todo and Note plugins.

Closes: Sf#3059292
Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
James Vega 2010-09-08 23:31:01 -04:00
parent fc2a84fb90
commit b0575cec88
3 changed files with 17 additions and 15 deletions

View File

@ -30,7 +30,6 @@
import re import re
import time import time
import fnmatch
import operator import operator
import supybot.dbi as dbi import supybot.dbi as dbi
@ -98,15 +97,9 @@ class DbiNoteDB(dbi.DB):
self.set(id, n) self.set(id, n)
def getUnnotifiedIds(self, to): def getUnnotifiedIds(self, to):
## def p(note):
## return not note.notified and note.to == to
## return [note.id for note in self.select(p)]
return self.unNotified.get(to, []) return self.unNotified.get(to, [])
def getUnreadIds(self, to): def getUnreadIds(self, to):
## def p(note):
## return not note.read and note.to == to
## return [note.id for note in self.select(p)]
return self.unRead.get(to, []) return self.unRead.get(to, [])
def send(self, frm, to, public, text): def send(self, frm, to, public, text):
@ -303,9 +296,8 @@ class Note(callbacks.Plugin):
elif option == 'sent': elif option == 'sent':
own = frm own = frm
if glob: if glob:
glob = fnmatch.translate(glob) glob = utils.python.glob2re(glob)
# ignore the trailing $ fnmatch.translate adds to the regexp criteria.append(re.compile(glob).search)
criteria.append(re.compile(glob[:-1]).search)
def match(note): def match(note):
for p in criteria: for p in criteria:
if not p(note.text): if not p(note.text):

View File

@ -1,5 +1,6 @@
### ###
# Copyright (c) 2003-2005, Daniel DiPaolo # Copyright (c) 2003-2005, Daniel DiPaolo
# Copyright (c) 2010, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -30,7 +31,6 @@
import os import os
import re import re
import time import time
import fnmatch
import operator import operator
import supybot.dbi as dbi import supybot.dbi as dbi
@ -230,8 +230,8 @@ class Todo(callbacks.Plugin):
if option == 'regexp': if option == 'regexp':
criteria.append(arg.search) criteria.append(arg.search)
for glob in globs: for glob in globs:
glob = fnmatch.translate(glob) glob = utils.python.glob2re(glob)
criteria.append(re.compile(glob[:-1]).search) criteria.append(re.compile(glob).search)
try: try:
tasks = self.db.select(user.id, criteria) tasks = self.db.select(user.id, criteria)
L = [format('#%i: %s', t.id, self._shrink(t.task)) for t in tasks] L = [format('#%i: %s', t.id, self._shrink(t.task)) for t in tasks]

View File

@ -1,6 +1,6 @@
### ###
# Copyright (c) 2005-2009, Jeremiah Fincher # Copyright (c) 2005-2009, Jeremiah Fincher
# Copyright (c) 2009, James Vega # Copyright (c) 2009-2010, James Vega
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -30,6 +30,7 @@
import sys import sys
import types import types
import fnmatch
import UserDict import UserDict
import threading import threading
@ -66,7 +67,6 @@ class Object(object):
def __ne__(self, other): def __ne__(self, other):
return not self == other return not self == other
class Synchronized(type): class Synchronized(type):
METHODS = '__synchronized__' METHODS = '__synchronized__'
LOCK = '_Synchronized_rlock' LOCK = '_Synchronized_rlock'
@ -103,5 +103,15 @@ class Synchronized(type):
newclass = super(Synchronized, cls).__new__(cls, name, bases, dict) newclass = super(Synchronized, cls).__new__(cls, name, bases, dict)
return newclass return newclass
# Translate glob to regular expression, trimming the "match EOL" portion of
# the regular expression.
if sys.version_info < (2, 6, 0):
# Pre-2.6 just uses the $ anchor
def glob2re(g):
return fnmatch.translate(g)[:-1]
else:
# Post-2.6 uses \Z(?ms) per http://issues.python.org/6665
def glob2re(g):
return fnmatch.translate(g)[:-7]
# vim:set shiftwidth=4 softtabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 softtabstop=8 expandtab textwidth=78: