Update of /cvsroot/mailman/mailman3/src/mailman/text
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31133/src/mailman/text

Added Files:
      Tag: 3.0
        __init__.py safedict.py strings.py 
Log Message:
Initial checkin of Mailman3 source

--- NEW FILE: __init__.py ---

--- NEW FILE: safedict.py ---
# Copyright (C) 2004 Barry A. Warsaw
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

"""Substitution dictionaries."""

import sys

_missing = object()



class safedict(dict):
    """Dictionary which returns a default value for unknown keys."""
    def __getitem__(self, key):
        if key.startswith('{') and key.endswith('}'):
            key = key[1:-1]
            fmt = '${%s}'
        else:
            fmt = '$%s'
        try:
            return super(safedict, self).__getitem__(key)
        except KeyError:
            return fmt % key



class nsdict(dict):
    """Dictionary which substitutes in locals and globals."""
    def __getitem__(self, key):
        if key.startswith('{') and key.endswith('}'):
            key = key[1:-1]
            fmt = '${%s}'
        else:
            fmt = '$%s'
        # Split the key as if it were an attribute path
        parts = key.split('.')
        part0 = parts.pop(0)
        # Now search for the parts, starting with the locals and then trying
        # the globals, in the frame two call sites up.  getframe(0) would be
        # this function, while getframe(1) would be the _() function in the
        # i18n module.  We want the one above that.
        frame = sys._getframe(2)
        if frame.f_locals.has_key(part0):
            obj = frame.f_locals[part0]
        elif frame.f_globals.has_key(part0):
            obj = frame.f_globals[part0]
        else:
            return fmt % key
        while parts:
            attr = parts.pop(0)
            obj = getattr(obj, attr, _missing)
            if obj is _missing:
                return fmt % key
        return obj

--- NEW FILE: strings.py ---
# Copyright (C) 2004 Barry A. Warsaw
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.

"""Some useful unicode string subclasses."""

import re

# Search for $$, $identifier, or ${identifier}
dre = re.compile(r'(\${2})|\$([_a-z][_a-z0-9]*)|\$({[_a-z][_a-z0-9]*})',
                 re.IGNORECASE)
# Like dre, but allows dots after the first character
ire = re.compile(r'(\${2})|\$([_a-z][_.a-z0-9]*)|\$({[_a-z][_.a-z0-9]*})',
                 re.IGNORECASE)
# Like drea, but requires ${mm:variable} patterns, for email
mre = re.compile(r'(\${2})|\$(mm:[_a-z]\w*)|\$({mm:[_a-z]\w*})', re.IGNORECASE)

EMPTYSTRING = ''
SPACE = ' '



class dstring(unicode):
    cre = dre

    """A string class for supporting $-substitutions."""
    def __new__(cls, ustr, encoding='ascii', errors='strict'):
        ustr = ustr.replace('%', '%%')
        parts = cls.cre.split(ustr)
        for i in range(1, len(parts), 4):
            if parts[i] is not None:
                parts[i] = '$'
            elif parts[i+1] is not None:
                parts[i+1] = '%(' + parts[i+1] + ')s'
            else:
                parts[i+2] = '%(' + parts[i+2] + ')s'
        newval = EMPTYSTRING.join(filter(None, parts))
        if isinstance(ustr, unicode):
            return unicode.__new__(cls, newval)
        else:
            return unicode.__new__(cls, newval, encoding, errors)



class mstring(dstring):
    """For email substitutions."""
    cre = mre


class istring(dstring):
    """For i18n text strings."""
    cre = ire



__pychecker__ = 'self=cls'


_______________________________________________
Mailman-checkins mailing list
[EMAIL PROTECTED]
Unsubscribe: http://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to