__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # This file is part of python-bidi
  3. #
  4. # python-bidi is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Lesser General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # Copyright (C) 2008-2010 Yaacov Zamir <kzamir_a_walla.co.il>,
  17. # Copyright (C) 2010-2024 Meir kriheli <mkriheli@gmail.com>.
  18. #
  19. from .wrapper import get_base_level, get_display
  20. __all__ = ["get_base_level", "get_display"]
  21. VERSION_TUPLE = (0, 6, 0)
  22. VERSION = ".".join(str(x) for x in VERSION_TUPLE)
  23. def main():
  24. """Will be used to create the console script"""
  25. import argparse
  26. import sys
  27. parser = argparse.ArgumentParser()
  28. parser.add_argument(
  29. "-e",
  30. "--encoding",
  31. dest="encoding",
  32. default="utf-8",
  33. type=str,
  34. help="Text encoding (default: utf-8)",
  35. )
  36. parser.add_argument(
  37. "-u",
  38. "--upper-is-rtl",
  39. dest="upper_is_rtl",
  40. default=False,
  41. action="store_true",
  42. help="Treat upper case chars as strong 'R' "
  43. "for debugging (default: False), Ignored in Rust algo",
  44. )
  45. parser.add_argument(
  46. "-d",
  47. "--debug",
  48. dest="debug",
  49. default=False,
  50. action="store_true",
  51. help="Output to stderr steps taken with the algorithm",
  52. )
  53. parser.add_argument(
  54. "-b",
  55. "--base-dir",
  56. dest="base_dir",
  57. choices=["L", "R"],
  58. default=None,
  59. type=str,
  60. help="Override base direction [L|R]",
  61. )
  62. parser.add_argument(
  63. "-r",
  64. "--rust",
  65. dest="use_rust",
  66. action="store_true",
  67. help="Use the Rust unicode-bidi implemention instead of the Python one",
  68. )
  69. parser.add_argument(
  70. "-v", "--version", action="version", version=f"pybidi {VERSION}"
  71. )
  72. options, rest = parser.parse_known_args()
  73. lines = rest or sys.stdin
  74. params = {
  75. "encoding": options.encoding,
  76. "base_dir": options.base_dir,
  77. "debug": options.debug,
  78. }
  79. if options.use_rust:
  80. display_func = get_display
  81. else:
  82. from .algorithm import get_display as get_display_python
  83. display_func = get_display_python
  84. params["upper_is_rtl"] = options.upper_is_rtl
  85. for line in lines:
  86. display = display_func(line, **params)
  87. # adjust the encoding as unicode, to match the output encoding
  88. if not isinstance(display, str):
  89. display = bytes(display).decode(options.encoding)
  90. print(display, end="")
  91. if __name__ == "__main__":
  92. main()