~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Lazily compiled regex objects.
18
18
 
19
 
This module defines a class which creates proxy objects for regex
20
 
compilation.  This allows overriding re.compile() to return lazily compiled
21
 
objects.  
22
 
 
23
 
We do this rather than just providing a new interface so that it will also
24
 
be used by existing Python modules that create regexs.
 
19
This module defines a class which creates proxy objects for regex compilation.
 
20
This allows overriding re.compile() to return lazily compiled objects.
25
21
"""
26
22
 
27
 
from __future__ import absolute_import
28
 
 
29
23
import re
30
24
 
31
 
from bzrlib import errors
32
 
 
33
25
 
34
26
class LazyRegex(object):
35
27
    """A proxy around a real regex, which won't be compiled until accessed."""
50
42
    def __init__(self, args=(), kwargs={}):
51
43
        """Create a new proxy object, passing in the args to pass to re.compile
52
44
 
53
 
        :param args: The `*args` to pass to re.compile
54
 
        :param kwargs: The `**kwargs` to pass to re.compile
 
45
        :param args: The *args to pass to re.compile
 
46
        :param kwargs: The **kwargs to pass to re.compile
55
47
        """
56
48
        self._real_regex = None
57
49
        self._regex_args = args
66
58
 
67
59
    def _real_re_compile(self, *args, **kwargs):
68
60
        """Thunk over to the original re.compile"""
69
 
        try:
70
 
            return _real_re_compile(*args, **kwargs)
71
 
        except re.error, e:
72
 
            # raise InvalidPattern instead of re.error as this gives a
73
 
            # cleaner message to the user.
74
 
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
75
 
 
76
 
    def __getstate__(self):
77
 
        """Return the state to use when pickling."""
78
 
        return {
79
 
            "args": self._regex_args,
80
 
            "kwargs": self._regex_kwargs,
81
 
            }
82
 
 
83
 
    def __setstate__(self, dict):
84
 
        """Restore from a pickled state."""
85
 
        self._real_regex = None
86
 
        setattr(self, "_regex_args", dict["args"])
87
 
        setattr(self, "_regex_kwargs", dict["kwargs"])
 
61
        return _real_re_compile(*args, **kwargs)
88
62
 
89
63
    def __getattr__(self, attr):
90
64
        """Return a member from the proxied regex object.
117
91
 
118
92
def reset_compile():
119
93
    """Restore the original function to re.compile().
120
 
 
 
94
    
121
95
    It is safe to call reset_compile() multiple times, it will always
122
96
    restore re.compile() to the value that existed at import time.
123
97
    Though the first call will reset back to the original (it doesn't