home *** CD-ROM | disk | FTP | other *** search
/ Freelog 116 / FreelogNo116-JuilletSeptembre2013.iso / Bureautique / gImageReader / gimagereader_0.9-1_win32.exe / bin / enchant / utils.pyc (.txt) < prev   
Python Compiled Bytecode  |  2011-03-24  |  9KB  |  272 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''
  5.  
  6.     enchant.utils:    Misc utilities for the enchant package
  7.     
  8.     This module provides miscellaneous utilities for use with the
  9.     enchant spellchecking package.  Currently available functionality
  10.     includes:
  11.         
  12.         * string/unicode compatibility wrappers
  13.         * functions for dealing with locale/language settings
  14.         * ability to list supporting data files (win32 only)
  15.         * functions for bundling supporting data files from a build
  16.           
  17. '''
  18. import os
  19. import sys
  20. import codecs
  21. from enchant.errors import *
  22.  
  23. try:
  24.     import locale
  25. except ImportError:
  26.     locale = None
  27.  
  28.  
  29. try:
  30.     unicode = unicode
  31. except NameError:
  32.     str = str
  33.     unicode = str
  34.     bytes = bytes
  35.     basestring = (str, bytes)
  36.  
  37. str = str
  38. unicode = unicode
  39. bytes = str
  40. basestring = basestring
  41.  
  42. def raw_unicode(raw):
  43.     '''Make a unicode string from a raw string.
  44.  
  45.     This function takes a string containing unicode escape characters,
  46.     and returns the corresponding unicode string.  Useful for writing
  47.     unicode string literals in your python source while being upwards-
  48.     compatible with Python 3.  For example, instead of doing this:
  49.  
  50.       s = u"hello\\u2149"  # syntax error in Python 3
  51.  
  52.     Or this:
  53.  
  54.       s = "hello\\u2149"   # not what you want in Python 2.x
  55.  
  56.     You can do this:
  57.  
  58.       s = raw_unicode(r"hello\\u2149")  # works everywhere!
  59.  
  60.     '''
  61.     return raw.encode('utf8').decode('unicode-escape')
  62.  
  63.  
  64. def raw_bytes(raw):
  65.     '''Make a bytes object out of a raw string.
  66.  
  67.     This is analogous to raw_unicode, but processes byte escape characters
  68.     to produce a bytes object.
  69.     '''
  70.     return codecs.escape_decode(raw)[0]
  71.  
  72.  
  73. class EnchantStr(str):
  74.     """String subclass for interfacing with enchant C library.
  75.  
  76.     This class encapsulates the logic for interfacing between python native
  77.     string/unicode objects and the underlying enchant library, which expects
  78.     all strings to be UTF-8 character arrays.  It is a subclass of the
  79.     default string class 'str' - on Python 2.x that makes it an ascii string,
  80.     on Python 3.x it is a unicode object.
  81.  
  82.     Initialise it with a string or unicode object, and use the encode() method
  83.     to obtain an object suitable for passing to the underlying C library.
  84.     When strings are read back into python, use decode(s) to translate them
  85.     back into the appropriate python-level string type.
  86.  
  87.     This allows us to following the common Python 2.x idiom of returning
  88.     unicode when unicode is passed in, and byte strings otherwise.  It also
  89.     lets the interface be upwards-compatible with Python 3, in which string
  90.     objects are unicode by default.
  91.     """
  92.     
  93.     def __new__(cls, value):
  94.         '''EnchantStr data constructor.
  95.  
  96.         This method records whether the initial string was unicode, then
  97.         simply passes it along to the default string constructor.
  98.         '''
  99.         if type(value) is unicode:
  100.             was_unicode = True
  101.             if str is not unicode:
  102.                 value = value.encode('utf-8')
  103.             
  104.         else:
  105.             was_unicode = False
  106.             if str is not bytes:
  107.                 raise Error("Don't pass bytestrings to pyenchant")
  108.         self = str.__new__(cls, value)
  109.         self._was_unicode = was_unicode
  110.         return self
  111.  
  112.     
  113.     def encode(self):
  114.         '''Encode this string into a form usable by the enchant C library.'''
  115.         if str is unicode:
  116.             return str.encode(self, 'utf-8')
  117.         return None
  118.  
  119.     
  120.     def decode(self, value):
  121.         '''Decode a string returned by the enchant C library.'''
  122.         if self._was_unicode:
  123.             if str is unicode:
  124.                 if isinstance(value, str):
  125.                     value = value.encode()
  126.                 return value.decode('utf-8')
  127.             return None.decode('utf-8')
  128.         return value
  129.  
  130.  
  131.  
  132. def printf(values, sep = ' ', end = '\n', file = None):
  133.     '''Compatability wrapper from print statement/function.
  134.  
  135.     This function is a simple Python2/Python3 compatability wrapper
  136.     for printing to stdout.
  137.     '''
  138.     if file is None:
  139.         file = sys.stdout
  140.     file.write(sep.join(map(str, values)))
  141.     file.write(end)
  142.  
  143.  
  144. try:
  145.     next = next
  146. except NameError:
  147.     
  148.     def next(iter):
  149.         '''Compatability wrapper for advancing an iterator.'''
  150.         return iter.next()
  151.  
  152.  
  153.  
  154. try:
  155.     xrange = xrange
  156. except NameError:
  157.     xrange = range
  158.  
  159.  
  160. def get_default_language(default = None):
  161.     """Determine the user's default language, if possible.
  162.     
  163.     This function uses the 'locale' module to try to determine
  164.     the user's preferred language.  The return value is as
  165.     follows:
  166.         
  167.         * if a locale is available for the LC_MESSAGES category,
  168.           that language is used
  169.         * if a default locale is available, that language is used
  170.         * if the keyword argument <default> is given, it is used
  171.         * if nothing else works, None is returned
  172.         
  173.     Note that determining the user's language is in general only
  174.     possible if they have set the necessary environment variables
  175.     on their system.
  176.     """
  177.     
  178.     try:
  179.         import locale as locale
  180.         tag = locale.getlocale()[0]
  181.         if tag is None:
  182.             tag = locale.getdefaultlocale()[0]
  183.             if tag is None:
  184.                 raise Error('No default language available')
  185.         return tag
  186.     except Exception:
  187.         pass
  188.  
  189.     return default
  190.  
  191. get_default_language._DOC_ERRORS = [
  192.     'LC']
  193.  
  194. def get_resource_filename(resname):
  195.     """Get the absolute path to the named resource file.
  196.  
  197.     This serves widely the same purpose as pkg_resources.resource_filename(),
  198.     but tries to avoid loading pkg_resources unless we're actually in
  199.     an egg.
  200.     """
  201.     path = os.path.dirname(os.path.abspath(__file__))
  202.     path = os.path.join(path, resname)
  203.     if os.path.exists(path):
  204.         return path
  205.     if None(sys, 'frozen'):
  206.         exe_path = unicode(sys.executable, sys.getfilesystemencoding())
  207.         exe_dir = os.path.dirname(exe_path)
  208.         path = os.path.join(exe_dir, resname)
  209.         if os.path.exists(path):
  210.             return path
  211.     import pkg_resources as pkg_resources
  212.     
  213.     try:
  214.         path = pkg_resources.resource_filename('enchant', resname)
  215.     except KeyError:
  216.         pass
  217.  
  218.     path = os.path.abspath(path)
  219.     if os.path.exists(path):
  220.         return path
  221.     raise None("Could not locate resource '%s'" % (resname,))
  222.  
  223.  
  224. def win32_data_files():
  225.     """Get list of supporting data files, for use with setup.py
  226.     
  227.     This function returns a list of the supporting data files available
  228.     to the running version of PyEnchant.  This is in the format expected
  229.     by the data_files argument of the distutils setup function.  It's
  230.     very useful, for example, for including the data files in an executable
  231.     produced by py2exe.
  232.     
  233.     Only really tested on the win32 platform (it's the only platform for
  234.     which we ship our own supporting data files)
  235.     """
  236.     
  237.     try:
  238.         libEnchant = get_resource_filename('libenchant.dll')
  239.     except Error:
  240.         libEnchant = get_resource_filename('libenchant-1.dll')
  241.  
  242.     mainDir = os.path.dirname(libEnchant)
  243.     dataFiles = [
  244.         ('', [
  245.             libEnchant])]
  246.     for dll in os.listdir(mainDir):
  247.         if not dll.endswith('.dll'):
  248.             continue
  249.         for prefix in ('iconv', 'intl', 'libglib', 'libgmodule'):
  250.             if dll.startswith(prefix):
  251.                 break
  252.                 continue
  253.             continue
  254.             dataFiles[0][1].append(os.path.join(mainDir, dll))
  255.     dataDirs = ('share/enchant/myspell', 'share/enchant/ispell', 'lib/enchant')
  256.     for dataDir in dataDirs:
  257.         files = []
  258.         fullDir = os.path.join(mainDir, os.path.normpath(dataDir))
  259.         for fn in os.listdir(fullDir):
  260.             fullFn = os.path.join(fullDir, fn)
  261.             if os.path.isfile(fullFn):
  262.                 files.append(fullFn)
  263.                 continue
  264.         dataFiles.append((dataDir, files))
  265.     
  266.     return dataFiles
  267.  
  268. win32_data_files._DOC_ERRORS = [
  269.     'py',
  270.     'py',
  271.     'exe']
  272.