__init__.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from csv import reader
  2. from difflib import ndiff
  3. from importlib import reload
  4. from json import loads as jloads
  5. from logging import getLogger
  6. from os import path
  7. import scriptshifter.tables
  8. from scriptshifter.trans import transliterate
  9. TEST_DIR = path.dirname(path.realpath(__file__))
  10. TEST_DATA_DIR = path.join(TEST_DIR, "data")
  11. logger = getLogger(__name__)
  12. def reload_tables():
  13. reload(scriptshifter.tables) # Reload new config dir.
  14. from scriptshifter import tables
  15. tables.list_tables.cache_clear()
  16. tables.load_table.cache_clear()
  17. return tables
  18. def test_sample(dset):
  19. """
  20. Test an individual sample set and produce a human-readable report.
  21. Used outside of automated tests.
  22. @param dset (str): sample set name (without the .csv extension) found in
  23. the `data/script_samples` directory.
  24. """
  25. deltas = []
  26. dset_fpath = path.join(TEST_DATA_DIR, "script_samples", dset + ".csv")
  27. log_fpath = path.join(TEST_DATA_DIR, f"test_{dset}.log")
  28. with open(dset_fpath, newline="") as fh:
  29. csv = reader(fh)
  30. for row in csv:
  31. lang, script, rom = row[:3]
  32. if not lang:
  33. continue
  34. opts = jloads(row[3]) if len(row) > 3 and row[3] else {}
  35. trans, warnings = transliterate(
  36. script, lang, t_dir="s2r",
  37. capitalize=opts.get("capitalize"), options=opts)
  38. if (trans == rom):
  39. print(".", end="")
  40. else:
  41. print("F", end="")
  42. deltas.append((lang, script, ndiff([trans], [rom])))
  43. with open(log_fpath, "w") as fh:
  44. # If no deltas, just truncate the file.
  45. for lang, script, delta in deltas:
  46. fh.write(f"Language: {lang}\n")
  47. fh.write(f"Original: {script}\n")
  48. for dline in delta:
  49. fh.write(dline.strip() + "\n")
  50. fh.write("\n\n")
  51. ct = len(deltas)
  52. if ct > 0:
  53. print(f"{ct} failed tests. See report at {log_fpath}")
  54. else:
  55. print("All tests passed.")