39
39
from email.Errors import HeaderParseError
40
40
from string import ascii_letters, digits, whitespace
42
import Mailman.templates
42
43
from Mailman import Errors
43
44
from Mailman import passwords
44
45
from Mailman.SafeDict import SafeDict
51
52
IDENTCHARS = ascii_letters + digits + '_'
55
TEMPLATE_DIR = os.path.dirname(Mailman.templates.__file__)
55
57
# Search for $(identifier)s strings, except that the trailing s is optional,
56
58
# since that's a common mistake
450
452
# language. After that, the server default language is used for
451
453
# <language>. If that still doesn't yield a template, then the standard
452
454
# distribution's English language template is used as an ultimate
453
# fallback. If that's missing you've got big problems. ;)
455
# fallback, and when lang is not 'en', the resulting template is passed
456
# through the translation service. If this template is missing you've got
455
459
# A word on backwards compatibility: Mailman versions prior to 2.1 stored
456
460
# templates in templates/*.{html,txt} and lists/<listname>/*.{html,txt}.
468
472
# Calculate the languages to scan
470
474
if lang is not None:
471
languages.append(lang)
472
476
if mlist is not None:
473
languages.append(mlist.preferred_language)
474
languages.append(config.DEFAULT_SERVER_LANGUAGE)
477
languages.add(mlist.preferred_language)
478
languages.add(config.DEFAULT_SERVER_LANGUAGE)
475
479
# Calculate the locations to scan
477
481
if mlist is not None:
478
482
searchdirs.append(mlist.full_path)
479
searchdirs.append(os.path.join(config.TEMPLATE_DIR, mlist.host_name))
480
searchdirs.append(os.path.join(config.TEMPLATE_DIR, 'site'))
481
searchdirs.append(config.TEMPLATE_DIR)
483
searchdirs.append(os.path.join(TEMPLATE_DIR, mlist.host_name))
484
searchdirs.append(os.path.join(TEMPLATE_DIR, 'site'))
485
searchdirs.append(TEMPLATE_DIR)
498
502
# Try one last time with the distro English template, which, unless
499
503
# you've got a really broken installation, must be there.
501
filename = os.path.join(config.TEMPLATE_DIR, 'en', templatefile)
505
filename = os.path.join(TEMPLATE_DIR, 'en', templatefile)
502
506
fp = open(filename)
503
507
except IOError, e:
504
if e.errno <> errno.ENOENT: raise
508
if e.errno <> errno.ENOENT:
505
510
# We never found the template. BAD!
506
511
raise IOError(errno.ENOENT, 'No template file found', templatefile)
509
template = unicode(template, GetCharSet(lang), 'replace')
513
from Mailman.i18n import get_translation
515
data = fp.read()[:-1]
516
template = get_translation().ugettext(data)
521
template = unicode(template, GetCharSet(lang), 'replace')
511
523
if dict is not None:
513
525
sdict = SafeDict(dict, lang=lang)
514
526
text = sdict.interpolate(template)
515
except (TypeError, ValueError), e:
527
except (TypeError, ValueError):
516
528
# The template is really screwed up
517
log.error('broken template: %s\n%s', filename, e)
529
log.exception('broken template: %s', filename)
520
531
return text, filename
521
532
return wrap(text), filename
649
def GetLanguageDescr(lang):
650
return config.LC_DESCRIPTIONS[lang][0]
660
# XXX Replace this with direct calls. For now, existing uses of GetCharSet()
661
# are too numerous to change.
653
662
def GetCharSet(lang):
654
return config.LC_DESCRIPTIONS[lang][1]
656
def IsLanguage(lang):
657
return config.LC_DESCRIPTIONS.has_key(lang)
663
return config.languages.get_language_data(lang)[1]