Statistics
| Branch: | Revision:

root / transifex-client-library / txlib / mappings / languages.py @ 7:e958b8256042

History | View | Annotate | Download (4.4 kB)

1
# -*- coding: utf-8 -*-
2

    
3
"""
4
Module to abstract the mapping of languages used locally versus the language
5
codes used in Transifex.
6
"""
7

    
8

    
9
class Language(object):
10
    """A language class.
11

12
    It supports defining a Trasnifex and a local language code.
13
    """
14

    
15
    def __init__(self, tx_code, local_code=None):
16
        """Initializer.
17

18
        Initialize the Tx and local codes. If no local_code is defined, we
19
        set it to tx_code.
20

21
        Args:
22
            tx_code: The language code as used by Transifex.
23
            local_code: The language code as used locally.
24
        """
25
        self._tx_code = tx_code
26
        if local_code is not None:
27
            self._local_code = local_code
28
        else:
29
            self._local_code = self._tx_code
30

    
31
    @property
32
    def local_code(self):
33
        return self._local_code
34

    
35
    @property
36
    def tx_code(self):
37
        return self._tx_code
38

    
39
    def __unicode__(self):
40
        return u"<Language: %s-%s>" % (self._tx_code, self._local_code)
41

    
42
    def __str__(self):
43
        return self.__unicode__().encode('UTF-8')
44

    
45

    
46
class LanguageContainer(object):
47
    """A container for Language objects."""
48

    
49
    def __init__(self):
50
        """Initializer."""
51
        self._tx = {}
52
        self._local = {}
53

    
54
    def add(self, lang):
55
        """Add a language to the collection.
56

57
        It will not add it, if the langauge exists already.
58

59
        Args:
60
            lang: The Language object to add.
61
        Raises:
62
            ValueError, if the language already exists in the collection.
63
        """
64
        self._insert_lang(lang, overwrite=False)
65

    
66
    def insert(self, lang):
67
        """Insert a language to the collection.
68

69
        This operation will override existing languages.
70

71
        Args:
72
            lang: The Language object to add.
73
        """
74
        self._insert_lang(lang, overwrite=True)
75

    
76
    def _insert_lang(self, lang, overwrite):
77
        """Insert a language to the collection.
78

79
        If overwrite is True, the overwrite any existing langauage. Else, raise
80
        a ValueError.
81

82
        Args:
83
            lang: The Language object to add.
84
            overwrite: A boolean flag, indicating whether to override existing
85
                values or raise an exception.
86
        Raises:
87
            ValueError, if overwrite is False and the lang object exists.
88
        """
89
        tx_key = lang.tx_code
90
        local_key = lang.local_code
91
        error_msg = "Language %s already exists."
92

    
93
        if tx_key in self._tx:
94
            if overwrite:
95
                old_lang = self._tx[tx_key]
96
                self.remove(old_lang)
97
            else:
98
                raise ValueError(error_msg % lang)
99
        if local_key in self._local:
100
            if overwrite:
101
                old_lang = self._local[local_key]
102
                self.remove(old_lang)
103
            else:
104
                raise ValueError(error_msg % lang)
105

    
106
        self._tx[tx_key] = lang
107
        self._local[local_key] = lang
108

    
109
    def remove(self, lang):
110
        """Remove a language from the collection.
111

112
        The method guarantees that the collection will always be at a
113
        consistent state.
114

115
        Args:
116
            lang: A Language object to remove.
117
        Raises:
118
            ValueError, if lang is not in the collection.
119
        """
120
        if lang.tx_code not in self._tx or lang.local_code not in self._local:
121
            raise ValueError("Language does not exist: %s." % lang)
122
        del self._tx[lang.tx_code]
123
        del self._local[lang.local_code]
124

    
125
    def __contains__(self, item):
126
        """Support the in operator.
127

128
        Args:
129
            item: A language object.
130
        Returns:
131
            True, if the language exists. False, otherwise.
132
        """
133
        return item.tx_code in self._tx or item.local_code in self._local
134

    
135
    def __len__(self):
136
        """Support the len function on the container."""
137
        assert len(self._tx) == len(self._local)
138
        return len(self._tx)
139

    
140

    
141
def iter_tx_codes(lang_container):
142
    """Iterator to iterate over the language codes as used in Transifex.
143

144
    Args:
145
        lang_container: The LanguageContainer object to iterate over.
146
    """
147
    for key in lang_container._tx.iterkeys():
148
        yield key
149

    
150

    
151
def iter_local_codes(lang_container):
152
    """Iterator to iterate over the language codes as used locally.
153

154
    Args:
155
        lang_container: The LanguageContainer object to iterate over.
156
    """
157
    for key in lang_container._local.iterkeys():
158
        yield key