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