changeset 579:99d51589c822 trunk

use decorators (as we require Python 2.4+ anyway)
author fschwarz
date Tue, 31 Jul 2012 08:46:19 +0000
parents b167d06df1d6
children ea413a4d754b
files babel/core.py babel/messages/catalog.py babel/plural.py babel/support.py
diffstat 4 files changed, 165 insertions(+), 201 deletions(-) [+]
line wrap: on
line diff
--- a/babel/core.py
+++ b/babel/core.py
@@ -136,6 +136,7 @@
         if not localedata.exists(identifier):
             raise UnknownLocaleError(identifier)
 
+    @classmethod
     def default(cls, category=None, aliases=LOCALE_ALIASES):
         """Return the system default locale for the specified category.
         
@@ -153,8 +154,8 @@
         :see: `default_locale`
         """
         return cls(default_locale(category, aliases=aliases))
-    default = classmethod(default)
 
+    @classmethod
     def negotiate(cls, preferred, available, sep='_', aliases=LOCALE_ALIASES):
         """Find the best match between available and requested locale strings.
         
@@ -183,8 +184,8 @@
                                       aliases=aliases)
         if identifier:
             return Locale.parse(identifier, sep=sep)
-    negotiate = classmethod(negotiate)
 
+    @classmethod
     def parse(cls, identifier, sep='_'):
         """Create a `Locale` instance for the given locale identifier.
         
@@ -211,7 +212,6 @@
         if isinstance(identifier, basestring):
             return cls(*parse_locale(identifier, sep=sep))
         return identifier
-    parse = classmethod(parse)
 
     def __eq__(self, other):
         return str(self) == str(other)
@@ -226,11 +226,11 @@
         return '_'.join(filter(None, [self.language, self.script,
                                       self.territory, self.variant]))
 
+    @property
     def _data(self):
         if self.__data is None:
             self.__data = localedata.LocaleDataDict(localedata.load(str(self)))
         return self.__data
-    _data = property(_data)
 
     def get_display_name(self, locale=None):
         """Return the display name of the locale using the given locale.
@@ -274,227 +274,208 @@
         :type: `unicode`
         """)
 
+    @property    
     def english_name(self):
-        return self.get_display_name(Locale('en'))
-    english_name = property(english_name, doc="""\
-        The english display name of the locale.
+        """The english display name of the locale.
         
         >>> Locale('de').english_name
         u'German'
         >>> Locale('de', 'DE').english_name
         u'German (Germany)'
         
-        :type: `unicode`
-        """)
+        :type: `unicode`"""
+        return self.get_display_name(Locale('en'))
 
     #{ General Locale Display Names
 
+    @property    
     def languages(self):
-        return self._data['languages']
-    languages = property(languages, doc="""\
-        Mapping of language codes to translated language names.
+        """Mapping of language codes to translated language names.
         
         >>> Locale('de', 'DE').languages['ja']
         u'Japanisch'
         
         :type: `dict`
-        :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_
-        """)
+        :see: `ISO 639 <http://www.loc.gov/standards/iso639-2/>`_"""
+        return self._data['languages']
 
+    @property    
     def scripts(self):
-        return self._data['scripts']
-    scripts = property(scripts, doc="""\
-        Mapping of script codes to translated script names.
+        """Mapping of script codes to translated script names.
         
         >>> Locale('en', 'US').scripts['Hira']
         u'Hiragana'
         
         :type: `dict`
-        :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_
-        """)
+        :see: `ISO 15924 <http://www.evertype.com/standards/iso15924/>`_"""
+        return self._data['scripts']
 
+    @property    
     def territories(self):
-        return self._data['territories']
-    territories = property(territories, doc="""\
-        Mapping of script codes to translated script names.
+        """Mapping of script codes to translated script names.
         
         >>> Locale('es', 'CO').territories['DE']
         u'Alemania'
         
         :type: `dict`
-        :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_
-        """)
+        :see: `ISO 3166 <http://www.iso.org/iso/en/prods-services/iso3166ma/>`_"""
+        return self._data['territories']
 
+    @property    
     def variants(self):
-        return self._data['variants']
-    variants = property(variants, doc="""\
-        Mapping of script codes to translated script names.
+        """Mapping of script codes to translated script names.
         
         >>> Locale('de', 'DE').variants['1901']
         u'Alte deutsche Rechtschreibung'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['variants']
 
     #{ Number Formatting
 
+    @property    
     def currencies(self):
-        return self._data['currency_names']
-    currencies = property(currencies, doc="""\
-        Mapping of currency codes to translated currency names.
+        """Mapping of currency codes to translated currency names.
         
         >>> Locale('en').currencies['COP']
         u'Colombian Peso'
         >>> Locale('de', 'DE').currencies['COP']
         u'Kolumbianischer Peso'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['currency_names']
 
+    @property    
     def currency_symbols(self):
-        return self._data['currency_symbols']
-    currency_symbols = property(currency_symbols, doc="""\
-        Mapping of currency codes to symbols.
+        """Mapping of currency codes to symbols.
         
         >>> Locale('en', 'US').currency_symbols['USD']
         u'$'
         >>> Locale('es', 'CO').currency_symbols['USD']
         u'US$'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['currency_symbols']
 
+    @property    
     def number_symbols(self):
-        return self._data['number_symbols']
-    number_symbols = property(number_symbols, doc="""\
-        Symbols used in number formatting.
+        """Symbols used in number formatting.
         
         >>> Locale('fr', 'FR').number_symbols['decimal']
         u','
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['number_symbols']
 
+    @property    
     def decimal_formats(self):
-        return self._data['decimal_formats']
-    decimal_formats = property(decimal_formats, doc="""\
-        Locale patterns for decimal number formatting.
+        """Locale patterns for decimal number formatting.
         
         >>> Locale('en', 'US').decimal_formats[None]
         <NumberPattern u'#,##0.###'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['decimal_formats']
 
+    @property    
     def currency_formats(self):
-        return self._data['currency_formats']
-    currency_formats = property(currency_formats, doc=r"""\
-        Locale patterns for currency number formatting.
+        """Locale patterns for currency number formatting.
         
         >>> print Locale('en', 'US').currency_formats[None]
-        <NumberPattern u'\xa4#,##0.00'>
+        <NumberPattern u'\\xa4#,##0.00'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['currency_formats']
 
+    @property    
     def percent_formats(self):
-        return self._data['percent_formats']
-    percent_formats = property(percent_formats, doc="""\
-        Locale patterns for percent number formatting.
+        """Locale patterns for percent number formatting.
         
         >>> Locale('en', 'US').percent_formats[None]
         <NumberPattern u'#,##0%'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['percent_formats']
 
+    @property    
     def scientific_formats(self):
-        return self._data['scientific_formats']
-    scientific_formats = property(scientific_formats, doc="""\
-        Locale patterns for scientific number formatting.
+        """Locale patterns for scientific number formatting.
         
         >>> Locale('en', 'US').scientific_formats[None]
         <NumberPattern u'#E0'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['scientific_formats']
 
     #{ Calendar Information and Date Formatting
 
+    @property    
     def periods(self):
-        return self._data['periods']
-    periods = property(periods, doc="""\
-        Locale display names for day periods (AM/PM).
+        """Locale display names for day periods (AM/PM).
         
         >>> Locale('en', 'US').periods['am']
         u'AM'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['periods']
 
+    @property    
     def days(self):
-        return self._data['days']
-    days = property(days, doc="""\
-        Locale display names for weekdays.
+        """Locale display names for weekdays.
         
         >>> Locale('de', 'DE').days['format']['wide'][3]
         u'Donnerstag'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['days']
 
+    @property    
     def months(self):
-        return self._data['months']
-    months = property(months, doc="""\
-        Locale display names for months.
+        """Locale display names for months.
         
         >>> Locale('de', 'DE').months['format']['wide'][10]
         u'Oktober'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['months']
 
+    @property    
     def quarters(self):
-        return self._data['quarters']
-    quarters = property(quarters, doc="""\
-        Locale display names for quarters.
+        """Locale display names for quarters.
         
         >>> Locale('de', 'DE').quarters['format']['wide'][1]
         u'1. Quartal'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['quarters']
 
+    @property    
     def eras(self):
-        return self._data['eras']
-    eras = property(eras, doc="""\
-        Locale display names for eras.
+        """Locale display names for eras.
         
         >>> Locale('en', 'US').eras['wide'][1]
         u'Anno Domini'
         >>> Locale('en', 'US').eras['abbreviated'][0]
         u'BC'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['eras']
 
+    @property    
     def time_zones(self):
-        return self._data['time_zones']
-    time_zones = property(time_zones, doc="""\
-        Locale display names for time zones.
+        """Locale display names for time zones.
         
         >>> Locale('en', 'US').time_zones['Europe/London']['long']['daylight']
         u'British Summer Time'
         >>> Locale('en', 'US').time_zones['America/St_Johns']['city']
         u"St. John's"
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['time_zones']
 
+    @property    
     def meta_zones(self):
-        return self._data['meta_zones']
-    meta_zones = property(meta_zones, doc="""\
-        Locale display names for meta time zones.
+        """Locale display names for meta time zones.
         
         Meta time zones are basically groups of different Olson time zones that
         have the same GMT offset and daylight savings time.
@@ -503,113 +484,104 @@
         u'Central European Summer Time'
         
         :type: `dict`
-        :since: version 0.9
-        """)
+        :since: version 0.9"""
+        return self._data['meta_zones']
 
+    @property    
     def zone_formats(self):
-        return self._data['zone_formats']
-    zone_formats = property(zone_formats, doc=r"""\
-        Patterns related to the formatting of time zones.
+        """Patterns related to the formatting of time zones.
         
         >>> Locale('en', 'US').zone_formats['fallback']
         u'%(1)s (%(0)s)'
         >>> Locale('pt', 'BR').zone_formats['region']
-        u'Hor\xe1rio %s'
+        u'Hor\\xe1rio %s'
         
         :type: `dict`
-        :since: version 0.9
-        """)
+        :since: version 0.9"""
+        return self._data['zone_formats']
 
+    @property    
     def first_week_day(self):
-        return self._data['week_data']['first_day']
-    first_week_day = property(first_week_day, doc="""\
-        The first day of a week, with 0 being Monday.
+        """The first day of a week, with 0 being Monday.
         
         >>> Locale('de', 'DE').first_week_day
         0
         >>> Locale('en', 'US').first_week_day
         6
         
-        :type: `int`
-        """)
+        :type: `int`"""
+        return self._data['week_data']['first_day']
 
+    @property    
     def weekend_start(self):
-        return self._data['week_data']['weekend_start']
-    weekend_start = property(weekend_start, doc="""\
-        The day the weekend starts, with 0 being Monday.
+        """The day the weekend starts, with 0 being Monday.
         
         >>> Locale('de', 'DE').weekend_start
         5
         
-        :type: `int`
-        """)
+        :type: `int`"""
+        return self._data['week_data']['weekend_start']
 
+    @property    
     def weekend_end(self):
-        return self._data['week_data']['weekend_end']
-    weekend_end = property(weekend_end, doc="""\
-        The day the weekend ends, with 0 being Monday.
+        """The day the weekend ends, with 0 being Monday.
         
         >>> Locale('de', 'DE').weekend_end
         6
         
-        :type: `int`
-        """)
+        :type: `int`"""
+        return self._data['week_data']['weekend_end']
 
+    @property    
     def min_week_days(self):
-        return self._data['week_data']['min_days']
-    min_week_days = property(min_week_days, doc="""\
-        The minimum number of days in a week so that the week is counted as the
-        first week of a year or month.
+        """The minimum number of days in a week so that the week is counted as 
+        the first week of a year or month.
         
         >>> Locale('de', 'DE').min_week_days
         4
         
-        :type: `int`
-        """)
+        :type: `int`"""
+        return self._data['week_data']['min_days']
 
+    @property    
     def date_formats(self):
-        return self._data['date_formats']
-    date_formats = property(date_formats, doc="""\
-        Locale patterns for date formatting.
+        """Locale patterns for date formatting.
         
         >>> Locale('en', 'US').date_formats['short']
         <DateTimePattern u'M/d/yy'>
         >>> Locale('fr', 'FR').date_formats['long']
         <DateTimePattern u'd MMMM y'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['date_formats']
 
+    @property    
     def time_formats(self):
-        return self._data['time_formats']
-    time_formats = property(time_formats, doc="""\
-        Locale patterns for time formatting.
+        """Locale patterns for time formatting.
         
         >>> Locale('en', 'US').time_formats['short']
         <DateTimePattern u'h:mm a'>
         >>> Locale('fr', 'FR').time_formats['long']
         <DateTimePattern u'HH:mm:ss z'>
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['time_formats']
 
+    @property    
     def datetime_formats(self):
-        return self._data['datetime_formats']
-    datetime_formats = property(datetime_formats, doc="""\
-        Locale patterns for datetime formatting.
+        """Locale patterns for datetime formatting.
         
         >>> Locale('en').datetime_formats['full']
         u'{1} {0}'
         >>> Locale('th').datetime_formats['medium']
         u'{1}, {0}'
         
-        :type: `dict`
-        """)
+        :type: `dict`"""
+        return self._data['datetime_formats']
 
+    @property    
     def plural_form(self):
-        return self._data['plural_form']
-    plural_form = property(plural_form, doc="""\
-        Plural rules for the locale.
+        """Plural rules for the locale.
         
         >>> Locale('en').plural_form(1)
         'one'
@@ -620,8 +592,8 @@
         >>> Locale('ru').plural_form(100)
         'many'
         
-        :type: `PluralRule`
-        """)
+        :type: `PluralRule`"""
+        return self._data['plural_form']
 
 
 def default_locale(category=None, aliases=LOCALE_ALIASES):
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -146,10 +146,9 @@
                 errors.append(e)
         return errors
 
+    @property
     def fuzzy(self):
-        return 'fuzzy' in self.flags
-    fuzzy = property(fuzzy, doc="""\
-        Whether the translation is fuzzy.
+        """Whether the translation is fuzzy.
 
         >>> Message('foo').fuzzy
         False
@@ -159,37 +158,35 @@
         >>> msg
         <Message 'foo' (flags: ['fuzzy'])>
 
-        :type:  `bool`
-        """)
+        :type:  `bool`"""
+        return 'fuzzy' in self.flags
 
+    @property
     def pluralizable(self):
-        return isinstance(self.id, (list, tuple))
-    pluralizable = property(pluralizable, doc="""\
-        Whether the message is plurizable.
+        """Whether the message is plurizable.
 
         >>> Message('foo').pluralizable
         False
         >>> Message(('foo', 'bar')).pluralizable
         True
 
-        :type:  `bool`
-        """)
+        :type:  `bool`"""
+        return isinstance(self.id, (list, tuple))
 
+    @property
     def python_format(self):
-        ids = self.id
-        if not isinstance(ids, (list, tuple)):
-            ids = [ids]
-        return bool(filter(None, [PYTHON_FORMAT.search(id) for id in ids]))
-    python_format = property(python_format, doc="""\
-        Whether the message contains Python-style parameters.
+        """Whether the message contains Python-style parameters.
 
         >>> Message('foo %(name)s bar').python_format
         True
         >>> Message(('foo %(name)s', 'foo %(name)s')).python_format
         True
 
-        :type:  `bool`
-        """)
+        :type:  `bool`"""
+        ids = self.id
+        if not isinstance(ids, (list, tuple)):
+            ids = [ids]
+        return bool(filter(None, [PYTHON_FORMAT.search(id) for id in ids]))
 
 
 class TranslationError(Exception):
@@ -472,54 +469,51 @@
     :type: `list`
     """)
 
+    @property
     def num_plurals(self):
+        """The number of plurals used by the catalog or locale.
+
+        >>> Catalog(locale='en').num_plurals
+        2
+        >>> Catalog(locale='ga').num_plurals
+        3
+
+        :type: `int`"""
         if self._num_plurals is None:
             num = 2
             if self.locale:
                 num = get_plural(self.locale)[0]
             self._num_plurals = num
         return self._num_plurals
-    num_plurals = property(num_plurals, doc="""\
-    The number of plurals used by the catalog or locale.
 
-    >>> Catalog(locale='en').num_plurals
-    2
-    >>> Catalog(locale='ga').num_plurals
-    3
+    @property
+    def plural_expr(self):
+        """The plural expression used by the catalog or locale.
 
-    :type: `int`
-    """)
+        >>> Catalog(locale='en').plural_expr
+        '(n != 1)'
+        >>> Catalog(locale='ga').plural_expr
+        '(n==1 ? 0 : n==2 ? 1 : 2)'
 
-    def plural_expr(self):
+        :type: `basestring`"""
         if self._plural_expr is None:
             expr = '(n != 1)'
             if self.locale:
                 expr = get_plural(self.locale)[1]
             self._plural_expr = expr
         return self._plural_expr
-    plural_expr = property(plural_expr, doc="""\
-    The plural expression used by the catalog or locale.
-
-    >>> Catalog(locale='en').plural_expr
-    '(n != 1)'
-    >>> Catalog(locale='ga').plural_expr
-    '(n==1 ? 0 : n==2 ? 1 : 2)'
-
-    :type: `basestring`
-    """)
 
+    @property
     def plural_forms(self):
-        return 'nplurals=%s; plural=%s' % (self.num_plurals, self.plural_expr)
-    plural_forms = property(plural_forms, doc="""\
-    Return the plural forms declaration for the locale.
+        """Return the plural forms declaration for the locale.
 
-    >>> Catalog(locale='en').plural_forms
-    'nplurals=2; plural=(n != 1)'
-    >>> Catalog(locale='pt_BR').plural_forms
-    'nplurals=2; plural=(n > 1)'
+        >>> Catalog(locale='en').plural_forms
+        'nplurals=2; plural=(n != 1)'
+        >>> Catalog(locale='pt_BR').plural_forms
+        'nplurals=2; plural=(n > 1)'
 
-    :type: `str`
-    """)
+        :type: `str`"""
+        return 'nplurals=%s; plural=%s' % (self.num_plurals, self.plural_expr)
 
     def __contains__(self, id):
         """Return whether the catalog has a message with the specified ID."""
@@ -528,16 +522,14 @@
     def __len__(self):
         """The number of messages in the catalog.
 
-        This does not include the special ``msgid ""`` entry.
-        """
+        This does not include the special ``msgid ""`` entry."""
         return len(self._messages)
 
     def __iter__(self):
         """Iterates through all the entries in the catalog, in the order they
         were added, yielding a `Message` object for every entry.
 
-        :rtype: ``iterator``
-        """
+        :rtype: ``iterator``"""
         buf = []
         for name, value in self.mime_headers:
             buf.append('%s: %s' % (name, value))
--- a/babel/plural.py
+++ b/babel/plural.py
@@ -73,6 +73,7 @@
                        if tag in rules])
         )
 
+    @classmethod
     def parse(cls, rules):
         """Create a `PluralRule` instance for the given rules.  If the rules
         are a `PluralRule` object, that object is returned.
@@ -84,8 +85,8 @@
         if isinstance(rules, cls):
             return rules
         return cls(rules)
-    parse = classmethod(parse)
 
+    @property
     def rules(self):
         """The `PluralRule` as a dict of unicode plural rules.
         
@@ -95,7 +96,6 @@
         """
         _compile = _UnicodeCompiler().compile
         return dict([(tag, _compile(ast)) for tag, ast in self.abstract])
-    rules = property(rules, doc=rules.__doc__)
 
     tags = property(lambda x: frozenset([i[0] for i in x.abstract]), doc="""
         A set of explicitly defined tags in this rule.  The implicit default
--- a/babel/support.py
+++ b/babel/support.py
@@ -194,6 +194,7 @@
         object.__setattr__(self, '_is_cache_enabled', is_cache_enabled)
         object.__setattr__(self, '_value', None)
 
+    @property
     def value(self):
         if self._value is None:
             value = self._func(*self._args, **self._kwargs)
@@ -201,7 +202,6 @@
                 return value
             object.__setattr__(self, '_value', value)
         return self._value
-    value = property(value)
 
     def __contains__(self, key):
         return key in self.value
@@ -298,6 +298,7 @@
         self.domain = domain
         self._domains = {}
 
+    @classmethod
     def load(cls, dirname=None, locales=None, domain=DEFAULT_DOMAIN):
         """Load translations from the given directory.
 
@@ -320,7 +321,6 @@
         if not filename:
             return gettext.NullTranslations()
         return cls(fileobj=open(filename, 'rb'), domain=domain)
-    load = classmethod(load)
 
     def __repr__(self):
         return '<%s: "%s">' % (type(self).__name__,
Copyright (C) 2012-2017 Edgewall Software