PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jni/harfbuzz-ng/test/shaping/hb_test_tools.py

https://gitlab.com/Smileyt/i18nWidgets
Python | 519 lines | 444 code | 62 blank | 13 comment | 84 complexity | b0647c649fc2455c1462fb58ae1b8e25 MD5 | raw file
  1. #!/usr/bin/python
  2. import sys, os, re, difflib, unicodedata, errno, cgi
  3. from itertools import *
  4. diff_symbols = "-+=*&^%$#@!~/"
  5. diff_colors = ['red', 'green', 'blue']
  6. class ColorFormatter:
  7. class Null:
  8. @staticmethod
  9. def start_color (c): return ''
  10. @staticmethod
  11. def end_color (): return ''
  12. @staticmethod
  13. def escape (s): return s
  14. @staticmethod
  15. def newline (): return '\n'
  16. class ANSI:
  17. @staticmethod
  18. def start_color (c):
  19. return {
  20. 'red': '\033[41;37;1m',
  21. 'green': '\033[42;37;1m',
  22. 'blue': '\033[44;37;1m',
  23. }[c]
  24. @staticmethod
  25. def end_color ():
  26. return '\033[m'
  27. @staticmethod
  28. def escape (s): return s
  29. @staticmethod
  30. def newline (): return '\n'
  31. class HTML:
  32. @staticmethod
  33. def start_color (c):
  34. return '<span style="background:%s">' % c
  35. @staticmethod
  36. def end_color ():
  37. return '</span>'
  38. @staticmethod
  39. def escape (s): return cgi.escape (s)
  40. @staticmethod
  41. def newline (): return '<br/>\n'
  42. @staticmethod
  43. def Auto (argv = [], out = sys.stdout):
  44. format = ColorFormatter.ANSI
  45. if "--format" in argv:
  46. argv.remove ("--format")
  47. format = ColorFormatter.ANSI
  48. if "--format=ansi" in argv:
  49. argv.remove ("--format=ansi")
  50. format = ColorFormatter.ANSI
  51. if "--format=html" in argv:
  52. argv.remove ("--format=html")
  53. format = ColorFormatter.HTML
  54. if "--no-format" in argv:
  55. argv.remove ("--no-format")
  56. format = ColorFormatter.Null
  57. return format
  58. class DiffColorizer:
  59. diff_regex = re.compile ('([a-za-z0-9_]*)([^a-za-z0-9_]?)')
  60. def __init__ (self, formatter, colors=diff_colors, symbols=diff_symbols):
  61. self.formatter = formatter
  62. self.colors = colors
  63. self.symbols = symbols
  64. def colorize_lines (self, lines):
  65. lines = (l if l else '' for l in lines)
  66. ss = [self.diff_regex.sub (r'\1\n\2\n', l).splitlines (True) for l in lines]
  67. oo = ["",""]
  68. st = [False, False]
  69. for l in difflib.Differ().compare (*ss):
  70. if l[0] == '?':
  71. continue
  72. if l[0] == ' ':
  73. for i in range(2):
  74. if st[i]:
  75. oo[i] += self.formatter.end_color ()
  76. st[i] = False
  77. oo = [o + self.formatter.escape (l[2:]) for o in oo]
  78. continue
  79. if l[0] in self.symbols:
  80. i = self.symbols.index (l[0])
  81. if not st[i]:
  82. oo[i] += self.formatter.start_color (self.colors[i])
  83. st[i] = True
  84. oo[i] += self.formatter.escape (l[2:])
  85. continue
  86. for i in range(2):
  87. if st[i]:
  88. oo[i] += self.formatter.end_color ()
  89. st[i] = False
  90. oo = [o.replace ('\n', '') for o in oo]
  91. return [s1+s2+self.formatter.newline () for (s1,s2) in zip (self.symbols, oo) if s2]
  92. def colorize_diff (self, f):
  93. lines = [None, None]
  94. for l in f:
  95. if l[0] not in self.symbols:
  96. yield self.formatter.escape (l).replace ('\n', self.formatter.newline ())
  97. continue
  98. i = self.symbols.index (l[0])
  99. if lines[i]:
  100. # Flush
  101. for line in self.colorize_lines (lines):
  102. yield line
  103. lines = [None, None]
  104. lines[i] = l[1:]
  105. if (all (lines)):
  106. # Flush
  107. for line in self.colorize_lines (lines):
  108. yield line
  109. lines = [None, None]
  110. if (any (lines)):
  111. # Flush
  112. for line in self.colorize_lines (lines):
  113. yield line
  114. class ZipDiffer:
  115. @staticmethod
  116. def diff_files (files, symbols=diff_symbols):
  117. files = tuple (files) # in case it's a generator, copy it
  118. try:
  119. for lines in izip_longest (*files):
  120. if all (lines[0] == line for line in lines[1:]):
  121. sys.stdout.writelines ([" ", lines[0]])
  122. continue
  123. for i, l in enumerate (lines):
  124. if l:
  125. sys.stdout.writelines ([symbols[i], l])
  126. except IOError as e:
  127. if e.errno != errno.EPIPE:
  128. print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror)
  129. sys.exit (1)
  130. class DiffFilters:
  131. @staticmethod
  132. def filter_failures (f):
  133. for key, lines in DiffHelpers.separate_test_cases (f):
  134. lines = list (lines)
  135. if not DiffHelpers.test_passed (lines):
  136. for l in lines: yield l
  137. class Stat:
  138. def __init__ (self):
  139. self.count = 0
  140. self.freq = 0
  141. def add (self, test):
  142. self.count += 1
  143. self.freq += test.freq
  144. class Stats:
  145. def __init__ (self):
  146. self.passed = Stat ()
  147. self.failed = Stat ()
  148. self.total = Stat ()
  149. def add (self, test):
  150. self.total.add (test)
  151. if test.passed:
  152. self.passed.add (test)
  153. else:
  154. self.failed.add (test)
  155. def mean (self):
  156. return float (self.passed.count) / self.total.count
  157. def variance (self):
  158. return (float (self.passed.count) / self.total.count) * \
  159. (float (self.failed.count) / self.total.count)
  160. def stddev (self):
  161. return self.variance () ** .5
  162. def zscore (self, population):
  163. """Calculate the standard score.
  164. Population is the Stats for population.
  165. Self is Stats for sample.
  166. Returns larger absolute value if sample is highly unlikely to be random.
  167. Anything outside of -3..+3 is very unlikely to be random.
  168. See: http://en.wikipedia.org/wiki/Standard_score"""
  169. return (self.mean () - population.mean ()) / population.stddev ()
  170. class DiffSinks:
  171. @staticmethod
  172. def print_stat (f):
  173. passed = 0
  174. failed = 0
  175. # XXX port to Stats, but that would really slow us down here
  176. for key, lines in DiffHelpers.separate_test_cases (f):
  177. if DiffHelpers.test_passed (lines):
  178. passed += 1
  179. else:
  180. failed += 1
  181. total = passed + failed
  182. print "%d out of %d tests passed. %d failed (%g%%)" % (passed, total, failed, 100. * failed / total)
  183. @staticmethod
  184. def print_ngrams (f, ns=(1,2,3)):
  185. gens = tuple (Ngram.generator (n) for n in ns)
  186. allstats = Stats ()
  187. allgrams = {}
  188. for key, lines in DiffHelpers.separate_test_cases (f):
  189. test = Test (lines)
  190. allstats.add (test)
  191. for gen in gens:
  192. for ngram in gen (test.unicodes):
  193. if ngram not in allgrams:
  194. allgrams[ngram] = Stats ()
  195. allgrams[ngram].add (test)
  196. importantgrams = {}
  197. for ngram, stats in allgrams.iteritems ():
  198. if stats.failed.count >= 30: # for statistical reasons
  199. importantgrams[ngram] = stats
  200. allgrams = importantgrams
  201. del importantgrams
  202. for ngram, stats in allgrams.iteritems ():
  203. print "zscore: %9f failed: %6d passed: %6d ngram: <%s>" % (stats.zscore (allstats), stats.failed.count, stats.passed.count, ','.join ("U+%04X" % u for u in ngram))
  204. class Test:
  205. def __init__ (self, lines):
  206. self.freq = 1
  207. self.passed = True
  208. self.identifier = None
  209. self.text = None
  210. self.unicodes = None
  211. self.glyphs = None
  212. for l in lines:
  213. symbol = l[0]
  214. if symbol != ' ':
  215. self.passed = False
  216. i = 1
  217. if ':' in l:
  218. i = l.index (':')
  219. if not self.identifier:
  220. self.identifier = l[1:i]
  221. i = i + 2 # Skip colon and space
  222. j = -1
  223. if l[j] == '\n':
  224. j -= 1
  225. brackets = l[i] + l[j]
  226. l = l[i+1:-2]
  227. if brackets == '()':
  228. self.text = l
  229. elif brackets == '<>':
  230. self.unicodes = Unicode.parse (l)
  231. elif brackets == '[]':
  232. # XXX we don't handle failed tests here
  233. self.glyphs = l
  234. class DiffHelpers:
  235. @staticmethod
  236. def separate_test_cases (f):
  237. '''Reads lines from f, and if the lines have identifiers, ie.
  238. have a colon character, groups them by identifier,
  239. yielding lists of all lines with the same identifier.'''
  240. def identifier (l):
  241. if ':' in l[1:]:
  242. return l[1:l.index (':')]
  243. return l
  244. return groupby (f, key=identifier)
  245. @staticmethod
  246. def test_passed (lines):
  247. lines = list (lines)
  248. # XXX This is a hack, but does the job for now.
  249. if any (l.find("space+0|space+0") >= 0 for l in lines if l[0] == '+'): return True
  250. if any (l.find("uni25CC") >= 0 for l in lines if l[0] == '+'): return True
  251. if any (l.find("dottedcircle") >= 0 for l in lines if l[0] == '+'): return True
  252. if any (l.find("glyph0") >= 0 for l in lines if l[0] == '+'): return True
  253. if any (l.find("gid0") >= 0 for l in lines if l[0] == '+'): return True
  254. if any (l.find("notdef") >= 0 for l in lines if l[0] == '+'): return True
  255. return all (l[0] == ' ' for l in lines)
  256. class FilterHelpers:
  257. @staticmethod
  258. def filter_printer_function (filter_callback):
  259. def printer (f):
  260. for line in filter_callback (f):
  261. print line
  262. return printer
  263. @staticmethod
  264. def filter_printer_function_no_newline (filter_callback):
  265. def printer (f):
  266. for line in filter_callback (f):
  267. sys.stdout.writelines ([line])
  268. return printer
  269. class Ngram:
  270. @staticmethod
  271. def generator (n):
  272. def gen (f):
  273. l = []
  274. for x in f:
  275. l.append (x)
  276. if len (l) == n:
  277. yield tuple (l)
  278. l[:1] = []
  279. gen.n = n
  280. return gen
  281. class UtilMains:
  282. @staticmethod
  283. def process_multiple_files (callback, mnemonic = "FILE"):
  284. if "--help" in sys.argv:
  285. print "Usage: %s %s..." % (sys.argv[0], mnemonic)
  286. sys.exit (1)
  287. try:
  288. files = sys.argv[1:] if len (sys.argv) > 1 else ['-']
  289. for s in files:
  290. callback (FileHelpers.open_file_or_stdin (s))
  291. except IOError as e:
  292. if e.errno != errno.EPIPE:
  293. print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror)
  294. sys.exit (1)
  295. @staticmethod
  296. def process_multiple_args (callback, mnemonic):
  297. if len (sys.argv) == 1 or "--help" in sys.argv:
  298. print "Usage: %s %s..." % (sys.argv[0], mnemonic)
  299. sys.exit (1)
  300. try:
  301. for s in sys.argv[1:]:
  302. callback (s)
  303. except IOError as e:
  304. if e.errno != errno.EPIPE:
  305. print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror)
  306. sys.exit (1)
  307. @staticmethod
  308. def filter_multiple_strings_or_stdin (callback, mnemonic, \
  309. separator = " ", \
  310. concat_separator = False):
  311. if "--help" in sys.argv:
  312. print "Usage:\n %s %s...\nor:\n %s\n\nWhen called with no arguments, input is read from standard input." \
  313. % (sys.argv[0], mnemonic, sys.argv[0])
  314. sys.exit (1)
  315. try:
  316. if len (sys.argv) == 1:
  317. while (1):
  318. line = sys.stdin.readline ()
  319. if not len (line):
  320. break
  321. if line[-1] == '\n':
  322. line = line[:-1]
  323. print callback (line)
  324. else:
  325. args = sys.argv[1:]
  326. if concat_separator != False:
  327. args = [concat_separator.join (args)]
  328. print separator.join (callback (x) for x in (args))
  329. except IOError as e:
  330. if e.errno != errno.EPIPE:
  331. print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror)
  332. sys.exit (1)
  333. class Unicode:
  334. @staticmethod
  335. def decode (s):
  336. return u','.join ("U+%04X" % ord (u) for u in unicode (s, 'utf-8')).encode ('utf-8')
  337. @staticmethod
  338. def parse (s):
  339. s = re.sub (r"0[xX]", " ", s)
  340. s = re.sub (r"[<+>,;&#\\xXuU\n ]", " ", s)
  341. return [int (x, 16) for x in s.split (' ') if len (x)]
  342. @staticmethod
  343. def encode (s):
  344. return u''.join (unichr (x) for x in Unicode.parse (s)).encode ('utf-8')
  345. shorthands = {
  346. "ZERO WIDTH NON-JOINER": "ZWNJ",
  347. "ZERO WIDTH JOINER": "ZWJ",
  348. "NARROW NO-BREAK SPACE": "NNBSP",
  349. "COMBINING GRAPHEME JOINER": "CGJ",
  350. "LEFT-TO-RIGHT MARK": "LRM",
  351. "RIGHT-TO-LEFT MARK": "RLM",
  352. "LEFT-TO-RIGHT EMBEDDING": "LRE",
  353. "RIGHT-TO-LEFT EMBEDDING": "RLE",
  354. "POP DIRECTIONAL FORMATTING": "PDF",
  355. "LEFT-TO-RIGHT OVERRIDE": "LRO",
  356. "RIGHT-TO-LEFT OVERRIDE": "RLO",
  357. }
  358. @staticmethod
  359. def pretty_name (u):
  360. try:
  361. s = unicodedata.name (u)
  362. except ValueError:
  363. return "XXX"
  364. s = re.sub (".* LETTER ", "", s)
  365. s = re.sub (".* VOWEL SIGN (.*)", r"\1-MATRA", s)
  366. s = re.sub (".* SIGN ", "", s)
  367. s = re.sub (".* COMBINING ", "", s)
  368. if re.match (".* VIRAMA", s):
  369. s = "HALANT"
  370. if s in Unicode.shorthands:
  371. s = Unicode.shorthands[s]
  372. return s
  373. @staticmethod
  374. def pretty_names (s):
  375. s = re.sub (r"[<+>\\uU]", " ", s)
  376. s = re.sub (r"0[xX]", " ", s)
  377. s = [unichr (int (x, 16)) for x in re.split ('[, \n]', s) if len (x)]
  378. return u' + '.join (Unicode.pretty_name (x) for x in s).encode ('utf-8')
  379. class FileHelpers:
  380. @staticmethod
  381. def open_file_or_stdin (f):
  382. if f == '-':
  383. return sys.stdin
  384. return file (f)
  385. class Manifest:
  386. @staticmethod
  387. def read (s, strict = True):
  388. if not os.path.exists (s):
  389. if strict:
  390. print >> sys.stderr, "%s: %s does not exist" % (sys.argv[0], s)
  391. sys.exit (1)
  392. return
  393. s = os.path.normpath (s)
  394. if os.path.isdir (s):
  395. try:
  396. m = file (os.path.join (s, "MANIFEST"))
  397. items = [x.strip () for x in m.readlines ()]
  398. for f in items:
  399. for p in Manifest.read (os.path.join (s, f)):
  400. yield p
  401. except IOError:
  402. if strict:
  403. print >> sys.stderr, "%s: %s does not exist" % (sys.argv[0], os.path.join (s, "MANIFEST"))
  404. sys.exit (1)
  405. return
  406. else:
  407. yield s
  408. @staticmethod
  409. def update_recursive (s):
  410. for dirpath, dirnames, filenames in os.walk (s, followlinks=True):
  411. for f in ["MANIFEST", "README", "LICENSE", "COPYING", "AUTHORS", "SOURCES", "ChangeLog"]:
  412. if f in dirnames:
  413. dirnames.remove (f)
  414. if f in filenames:
  415. filenames.remove (f)
  416. dirnames.sort ()
  417. filenames.sort ()
  418. ms = os.path.join (dirpath, "MANIFEST")
  419. print " GEN %s" % ms
  420. m = open (ms, "w")
  421. for f in filenames:
  422. print >> m, f
  423. for f in dirnames:
  424. print >> m, f
  425. for f in dirnames:
  426. Manifest.update_recursive (os.path.join (dirpath, f))
  427. if __name__ == '__main__':
  428. pass