lang-check.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. """Check lang files."""
  3. from argparse import ArgumentParser
  4. from traceback import print_exc
  5. from sys import stderr
  6. def parse_txt(lang, no_warning):
  7. """Parse txt file and check strings to display definition."""
  8. if lang == "en":
  9. file_path = "lang_en.txt"
  10. else:
  11. file_path = "lang_en_%s.txt" % lang
  12. lines = 1
  13. with open(file_path) as src:
  14. while True:
  15. comment = src.readline().split(' ')
  16. src.readline() # source
  17. translation = src.readline()[:-1]
  18. cols = None
  19. rows = None
  20. for item in comment[1:]:
  21. key, val = item.split('=')
  22. if key == 'c':
  23. cols = int(val)
  24. elif key == 'r':
  25. rows = int(val)
  26. else:
  27. raise RuntimeError(
  28. "Unknown display definition %s on line %d" %
  29. (' '.join(comment), lines))
  30. if cols is None and rows is None:
  31. if not no_warning:
  32. print("[W]: No display definition on line %d" % lines)
  33. cols = len(translation) # propably fullscreen
  34. if rows is None:
  35. rows = 1
  36. if len(translation)-2 > cols*rows:
  37. stderr.write(
  38. "[E]: Text %s is longer then definiton on line %d\n" %
  39. (translation, lines))
  40. stderr.flush()
  41. if len(src.readline()) != 1: # empty line
  42. break
  43. lines += 4
  44. def main():
  45. """Main function."""
  46. parser = ArgumentParser(
  47. description=__doc__,
  48. usage="$(prog)s lang")
  49. parser.add_argument(
  50. "lang", nargs='?', default="en", type=str,
  51. help="Check lang file (en|cs|de|es|fr|nl|it|pl)")
  52. parser.add_argument(
  53. "--no-warning", action="store_true",
  54. help="Disable warnings")
  55. args = parser.parse_args()
  56. try:
  57. parse_txt(args.lang, args.no_warning)
  58. return 0
  59. except Exception as exc:
  60. print_exc()
  61. parser.error("%s" % exc)
  62. return 1
  63. if __name__ == "__main__":
  64. exit(main())