src/pyams_utils/encoding.py
changeset 1 3f89629b9e54
child 7 ad40fd668164
equal deleted inserted replaced
0:16d47bd81d84 1:3f89629b9e54
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from zope.schema.interfaces import IVocabularyFactory, IChoice
       
    20 
       
    21 # import packages
       
    22 from pyams_utils.request import check_request
       
    23 from zope.interface import provider, implementer
       
    24 from zope.schema import Choice
       
    25 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary, getVocabularyRegistry
       
    26 
       
    27 from pyams_utils import _
       
    28 
       
    29 
       
    30 ENCODINGS = {
       
    31     'ascii': _('English (ASCII)'),
       
    32     'big5': _('Traditional Chinese (big5)'),
       
    33     'big5hkscs': _('Traditional Chinese (big5hkscs)'),
       
    34     'cp037': _('English (cp037)'),
       
    35     'cp424': _('Hebrew (cp424)'),
       
    36     'cp437': _('English (cp437)'),
       
    37     'cp500': _('Western Europe (cp500)'),
       
    38     'cp720': _('Arabic (cp720)'),
       
    39     'cp737': _('Greek (cp737)'),
       
    40     'cp775': _('Baltic languages (cp775)'),
       
    41     'cp850': _('Western Europe (cp850)'),
       
    42     'cp852': _('Central and Eastern Europe (cp852)'),
       
    43     'cp855': _('Bulgarian, Byelorussian, Macedonian, Russian, Serbian (cp855)'),
       
    44     'cp856': _('Hebrew (cp856)'),
       
    45     'cp857': _('Turkish (cp857)'),
       
    46     'cp858': _('Western Europe (cp858)'),
       
    47     'cp860': _('Portuguese (cp860)'),
       
    48     'cp861': _('Icelandic (cp861)'),
       
    49     'cp862': _('Hebrew (cp862)'),
       
    50     'cp863': _('Canadian (cp863)'),
       
    51     'cp864': _('Arabic (cp864)'),
       
    52     'cp865': _('Danish, Norwegian (cp865)'),
       
    53     'cp866': _('Russian (cp866)'),
       
    54     'cp869': _('Greek (cp869)'),
       
    55     'cp874': _('Thai (cp874)'),
       
    56     'cp875': _('Greek (cp875)'),
       
    57     'cp932': _('Japanese (cp932)'),
       
    58     'cp949': _('Korean (cp949)'),
       
    59     'cp950': _('Traditional Chinese (cp950)'),
       
    60     'cp1006': _('Urdu (cp1006)'),
       
    61     'cp1026': _('Turkish (cp1026)'),
       
    62     'cp1140': _('Western Europe (cp1140)'),
       
    63     'cp1250': _('Central and Eastern Europe (cp1250)'),
       
    64     'cp1251': _('Bulgarian, Byelorussian, Macedonian, Russian, Serbian (cp1251)'),
       
    65     'cp1252': _('Western Europe (cp1252)'),
       
    66     'cp1253': _('Greek (cp1253)'),
       
    67     'cp1254': _('Turkish (cp1254)'),
       
    68     'cp1255': _('Hebrew (cp1255)'),
       
    69     'cp1256': _('Arabic (cp1256)'),
       
    70     'cp1257': _('Baltic languages (cp1257)'),
       
    71     'cp1258': _('Vietnamese (cp1258)'),
       
    72     'euc_jp': _('Japanese (euc_jp)'),
       
    73     'euc_jis_2004': _('Japanese (euc_jis_2004)'),
       
    74     'euc_jisx0213': _('Japanese (euc_jisx0213)'),
       
    75     'euc_kr': _('Korean (euc_kr)'),
       
    76     'gb2312': _('Simplified Chinese (gb2312)'),
       
    77     'gbk': _('Unified Chinese (gbk)'),
       
    78     'gb18030': _('Unified Chinese (gb18030)'),
       
    79     'hz': _('Simplified Chinese (hz)'),
       
    80     'iso2022_jp': _('Japanese (iso2022_jp)'),
       
    81     'iso2022_jp_1': _('Japanese (iso2022_jp_1)'),
       
    82     'iso2022_jp_2': _('Japanese, Korean, Simplified Chinese, Western Europe, Greek (iso2022_jp_2)'),
       
    83     'iso2022_jp_2004': _('Japanese (iso2022_jp_2004)'),
       
    84     'iso2022_jp_3': _('Japanese (iso2022_jp_3)'),
       
    85     'iso2022_jp_ext': _('Japanese (iso2022_jp_ext)'),
       
    86     'iso2022_kr': _('Korean (iso2022_kr)'),
       
    87     'latin_1': _('West Europe (latin_1)'),
       
    88     'iso8859_2': _('Central and Eastern Europe (iso8859_2)'),
       
    89     'iso8859_3': _('Esperanto, Maltese (iso8859_3)'),
       
    90     'iso8859_4': _('Baltic languages (iso8859_4)'),
       
    91     'iso8859_5': _('Bulgarian, Byelorussian, Macedonian, Russian, Serbian (iso8859_5)'),
       
    92     'iso8859_6': _('Arabic (iso8859_6)'),
       
    93     'iso8859_7': _('Greek (iso8859_7)'),
       
    94     'iso8859_8': _('Hebrew (iso8859_8)'),
       
    95     'iso8859_9': _('Turkish (iso8859_9)'),
       
    96     'iso8859_10': _('Nordic languages (iso8859_10)'),
       
    97     'iso8859_13': _('Baltic languages (iso8859_13)'),
       
    98     'iso8859_14': _('Celtic languages (iso8859_14)'),
       
    99     'iso8859_15': _('Western Europe (iso8859_15)'),
       
   100     'iso8859_16': _('South-Eastern Europe (iso8859_16)'),
       
   101     'johab': _('Korean (johab)'),
       
   102     'koi8_r': _('Russian (koi8_r)'),
       
   103     'koi8_u': _('Ukrainian (koi8_u)'),
       
   104     'mac_cyrillic': _('Bulgarian, Byelorussian, Macedonian, Russian, Serbian (mac_cyrillic)'),
       
   105     'mac_greek': _('Greek (mac_greek)'),
       
   106     'mac_iceland': _('Icelandic (mac_iceland)'),
       
   107     'mac_latin2': _('Central and Eastern Europe (mac_latin2)'),
       
   108     'mac_roman': _('Western Europe (mac_roman)'),
       
   109     'mac_turkish': _('Turkish (mac_turkish)'),
       
   110     'ptcp154': _('Kazakh (ptcp154)'),
       
   111     'shift_jis': _('Japanese (shift_jis)'),
       
   112     'shift_jis_2004': _('Japanese (shift_jis_2004)'),
       
   113     'shift_jisx0213': _('Japanese (shift_jisx0213)'),
       
   114     'utf_32': _('all languages (utf_32)'),
       
   115     'utf_32_be': _('all languages (utf_32_be)'),
       
   116     'utf_32_le': _('all languages (utf_32_le)'),
       
   117     'utf_16': _('all languages (utf_16)'),
       
   118     'utf_16_be': _('all languages (BMP only - utf_16_be)'),
       
   119     'utf_16_le': _('all languages (BMP only - utf_16_le)'),
       
   120     'utf_7': _('all languages (utf_7)'),
       
   121     'utf_8': _('all languages (utf_8)'),
       
   122     'utf_8_sig': _('all languages (utf_8_sig)'),
       
   123 }
       
   124 
       
   125 
       
   126 @provider(IVocabularyFactory)
       
   127 class EncodingsVocabulary(SimpleVocabulary):
       
   128 
       
   129     def __init__(self, terms, *interfaces):
       
   130         request = check_request()
       
   131         translate = request.localizer.translate
       
   132         terms = [SimpleTerm(v, title=translate(t)) for v, t in ENCODINGS.items()]
       
   133         terms.sort(key=lambda x: x.title)
       
   134         super(EncodingsVocabulary, self).__init__(terms, *interfaces)
       
   135 
       
   136 getVocabularyRegistry().register('PyAMS encodings', EncodingsVocabulary)
       
   137 
       
   138 
       
   139 class IEncodingField(IChoice):
       
   140     """Encoding field interface"""
       
   141 
       
   142 
       
   143 @implementer(IEncodingField)
       
   144 class EncodingField(Choice):
       
   145     """Encoding field"""
       
   146 
       
   147     def __init__(self, vocabulary='PyAMS encodings', **kw):
       
   148         if 'values' in kw:
       
   149             del kw['values']
       
   150         if 'source' in kw:
       
   151             del kw['source']
       
   152         kw['vocabulary'] = vocabulary
       
   153         super(EncodingField, self).__init__(**kw)