annotate babel/messages/catalog.py @ 58:068952b4d4c0

Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
author cmlenz
date Fri, 08 Jun 2007 11:08:03 +0000
parents
children a60ecd4a4954
rev   line source
58
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
2 #
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
4 # All rights reserved.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
5 #
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
9 #
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
13
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
14 """Data structures for message catalogs."""
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
15
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
16 import re
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
17 try:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
18 set
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
19 except NameError:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
20 from sets import Set as set
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
21
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
22 from babel.util import odict
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
23
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
24 __all__ = ['Message', 'Catalog']
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
25 __docformat__ = 'restructuredtext en'
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
26
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
27 PYTHON_FORMAT = re.compile(r'\%(\([\w]+\))?[diouxXeEfFgGcrs]').search
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
28
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
29
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
30 class Message(object):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
31 """Representation of a single message in a catalog."""
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
32
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
33 def __init__(self, id, string=None, locations=(), flags=()):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
34 """Create the message object.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
35
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
36 :param id: the message ID, or a ``(singular, plural)`` tuple for
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
37 pluralizable messages
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
38 :param string: the translated message string, or a
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
39 ``(singular, plural)`` tuple for pluralizable messages
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
40 :param locations: a sequence of ``(filenname, lineno)`` tuples
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
41 :param flags: a set or sequence of flags
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
42 """
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
43 self.id = id
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
44 self.string = string
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
45 self.locations = locations
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
46 self.flags = set(flags)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
47 if self.python_format:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
48 self.flags.add('python-format')
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
49 else:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
50 self.flags.discard('python-format')
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
51
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
52 def __repr__(self):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
53 return '<%s %r>' % (type(self).__name__, self.id)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
54
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
55 def pluralizable(self):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
56 return isinstance(self.id, (list, tuple))
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
57 pluralizable = property(pluralizable, doc="""\
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
58 Whether the message is plurizable.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
59
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
60 >>> Message('foo').pluralizable
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
61 False
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
62 >>> Message(('foo', 'bar')).pluralizable
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
63 True
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
64
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
65 :rtype: `bool`
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
66 """)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
67
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
68 def python_format(self):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
69 ids = self.id
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
70 if not isinstance(ids, (list, tuple)):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
71 ids = [ids]
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
72 return bool(filter(None, [PYTHON_FORMAT(id) for id in ids]))
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
73 python_format = property(python_format, doc="""\
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
74 Whether the message contains Python-style parameters.
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
75
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
76 >>> Message('foo %(name)s bar').python_format
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
77 True
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
78 >>> Message(('foo %(name)s', 'foo %(name)s')).python_format
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
79 True
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
80
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
81 :rtype: `bool`
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
82 """)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
83
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
84
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
85 class Catalog(object):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
86 """Representation a message catalog."""
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
87
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
88 def __init__(self, domain=None):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
89 self.domain = domain
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
90 self.messages = odict()
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
91
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
92 def __iter__(self):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
93 for id in self.messages:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
94 yield self.messages[id]
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
95
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
96 def __repr__(self):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
97 return '<%s %r>' % (type(self).__name__, self.domain)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
98
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
99 def __delitem__(self, id):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
100 if id in self.messaages:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
101 del self.messages[id]
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
102
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
103 def __getitem__(self, id):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
104 return self.messages.get(id)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
105
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
106 def __setitem__(self, id, message):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
107 assert isinstance(message, Message), 'expected a Message object'
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
108 current = self.messages.get(id)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
109 if current:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
110 assert current.string == message.string, 'translation mismatch'
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
111 current.locations.extend(message.locations)
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
112 current.flags |= message.flags
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
113 message = current
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
114 else:
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
115 if isinstance(id, (list, tuple)):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
116 singular, plural = id
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
117 id = singular
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
118 self.messages[id] = message
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
119
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
120 def add(self, id, string=None, locations=(), flags=()):
068952b4d4c0 Add actual data structures for handling message catalogs, so that more code can be reused here between the frontends.
cmlenz
parents:
diff changeset
121 self[id] = Message(id, string, locations, flags)
Copyright (C) 2012-2017 Edgewall Software