MsgCatalog.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » AE » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » AE » MsgCatalog.py
#  
#  Copyright (C) 2001 Andrew T. Csillag <drew_csillag@geocities.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
#   
# Time-stamp: <01/04/26 14:56:47 smulloni>
########################################################################

import re
import cfg
from SkunkExcept import SkunkStandardError

cfg.Configuration.mergeDefaults(
    strictMessageCatalogs = 1,
    DefaultLanguage="eng"
    )

class NO_DEFAULT: pass

def getMessage(catalog,
               message,
               lang = cfg.Configuration.DefaultLanguage,
               fmt = lambda x: x,
               bindvars = {},
               default=NO_DEFAULT):
    """
    A gentle getMessage function, to extract a message from a catalog
    """
    if isinstance(catalog, MultiLingualCatalog):
        if not default is NO_DEFAULT:
            if not catalog.hasMessage(message, lang):
                return default
        return fmt(catalog.getMessage(message,
                                      lang = lang, 
                                      bindvars = bindvars))
    else:
        if not default is NO_DEFAULT:
            if not catalog.hasMessage(message):
                return default
        return fmt(catalog.getMessage(message,
                                      bindvars = bindvars))


class MessageCatalog:
    def __init__(self, dict, name):
        self._dict = dict
        self._name = name

    def msg (self,
             message,
             bindvars = {},
             fmt = lambda x:x,
             lang = None):
        return fmt(self.getMessage(message,
                                   bindvars = bindvars))

    def hasMessage(self, name):
        return self._dict.has_key(name)
    
    def getMessage(self, name, bindvars = {}):
        """
        Retrieve a message by name
        """

        try: 
            msg = self._dict[name]
        except KeyError:
            if not cfg.Configuration.strictMessageCatalogs:
                msg = 'Error: no such message "%s" in catalog %s' % \
                      (name, self._name)
            else:
                raise SkunkStandardError, \
                      'no such message "%s" in catalog %s' % (
                          name, self._name)
        if not msg or len(bindvars) == 0:
            return msg
        else:
            return self._bind(msg, bindvars)

    def _bind (self, msg, bindvars):
        """
        Perform item substitution
        """
        regexes = {} 
        for k, v in bindvars.items():
            regexes[ re.compile('\[\[%s\]\]' % k) ] = str(v)

        for k, v in regexes.items():
            msg = k.sub ( v, msg )

        return msg

class MultiLingualCatalog(MessageCatalog):
    
    def msg (self,
             message,
             bindvars = {},
             fmt = lambda x:x,
             lang = cfg.Configuration.DefaultLanguage):

        return fmt(self.getMessage(message,
                                   lang = lang, 
                                   bindvars = bindvars))

    def hasMessage(self, name, lang):
        return self._dict.has_key(lang) and \
               self._dict[lang].has_key(name)
    
    def getMessage(self, name, lang, bindvars = {}):
        """
        Retrieve a message by name and language
        """

        # fix: 'spa' maps to 'esp' language string,
        # but message catalogs  may not have 'spa'
        # string directly inside them
        if lang == 'spa': lang = 'esp'

        try:
            msg = self._dict[lang][name]
        except KeyError:
            if not cfg.Configuration.strictMessageCatalogs:
                msg = 'Error: no such message "%s" for language %s in ' \
                      'catalog %s' % ( name, lang, self._name )
            else:
                raise SkunkStandardError, \
                      'no such message "%s" for language %s in catalog %s' % \
                      ( name, lang, self._name)

        if not msg or len(bindvars) == 0:
            return msg
        else:
            return self._bind(msg, bindvars)

########################################################################
# $Log: MsgCatalog.py,v $
# Revision 1.4  2003/05/01 20:45:58  drew_csillag
# Changed license text
#
# Revision 1.3  2003/04/19 14:19:35  smulloni
# changes for scopeable
#
# Revision 1.2  2002/07/15 15:07:11  smulloni
# various changes: configuration (DOCROOT); new sw.conf directive (File);
# less spurious debug messages from rewrite; more forgiving interface to
# MsgCatalog.
#
# Revision 1.1.1.1  2001/08/05 15:00:37  drew_csillag
# take 2 of import
#
#
# Revision 1.4  2001/07/09 20:38:41  drew
# added licence comments
#
# Revision 1.3  2001/04/26 19:01:30  smullyan
# added DefaultLanguage to Configuration and fixed references to AED and
# SkunkStandardError in MsgCatalog
#
########################################################################
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.