Guest User

Untitled

a guest
Feb 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import argparse
  4. from re import match
  5.  
  6. parser = argparse.ArgumentParser(description='Synchronizes translated values from a .PO file with another one. By MikiSoft')
  7. parser.add_argument('source_file', type=argparse.FileType('r'))
  8. parser.add_argument('target_file', type=argparse.FileType('r'))
  9. args = parser.parse_args()
  10.  
  11. def extract_strings(lines):
  12. c = []
  13. o = []
  14. last = None
  15. def checkappend(c, o, last, m=False):
  16. if len(o) == 0 or not m and (last in o[-1] or last == 'msgctxt') or m and last[0] in o[-1]:
  17. if len(c) > 0:
  18. o.append({'#': c.copy()})
  19. c[:] = []
  20. else:
  21. o.append({})
  22. for l in lines:
  23. if l.strip() == '':
  24. continue
  25. r = match('^(?:(?P<key>msgid(?:_plural)?|msgstr|msgctxt)(?:\[(?P<i>\d)\])? )?"(?P<value>.*?)"$', l)
  26. if not r:
  27. if l[0] == '#':
  28. c.append(l[1:])
  29. continue
  30. if r.group('i') or not r.group('key') and isinstance(last, list):
  31. if not r.group('i'):
  32. o[-1][last[0]][last[1]].append(r.group('value'))
  33. elif r.group('key') in o[-1] and r.group('i') not in o[-1][last[0]]:
  34. last[1] = r.group('i')
  35. o[-1][last[0]][last[1]] = [r.group('value')]
  36. else:
  37. last = [r.group('key'), r.group('i')]
  38. checkappend(c, o, last, True)
  39. o[-1][last[0]] = {last[1]: [r.group('value')]}
  40. else:
  41. if not r.group('key'):
  42. o[-1][last].append(r.group('value'))
  43. else:
  44. last = r.group('key')
  45. checkappend(c, o, last)
  46. o[-1][last] = [r.group('value')]
  47. if len(c) > 0:
  48. o[-1]['#'] = c
  49. return o
  50.  
  51. files = [args.source_file.readlines(), args.target_file.readlines()]
  52. args.source_file.close()
  53. for i, f in enumerate(files):
  54. files[i] = extract_strings(f)
  55.  
  56. for s in files[0]:
  57. for s1 in files[1]:
  58. if s['msgid'] == s1['msgid'] and ('msgid_plural' not in s and 'msgid_plural' not in s1 or s['msgid_plural'] == s1['msgid_plural']):
  59. s1['msgstr'] = s['msgstr']
  60.  
  61. def output_strings(lst, file):
  62. def gen(s, f, i=None):
  63. out = f+('['+i+'] ' if i else ' ')
  64. for l in (s[f][i] if i else s[f]):
  65. out += '"'+l+'"\n'
  66. return out
  67. for s in lst:
  68. out = ''
  69. if '#' in s:
  70. for l in s['#']:
  71. out += '#'+l
  72. for t in ['msgctxt', 'msgid', 'msgid_plural']:
  73. if t in s:
  74. out += gen(s, t)
  75. if isinstance(s['msgstr'], list):
  76. out += gen(s, 'msgstr')
  77. else:
  78. for i in range(0, len(s['msgstr'].keys())):
  79. out += gen(s, 'msgstr', str(i))
  80. file.write(out+'\n')
  81.  
  82. args.target_file = open(args.target_file.name, 'w')
  83. output_strings(files[1], args.target_file)
  84. args.target_file.close()
Add Comment
Please, Sign In to add comment