A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

101 lignes
3.3 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 sys
  26. from enum import Enum
  27. from setuptools import Extension, setup
  28. from setuptools.command.build_ext import build_ext
  29. class UseExtension(Enum):
  30. REQUIRED = 1
  31. OPTIONAL = 2
  32. IGNORED = 3
  33. if "WITH_EXTENSION" in os.environ:
  34. if "WITHOUT_EXTENSION" in os.environ:
  35. raise RuntimeError("Cannot set both $WITH_EXTENSION and $WITHOUT_EXTENSION")
  36. value = os.environ["WITH_EXTENSION"].lower()
  37. if value in ("1", "true", "yes", "required"):
  38. use_extension = UseExtension.REQUIRED
  39. elif value == "optional":
  40. use_extension = UseExtension.OPTIONAL
  41. elif value in ("0", "false", "no"):
  42. use_extension = UseExtension.IGNORED
  43. else:
  44. raise RuntimeError(
  45. f"Unknown value for $WITH_EXTENSION; should be '1', '0', or 'optional', "
  46. f"but found {value!r}"
  47. )
  48. elif "WITHOUT_EXTENSION" in os.environ:
  49. value = os.environ["WITHOUT_EXTENSION"].lower()
  50. if value in ("1", "true", "yes"):
  51. use_extension = UseExtension.IGNORED
  52. elif value in ("0", "false", "no"):
  53. use_extension = UseExtension.REQUIRED
  54. else:
  55. raise RuntimeError(
  56. f"Unknown value for $WITHOUT_EXTENSION; should be '1', or '0', "
  57. f"but found {value!r}"
  58. )
  59. else:
  60. use_extension = UseExtension.REQUIRED
  61. if use_extension == UseExtension.IGNORED:
  62. ext_modules = []
  63. else:
  64. tokenizer = Extension(
  65. "mwparserfromhell.parser._tokenizer",
  66. sources=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.c")),
  67. depends=sorted(glob.glob("src/mwparserfromhell/parser/ctokenizer/*.h")),
  68. optional=use_extension == UseExtension.OPTIONAL,
  69. )
  70. ext_modules = [tokenizer]
  71. def build_ext_patched(self):
  72. try:
  73. build_ext_original(self)
  74. except Exception:
  75. print(
  76. """
  77. **********
  78. Note: To avoid building the C tokenizer extension, set the environment variable \
  79. `WITH_EXTENSION=0`.
  80. This will fall back to a pure-Python tokenizer.
  81. **********
  82. """,
  83. file=sys.stderr,
  84. )
  85. raise
  86. build_ext.run, build_ext_original = build_ext_patched, build_ext.run
  87. setup(ext_modules=ext_modules)