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
Wrap
Python Compiled Bytecode
|
2011-03-24
|
9KB
|
272 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
'''
enchant.utils: Misc utilities for the enchant package
This module provides miscellaneous utilities for use with the
enchant spellchecking package. Currently available functionality
includes:
* string/unicode compatibility wrappers
* functions for dealing with locale/language settings
* ability to list supporting data files (win32 only)
* functions for bundling supporting data files from a build
'''
import os
import sys
import codecs
from enchant.errors import *
try:
import locale
except ImportError:
locale = None
try:
unicode = unicode
except NameError:
str = str
unicode = str
bytes = bytes
basestring = (str, bytes)
str = str
unicode = unicode
bytes = str
basestring = basestring
def raw_unicode(raw):
'''Make a unicode string from a raw string.
This function takes a string containing unicode escape characters,
and returns the corresponding unicode string. Useful for writing
unicode string literals in your python source while being upwards-
compatible with Python 3. For example, instead of doing this:
s = u"hello\\u2149" # syntax error in Python 3
Or this:
s = "hello\\u2149" # not what you want in Python 2.x
You can do this:
s = raw_unicode(r"hello\\u2149") # works everywhere!
'''
return raw.encode('utf8').decode('unicode-escape')
def raw_bytes(raw):
'''Make a bytes object out of a raw string.
This is analogous to raw_unicode, but processes byte escape characters
to produce a bytes object.
'''
return codecs.escape_decode(raw)[0]
class EnchantStr(str):
"""String subclass for interfacing with enchant C library.
This class encapsulates the logic for interfacing between python native
string/unicode objects and the underlying enchant library, which expects
all strings to be UTF-8 character arrays. It is a subclass of the
default string class 'str' - on Python 2.x that makes it an ascii string,
on Python 3.x it is a unicode object.
Initialise it with a string or unicode object, and use the encode() method
to obtain an object suitable for passing to the underlying C library.
When strings are read back into python, use decode(s) to translate them
back into the appropriate python-level string type.
This allows us to following the common Python 2.x idiom of returning
unicode when unicode is passed in, and byte strings otherwise. It also
lets the interface be upwards-compatible with Python 3, in which string
objects are unicode by default.
"""
def __new__(cls, value):
'''EnchantStr data constructor.
This method records whether the initial string was unicode, then
simply passes it along to the default string constructor.
'''
if type(value) is unicode:
was_unicode = True
if str is not unicode:
value = value.encode('utf-8')
else:
was_unicode = False
if str is not bytes:
raise Error("Don't pass bytestrings to pyenchant")
self = str.__new__(cls, value)
self._was_unicode = was_unicode
return self
def encode(self):
'''Encode this string into a form usable by the enchant C library.'''
if str is unicode:
return str.encode(self, 'utf-8')
return None
def decode(self, value):
'''Decode a string returned by the enchant C library.'''
if self._was_unicode:
if str is unicode:
if isinstance(value, str):
value = value.encode()
return value.decode('utf-8')
return None.decode('utf-8')
return value
def printf(values, sep = ' ', end = '\n', file = None):
'''Compatability wrapper from print statement/function.
This function is a simple Python2/Python3 compatability wrapper
for printing to stdout.
'''
if file is None:
file = sys.stdout
file.write(sep.join(map(str, values)))
file.write(end)
try:
next = next
except NameError:
def next(iter):
'''Compatability wrapper for advancing an iterator.'''
return iter.next()
try:
xrange = xrange
except NameError:
xrange = range
def get_default_language(default = None):
"""Determine the user's default language, if possible.
This function uses the 'locale' module to try to determine
the user's preferred language. The return value is as
follows:
* if a locale is available for the LC_MESSAGES category,
that language is used
* if a default locale is available, that language is used
* if the keyword argument <default> is given, it is used
* if nothing else works, None is returned
Note that determining the user's language is in general only
possible if they have set the necessary environment variables
on their system.
"""
try:
import locale as locale
tag = locale.getlocale()[0]
if tag is None:
tag = locale.getdefaultlocale()[0]
if tag is None:
raise Error('No default language available')
return tag
except Exception:
pass
return default
get_default_language._DOC_ERRORS = [
'LC']
def get_resource_filename(resname):
"""Get the absolute path to the named resource file.
This serves widely the same purpose as pkg_resources.resource_filename(),
but tries to avoid loading pkg_resources unless we're actually in
an egg.
"""
path = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(path, resname)
if os.path.exists(path):
return path
if None(sys, 'frozen'):
exe_path = unicode(sys.executable, sys.getfilesystemencoding())
exe_dir = os.path.dirname(exe_path)
path = os.path.join(exe_dir, resname)
if os.path.exists(path):
return path
import pkg_resources as pkg_resources
try:
path = pkg_resources.resource_filename('enchant', resname)
except KeyError:
pass
path = os.path.abspath(path)
if os.path.exists(path):
return path
raise None("Could not locate resource '%s'" % (resname,))
def win32_data_files():
"""Get list of supporting data files, for use with setup.py
This function returns a list of the supporting data files available
to the running version of PyEnchant. This is in the format expected
by the data_files argument of the distutils setup function. It's
very useful, for example, for including the data files in an executable
produced by py2exe.
Only really tested on the win32 platform (it's the only platform for
which we ship our own supporting data files)
"""
try:
libEnchant = get_resource_filename('libenchant.dll')
except Error:
libEnchant = get_resource_filename('libenchant-1.dll')
mainDir = os.path.dirname(libEnchant)
dataFiles = [
('', [
libEnchant])]
for dll in os.listdir(mainDir):
if not dll.endswith('.dll'):
continue
for prefix in ('iconv', 'intl', 'libglib', 'libgmodule'):
if dll.startswith(prefix):
break
continue
continue
dataFiles[0][1].append(os.path.join(mainDir, dll))
dataDirs = ('share/enchant/myspell', 'share/enchant/ispell', 'lib/enchant')
for dataDir in dataDirs:
files = []
fullDir = os.path.join(mainDir, os.path.normpath(dataDir))
for fn in os.listdir(fullDir):
fullFn = os.path.join(fullDir, fn)
if os.path.isfile(fullFn):
files.append(fullFn)
continue
dataFiles.append((dataDir, files))
return dataFiles
win32_data_files._DOC_ERRORS = [
'py',
'py',
'exe']