annotate babel/support.py @ 353:cd8702e96f45

Added `validate_format helper function to `babel.support`.
author aronacher
date Tue, 17 Jun 2008 19:32:09 +0000
parents e5db561bf70e
children 249aab27c4b3
rev   line source
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
2 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2007 Edgewall Software
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
4 # All rights reserved.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
5 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
9 #
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
13
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
14 """Several classes and functions that help with integrating and using Babel
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
15 in applications.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
16
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
17 .. note: the code in this module is not used by Babel itself
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
18 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
19
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
20 from datetime import date, datetime, time
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
21 import gettext
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
22
353
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
23 try:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
24 set
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
25 except NameError:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
26 from sets import set
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
27
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
28 from babel.core import Locale
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
29 from babel.dates import format_date, format_datetime, format_time, LC_TIME
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
30 from babel.numbers import format_number, format_decimal, format_currency, \
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
31 format_percent, format_scientific, LC_NUMERIC
353
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
32 from babel.util import UTC, PYTHON_FORMAT
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
33
353
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
34 __all__ = ['Format', 'LazyProxy', 'Translations', 'validate_format']
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
35 __docformat__ = 'restructuredtext en'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
36
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
37
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
38 class Format(object):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
39 """Wrapper class providing the various date and number formatting functions
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
40 bound to a specific locale and time-zone.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
41
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
42 >>> fmt = Format('en_US', UTC)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
43 >>> fmt.date(date(2007, 4, 1))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
44 u'Apr 1, 2007'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
45 >>> fmt.decimal(1.2345)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
46 u'1.234'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
47 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
48
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
49 def __init__(self, locale, tzinfo=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
50 """Initialize the formatter.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
51
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
52 :param locale: the locale identifier or `Locale` instance
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
53 :param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
54 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
55 self.locale = Locale.parse(locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
56 self.tzinfo = tzinfo
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
57
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
58 def date(self, date=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
59 """Return a date formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
60
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
61 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
62 >>> fmt.date(date(2007, 4, 1))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
63 u'Apr 1, 2007'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
64
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
65 :see: `babel.dates.format_date`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
66 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
67 return format_date(date, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
68
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
69 def datetime(self, datetime=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
70 """Return a date and time formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
71
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
72 >>> from pytz import timezone
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
73 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
74 >>> fmt.datetime(datetime(2007, 4, 1, 15, 30))
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
75 u'Apr 1, 2007 11:30:00 AM'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
76
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
77 :see: `babel.dates.format_datetime`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
78 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
79 return format_datetime(datetime, format, tzinfo=self.tzinfo,
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
80 locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
81
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
82 def time(self, time=None, format='medium'):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
83 """Return a time formatted according to the given pattern.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
84
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
85 >>> from pytz import timezone
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
86 >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern'))
350
e5db561bf70e Fixes for timezone calculations in time formatting (#83).
cmlenz
parents: 338
diff changeset
87 >>> fmt.time(datetime(2007, 4, 1, 15, 30))
103
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
88 u'11:30:00 AM'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
89
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
90 :see: `babel.dates.format_time`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
91 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
92 return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
93
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
94 def number(self, number):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
95 """Return an integer number formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
96
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
97 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
98 >>> fmt.number(1099)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
99 u'1,099'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
100
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
101 :see: `babel.numbers.format_number`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
102 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
103 return format_number(number, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
104
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
105 def decimal(self, number, format=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
106 """Return a decimal number formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
107
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
108 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
109 >>> fmt.decimal(1.2345)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
110 u'1.234'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
111
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
112 :see: `babel.numbers.format_decimal`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
113 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
114 return format_decimal(number, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
115
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
116 def currency(self, number, currency):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
117 """Return a number in the given currency formatted for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
118
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
119 :see: `babel.numbers.format_currency`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
120 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
121 return format_currency(number, currency, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
122
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
123 def percent(self, number, format=None):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
124 """Return a number formatted as percentage for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
125
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
126 >>> fmt = Format('en_US')
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
127 >>> fmt.percent(0.34)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
128 u'34%'
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
129
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
130 :see: `babel.numbers.format_percent`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
131 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
132 return format_percent(number, format, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
133
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
134 def scientific(self, number):
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
135 """Return a number formatted using scientific notation for the locale.
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
136
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
137 :see: `babel.numbers.format_scientific`
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
138 """
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
139 return format_scientific(number, locale=self.locale)
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
140
1ba215a5774d Add wrapper class bundling the various formatting functions bound to a specific locale and time-zone.
cmlenz
parents: 102
diff changeset
141
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
142 class LazyProxy(object):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
143 """Class for proxy objects that delegate to a specified function to evaluate
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
144 the actual object.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
145
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
146 >>> def greeting(name='world'):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
147 ... return 'Hello, %s!' % name
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
148 >>> lazy_greeting = LazyProxy(greeting, name='Joe')
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
149 >>> print lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
150 Hello, Joe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
151 >>> u' ' + lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
152 u' Hello, Joe!'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
153 >>> u'(%s)' % lazy_greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
154 u'(Hello, Joe!)'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
155
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
156 This can be used, for example, to implement lazy translation functions that
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
157 delay the actual translation until the string is actually used. The
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
158 rationale for such behavior is that the locale of the user may not always
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
159 be available. In web applications, you only know the locale when processing
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
160 a request.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
161
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
162 The proxy implementation attempts to be as complete as possible, so that
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
163 the lazy objects should mostly work as expected, for example for sorting:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
164
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
165 >>> greetings = [
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
166 ... LazyProxy(greeting, 'world'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
167 ... LazyProxy(greeting, 'Joe'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
168 ... LazyProxy(greeting, 'universe'),
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
169 ... ]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
170 >>> greetings.sort()
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
171 >>> for greeting in greetings:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
172 ... print greeting
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
173 Hello, Joe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
174 Hello, universe!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
175 Hello, world!
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
176 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
177 __slots__ = ['_func', '_args', '_kwargs', '_value']
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
178
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
179 def __init__(self, func, *args, **kwargs):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
180 # Avoid triggering our own __setattr__ implementation
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
181 object.__setattr__(self, '_func', func)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
182 object.__setattr__(self, '_args', args)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
183 object.__setattr__(self, '_kwargs', kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
184 object.__setattr__(self, '_value', None)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
185
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
186 def value(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
187 if self._value is None:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
188 value = self._func(*self._args, **self._kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
189 object.__setattr__(self, '_value', value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
190 return self._value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
191 value = property(value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
192
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
193 def __contains__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
194 return key in self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
195
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
196 def __nonzero__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
197 return bool(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
198
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
199 def __dir__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
200 return dir(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
201
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
202 def __iter__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
203 return iter(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
204
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
205 def __len__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
206 return len(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
207
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
208 def __str__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
209 return str(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
210
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
211 def __unicode__(self):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
212 return unicode(self.value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
213
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
214 def __add__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
215 return self.value + other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
216
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
217 def __radd__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
218 return other + self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
219
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
220 def __mod__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
221 return self.value % other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
222
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
223 def __rmod__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
224 return other % self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
225
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
226 def __mul__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
227 return self.value * other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
228
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
229 def __rmul__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
230 return other * self.value
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
231
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
232 def __call__(self, *args, **kwargs):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
233 return self.value(*args, **kwargs)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
234
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
235 def __lt__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
236 return self.value < other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
237
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
238 def __le__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
239 return self.value <= other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
240
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
241 def __eq__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
242 return self.value == other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
243
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
244 def __ne__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
245 return self.value != other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
246
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
247 def __gt__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
248 return self.value > other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
249
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
250 def __ge__(self, other):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
251 return self.value >= other
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
252
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
253 def __delattr__(self, name):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
254 delattr(self.value, name)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
255
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
256 def __getattr__(self, name):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
257 return getattr(self.value, name)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
258
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
259 def __setattr__(self, name, value):
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
260 setattr(self.value, name, value)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
261
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
262 def __delitem__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
263 del self.value[key]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
264
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
265 def __getitem__(self, key):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
266 return self.value[key]
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
267
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
268 def __setitem__(self, key, value):
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
269 self.value[key] = value
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
270
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
271
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
272 class Translations(gettext.GNUTranslations):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
273 """An extended translation catalog class."""
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
274
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
275 DEFAULT_DOMAIN = 'messages'
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
276
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
277 def __init__(self, fileobj=None):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
278 """Initialize the translations catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
279
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
280 :param fileobj: the file-like object the translation should be read
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
281 from
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
282 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
283 gettext.GNUTranslations.__init__(self, fp=fileobj)
338
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
284 self.files = filter(None, [getattr(fileobj, 'name', None)])
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
285
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
286 def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
287 """Load translations from the given directory.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
288
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
289 :param dirname: the directory containing the ``MO`` files
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
290 :param locales: the list of locales in order of preference (items in
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
291 this list can be either `Locale` objects or locale
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
292 strings)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
293 :param domain: the message domain
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
294 :return: the loaded catalog, or a ``NullTranslations`` instance if no
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
295 matching translations were found
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
296 :rtype: `Translations`
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
297 """
338
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
298 if locales is not None:
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
299 if not isinstance(locales, (list, tuple)):
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
300 locales = [locales]
398e7c377165 Fix handling of default value of `locales` parameter of the `Translations.load()` method. Thanks to Armin Ronacher for reporting the problem.
cmlenz
parents: 285
diff changeset
301 locales = [str(locale) for locale in locales]
102
4663b9130c3e o fix misnamed vars in LazyProxy
pjenvey
parents: 63
diff changeset
302 filename = gettext.find(domain or cls.DEFAULT_DOMAIN, dirname, locales)
63
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
303 if not filename:
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
304 return gettext.NullTranslations()
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
305 return cls(fileobj=open(filename, 'rb'))
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
306 load = classmethod(load)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
307
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
308 def merge(self, translations):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
309 """Merge the given translations into the catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
310
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
311 Message translations in the specfied catalog override any messages with
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
312 the same identifier in the existing catalog.
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
313
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
314 :param translations: the `Translations` instance with the messages to
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
315 merge
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
316 :return: the `Translations` instance (``self``) so that `merge` calls
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
317 can be easily chained
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
318 :rtype: `Translations`
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
319 """
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
320 if isinstance(translations, Translations):
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
321 self._catalog.update(translations._catalog)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
322 self.files.extend(translations.files)
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
323 return self
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
324
a60ecd4a4954 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
325 def __repr__(self):
285
e9550fb98127 Fix `__repr__` of `Translations` class. Closes #63.
cmlenz
parents: 103
diff changeset
326 return "<%s>" % (type(self).__name__)
353
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
327
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
328
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
329 #: list of format chars that are compatible to each other
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
330 _string_format_compatibilities = [
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
331 set(['i', 'd', 'u']),
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
332 set(['x', 'X']),
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
333 set(['f', 'F', 'g', 'G'])
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
334 ]
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
335
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
336
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
337 def validate_format(format, alternative):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
338 """Test format string `alternative` against `format`. `format` can be the
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
339 msgid of a message and `alternative` one of the `msgstr`\s. The two
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
340 arguments are not interchangeable as `alternative` may contain less
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
341 placeholders if `format` uses named placeholders.
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
342
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
343 If `format` does not use string formatting a `TypeError` is raised.
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
344
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
345 If the string formatting of `alternative` is compatible to `format` the
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
346 function returns `None`, otherwise a `ValueError` is raised.
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
347
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
348 Examples for compatible format strings:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
349
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
350 >>> validate_format('Hello %s!', 'Hallo %s!')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
351 >>> validate_format('Hello %i!', 'Hallo %d!')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
352
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
353 Example for an incompatible format strings:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
354
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
355 >>> validate_format('Hello %(name)s!', 'Hallo %s!')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
356 Traceback (most recent call last):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
357 ...
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
358 TypeError: the format strings are of different kinds
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
359
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
360 :param format: The original format string
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
361 :param alternative: The alternative format string that should be checked
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
362 against format
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
363 :return: None on success
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
364 :raises ValueError: on an formatting error
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
365 """
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
366
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
367 def _parse(string):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
368 result = []
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
369 for match in PYTHON_FORMAT.finditer(string):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
370 name, format, typechar = match.groups()
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
371 if typechar == '%' and name is not None:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
372 continue
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
373 result.append((name, typechar))
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
374 return result
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
375
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
376 def _compatible(a, b):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
377 if a == b:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
378 return True
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
379 for set in _string_format_compatibilities:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
380 if a in set and b in set:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
381 return True
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
382 return False
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
383
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
384 def _check_positional(results):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
385 positional = None
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
386 for name, char in results:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
387 if positional is None:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
388 positional = name is None
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
389 else:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
390 if (name is None) != positional:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
391 raise ValueError('format string mixes positional '
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
392 'and named placeholders')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
393 return bool(positional)
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
394
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
395 a, b = map(_parse, (format, alternative))
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
396
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
397 # if a does not use string formattings, we are dealing with invalid
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
398 # input data. This function only works if the first string provided
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
399 # does contain string format chars
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
400 if not a:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
401 raise TypeError('original string provided does not use string '
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
402 'formatting.')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
403
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
404 # now check if both strings are positional or named
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
405 a_positional, b_positional = map(_check_positional, (a, b))
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
406 if a_positional and not b_positional and not b:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
407 raise ValueError('placeholders are incompatible')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
408 elif a_positional != b_positional:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
409 raise TypeError('the format strings are of different kinds')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
410
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
411 # if we are operating on positional strings both must have the
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
412 # same number of format chars and those must be compatible
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
413 if a_positional:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
414 if len(a) != len(b):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
415 raise ValueError('positional format placeholders unbalanced')
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
416 for idx, ((_, first), (_, second)) in enumerate(zip(a, b)):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
417 if not _compatible(first, second):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
418 raise ValueError('incompatible format for placeholder %d: '
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
419 '%r and %r are not compatible' %
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
420 (idx + 1, first, second))
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
421
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
422 # otherwise the second string must not have names the first one
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
423 # doesn't have and the types of those included must be compatible
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
424 else:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
425 type_map = dict(a)
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
426 for name, typechar in b:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
427 if name not in type_map:
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
428 raise ValueError('unknown named placeholder %r' % name)
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
429 elif not _compatible(typechar, type_map[name]):
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
430 raise ValueError('incompatible format for placeholder %r: '
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
431 '%r and %r are not compatible' %
cd8702e96f45 Added `validate_format helper function to `babel.support`.
aronacher
parents: 350
diff changeset
432 (name, typechar, type_map[name]))
Copyright (C) 2012-2017 Edgewall Software