A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 lines
3.4 KiB

  1. #! /usr/bin/env python
  2. #
  3. # Copyright (C) 2012-2025 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import annotations
  23. import glob
  24. import os
  25. import platform
  26. import sys
  27. from enum import Enum
  28. from setuptools import Extension, setup
  29. from setuptools.command.build_ext import build_ext
  30. class UseExtension(Enum):
  31. REQUIRED = 1
  32. OPTIONAL = 2
  33. IGNORED = 3
  34. if "WITH_EXTENSION" in os.environ:
  35. if "WITHOUT_EXTENSION" in os.environ:
  36. raise RuntimeError("Cannot set both $WITH_EXTENSION and $WITHOUT_EXTENSION")
  37. value = os.environ["WITH_EXTENSION"].lower()
  38. if value in ("1", "true", "yes", "required"):
  39. use_extension = UseExtension.REQUIRED
  40. elif value == "optional":
  41. use_extension = UseExtension.OPTIONAL
  42. elif value in ("0", "false", "no"):
  43. use_extension = UseExtension.IGNORED
  44. else:
  45. raise RuntimeError(
  46. f"Unknown value for $WITH_EXTENSION; should be '1', '0', or 'optional', "
  47. f"but found {value!r}"
  48. )
  49. elif "WITHOUT_EXTENSION" in os.environ:
  50. value = os.environ["WITHOUT_EXTENSION"].lower()
  51. if value in ("1", "true", "yes"):
  52. use_extension = UseExtension.IGNORED
  53. elif value in ("0", "false", "no"):
  54. use_extension = UseExtension.REQUIRED
  55. else:
  56. raise RuntimeError(
  57. f"Unknown value for $WITHOUT_EXTENSION; should be '1', or '0', "
  58. f"but found {value!r}"
  59. )
  60. elif platform.python_implementation() == "CPython":
  61. use_extension = UseExtension.REQUIRED
  62. else:
  63. use_extension = UseExtension.IGNORED
  64. if use_extension == UseExtension.IGNORED:
  65. ext_modules = []
  66. else:
  67. tokenizer = Extension(
  68. "mwparserfromhell.parser._tokenizer",
  69. sources=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.c")),
  70. depends=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.h")),
  71. optional=use_extension == UseExtension.OPTIONAL,
  72. )
  73. ext_modules = [tokenizer]
  74. def build_ext_patched(self):
  75. try:
  76. build_ext_original(self)
  77. except Exception:
  78. print(
  79. """
  80. **********
  81. Note: To avoid building the C tokenizer extension, set the environment variable \
  82. `WITH_EXTENSION=0`.
  83. This will fall back to a pure-Python tokenizer.
  84. **********
  85. """,
  86. file=sys.stderr,
  87. )
  88. raise
  89. build_ext.run, build_ext_original = build_ext_patched, build_ext.run
  90. setup(ext_modules=ext_modules)