A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

123 lines
4.6 KiB

  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. import os
  24. import sys
  25. if (sys.version_info[0] == 2 and sys.version_info[1] < 6) or \
  26. (sys.version_info[1] == 3 and sys.version_info[1] < 2):
  27. raise Exception("mwparserfromhell needs Python 2.6+ or 3.2+")
  28. from setuptools import setup, find_packages, Extension
  29. from mwparserfromhell import __version__
  30. from mwparserfromhell.compat import py26, py3k
  31. with open("README.rst", **{'encoding':'utf-8'} if py3k else {}) as fp:
  32. long_docs = fp.read()
  33. tokenizer = Extension("mwparserfromhell.parser._tokenizer",
  34. sources=["mwparserfromhell/parser/tokenizer.c"],
  35. depends=["mwparserfromhell/parser/tokenizer.h"])
  36. def optional_compile_setup(func=setup, use_ext=True, *args, **kwargs):
  37. """
  38. Wrap setup to allow optional compilation of extensions.
  39. Falls back to pure python mode (no extensions)
  40. if compilation of extensions fails.
  41. """
  42. extensions = kwargs.get('ext_modules', None)
  43. if use_ext and extensions:
  44. try:
  45. func(*args, **kwargs)
  46. return
  47. except (Exception, SystemExit) as e:
  48. print('Building extension failed: %s' % repr(e))
  49. if extensions:
  50. if use_ext:
  51. print('Falling back to pure python mode.')
  52. else:
  53. print('Using pure python mode.')
  54. del kwargs['ext_modules']
  55. # Basic algorithm to push the extension sources into
  56. # the package as data.
  57. ext_files = [(ext, filename)
  58. for ext in extensions
  59. for filename in ext.sources + ext.depends]
  60. pkg_data = kwargs.get('package_data', {})
  61. for ext, filename in ext_files:
  62. ext_name_parts = ext.name.split('.')
  63. pkg_name = '.'.join(ext_name_parts[0:-1])
  64. pkg = pkg_data.setdefault(pkg_name, [])
  65. # This assumes the extension's package name
  66. # is the same prefix as the filename.
  67. pkg.append(os.path.basename(filename))
  68. kwargs['package_data'] = pkg_data
  69. # Ensure the extension package is in the main packages list.
  70. for name in pkg_data.keys():
  71. if name not in kwargs['packages']:
  72. kwargs['packages'].append(name)
  73. func(*args, **kwargs)
  74. optional_compile_setup(
  75. name = "mwparserfromhell",
  76. packages = find_packages(exclude=("tests",)),
  77. ext_modules = [tokenizer],
  78. tests_require = ["unittest2"] if py26 else [],
  79. test_suite = "tests.discover",
  80. version = __version__,
  81. author = "Ben Kurtovic",
  82. author_email = "ben.kurtovic@gmail.com",
  83. url = "https://github.com/earwig/mwparserfromhell",
  84. description = "MWParserFromHell is a parser for MediaWiki wikicode.",
  85. long_description = long_docs,
  86. download_url = "https://github.com/earwig/mwparserfromhell/tarball/v{0}".format(__version__),
  87. keywords = "earwig mwparserfromhell wikipedia wiki mediawiki wikicode template parsing",
  88. license = "MIT License",
  89. classifiers = [
  90. "Development Status :: 4 - Beta",
  91. "Environment :: Console",
  92. "Intended Audience :: Developers",
  93. "License :: OSI Approved :: MIT License",
  94. "Operating System :: OS Independent",
  95. "Programming Language :: Python :: 2.6",
  96. "Programming Language :: Python :: 2.7",
  97. "Programming Language :: Python :: 3",
  98. "Programming Language :: Python :: 3.2",
  99. "Programming Language :: Python :: 3.3",
  100. "Programming Language :: Python :: 3.4",
  101. "Topic :: Text Processing :: Markup"
  102. ],
  103. )