__main__.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import annotations
  2. import argparse
  3. import sys
  4. from typing import Any
  5. from .slugify import slugify, DEFAULT_SEPARATOR
  6. def parse_args(argv: list[str]) -> argparse.Namespace:
  7. parser = argparse.ArgumentParser(description="Slug string")
  8. input_group = parser.add_argument_group(description="Input")
  9. input_group.add_argument("input_string", nargs='*',
  10. help='Text to slugify')
  11. input_group.add_argument("--stdin", action='store_true',
  12. help="Take the text from STDIN")
  13. parser.add_argument("--no-entities", action='store_false', dest='entities', default=True,
  14. help="Do not convert HTML entities to unicode")
  15. parser.add_argument("--no-decimal", action='store_false', dest='decimal', default=True,
  16. help="Do not convert HTML decimal to unicode")
  17. parser.add_argument("--no-hexadecimal", action='store_false', dest='hexadecimal', default=True,
  18. help="Do not convert HTML hexadecimal to unicode")
  19. parser.add_argument("--max-length", type=int, default=0,
  20. help="Output string length, 0 for no limit")
  21. parser.add_argument("--word-boundary", action='store_true', default=False,
  22. help="Truncate to complete word even if length ends up shorter than --max_length")
  23. parser.add_argument("--save-order", action='store_true', default=False,
  24. help="When set and --max_length > 0 return whole words in the initial order")
  25. parser.add_argument("--separator", type=str, default=DEFAULT_SEPARATOR,
  26. help="Separator between words. By default " + DEFAULT_SEPARATOR)
  27. parser.add_argument("--stopwords", nargs='+',
  28. help="Words to discount")
  29. parser.add_argument("--regex-pattern",
  30. help="Python regex pattern for disallowed characters")
  31. parser.add_argument("--no-lowercase", action='store_false', dest='lowercase', default=True,
  32. help="Activate case sensitivity")
  33. parser.add_argument("--replacements", nargs='+',
  34. help="""Additional replacement rules e.g. "|->or", "%%->percent".""")
  35. parser.add_argument("--allow-unicode", action='store_true', default=False,
  36. help="Allow unicode characters")
  37. args = parser.parse_args(argv[1:])
  38. if args.input_string and args.stdin:
  39. parser.error("Input strings and --stdin cannot work together")
  40. if args.replacements:
  41. def split_check(repl):
  42. SEP = '->'
  43. if SEP not in repl:
  44. parser.error("Replacements must be of the form: ORIGINAL{SEP}REPLACED".format(SEP=SEP))
  45. return repl.split(SEP, 1)
  46. args.replacements = [split_check(repl) for repl in args.replacements]
  47. if args.input_string:
  48. args.input_string = " ".join(args.input_string)
  49. elif args.stdin:
  50. args.input_string = sys.stdin.read()
  51. if not args.input_string:
  52. args.input_string = ''
  53. return args
  54. def slugify_params(args: argparse.Namespace) -> dict[str, Any]:
  55. return dict(
  56. text=args.input_string,
  57. entities=args.entities,
  58. decimal=args.decimal,
  59. hexadecimal=args.hexadecimal,
  60. max_length=args.max_length,
  61. word_boundary=args.word_boundary,
  62. save_order=args.save_order,
  63. separator=args.separator,
  64. stopwords=args.stopwords,
  65. lowercase=args.lowercase,
  66. replacements=args.replacements,
  67. allow_unicode=args.allow_unicode
  68. )
  69. def main(argv: list[str] | None = None): # pragma: no cover
  70. """ Run this program """
  71. if argv is None:
  72. argv = sys.argv
  73. args = parse_args(argv)
  74. params = slugify_params(args)
  75. try:
  76. print(slugify(**params))
  77. except KeyboardInterrupt:
  78. sys.exit(-1)
  79. if __name__ == '__main__': # pragma: no cover
  80. main()