<filename>PyHyphen-2.0.5/setup.py
# setup.py for the PyHyphen hyphenation package
# (c) Dr. Leo (fhaxbox66 <at> gmail >dot< com)

import sys, os, shutil, imp, py_compile, codecs, locale, platform
from string import Template
from distutils.core import setup, Extension
from warnings import warn


# URL of the default repository. It goes into config.py.
# Change this if you want to download dictionaries from somewhere else by default.
# Note that you can also specify the repository individualy
# when calling hyphen.dictools.install.
default_repo = 'http://cgit.freedesktop.org/libreoffice/dictionaries/plain/'


# Copy version-specific files
# to be copied from 2.x/
files_from_2x = {
    '__init__.py' : './hyphen/',
    'config.py' : './hyphen/',
    'dictools.py' : './hyphen/'}

# from either 2.x/ or 3.x/
files_from_any = {
    'hnjmodule.c' : 'src/',
    'textwrap2.py' : './'}
        

#copy version-specific files
ver = sys.version[0]
py3k = (ver == '3')
if not os.path.exists('hyphen'):
    os.mkdir('hyphen')
for file_name, dest in files_from_2x.items():
    shutil.copy('2.x/' + file_name, dest + file_name)

for file_name, dest in files_from_any.items():
    shutil.copy(ver + '.x/' + file_name, dest + file_name)


# refactor 2to3
if py3k:
    import lib2to3.main
    lib2to3.main.main('lib2to3.fixes', args = '--no-diffs -wn -f unicode -f urllib \
        hyphen'.split())


longdescr = open('README.txt', 'r').read()



arg_dict = dict(
    name = "PyHyphen",
    version = "2.0.5",
    author = "<NAME>",
    author_email = "<EMAIL>",
    url = "http://pyhyphen.googlecode.com",
    description = "The hyphenation library of LibreOffice and FireFox wrapped for Python",
    long_description = longdescr,
    classifiers = [
        'Intended Audience :: Developers',
         'Development Status :: 5 - Production/Stable',
        'License :: OSI Approved',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
        'Programming Language :: C',
                'Topic :: Text Processing',
                'Topic :: Text Processing :: Linguistic'
    ],
    packages = ['hyphen'],
    ext_modules = [
      Extension('hyphen.hnj', ['src/hnjmodule.c',
                                  'src/hyphen.c',
                                   'src/hnjalloc.c' ],
                                   include_dirs = ['include'])],
    py_modules = ['textwrap2'],
    provides = ['hyphen', 'textwrap2']
)


# Check for a binary shipping with this distribution and use it instead of compiling
# the C sources, unless --force_build_ext is given.
if len(set(('install', 'bdist_wininst', 'bdist')) - set(sys.argv)) < 3:
    if  '--force_build_ext' in sys.argv:
        sys.argv.remove('--force_build_ext')
    else:
        # construct string describing platform
        if platform.system() == 'Windows':
            if sys.maxsize > 2**32: platform_descr = 'amd64'
            else: platform_descr = 'win32'
        else: platform_descr = platform.system()
        
        bin_file = ''.join(('bin/hnj', '.', platform_descr, '-', sys.version[:3], '.pyd'))
        if os.path.exists(bin_file):
            shutil.copy(bin_file, './hyphen/hnj.pyd')
            arg_dict['package_data'] = {'hyphen' : ['hnj.pyd']}
            arg_dict.pop('ext_modules')
            print("""Found a suitable binary version of the C extension module.
            This binary will be installed rather than building it from source.
            However, if you prefer compiling, reenter 'python setup.py <command> --force_build_ext'.""")


setup(**arg_dict)

# clean up
shutil.rmtree('hyphen') # it would disturb the following import of hyphen
os.remove('textwrap2.py')
os.remove('src/hnjmodule.c')


# Configure the path for dictionaries in config.py
if 'install' in sys.argv:
    print("Adjusting /.../hyphen/config.py... ")
    # We catch ImportErrors to handle situations where the
    # hyphen package has been
    # installed in a directory that is not listed in
    # sys.path. This occurs, e.g.,
    # when creating a Debian package.
    try:
        pkg_path = imp.find_module('hyphen')[1]
        if ver == '2':
            pkg_path = pkg_path.decode(sys.getfilesystemencoding())
        mod_path = os.path.join(pkg_path, 'config.py')
        sep = os.path.sep
        if sep != '/':
            pkg_path = pkg_path.replace(sep, '/')
            mod_path = mod_path.replace(sep, '/')
        content = codecs.open(mod_path, 'r', 'utf8').read()
        new_content = Template(content).substitute(path = pkg_path,
            repo = default_repo)
        
        # Write the new config.py
        codecs.open(mod_path, 'w', 'utf8').write(new_content)
        py_compile.compile(mod_path)
        print("Done.")
        
        # Delete any existing dict registry file
        reg_file = pkg_path + '/hyphen_dict_info.pickle'
        if os.path.exists(reg_file):
            os.remove(reg_file)

        # Install dictionaries
        if '--no_dictionaries' not in sys.argv:
            from hyphen.dictools import install
            print('Installing dictionaries... en_US ...')
            install('en_US')
            
            # Install dict for local language if needed
            try:
                locale.setlocale(locale.LC_ALL, '')
                local_lang = locale.getlocale()[0]
                # Install local dict only if locale has been read (= is not None)
                # and local_lang is not en_US.
                if local_lang and local_lang != 'en_US':
                    print(local_lang + ' ')
                    install(local_lang)
                    print('Done.')
            except Exception:
                warn('Could not install dictionary for local language.')

            
    except ImportError:
        warn("""Could not import hyphen package.
        You may wish to adjust config.py
            manually or run setup.py with different options.
            No dictionary has been installed.""")

    