File: extractGuideJsonStrings.py

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (58 lines) | stat: -rwxr-xr-x 2,056 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# encoding: utf-8

import json, sys, os
from funcs.wzjsonlocalizedstring import wz_localized_string_get_base_string

def print_string(s, out_file):
	out_file.write('_({})\n'.format(json.dumps(s, ensure_ascii=False)))

def parse_wz_guide_json_string(value, filename, out_file, jsonPath):
	try:
		base_en_str = wz_localized_string_get_base_string(value)
	except ValueError as e:
		raise ValueError('Invalid localized string [{0}]: {1}'.format(jsonPath, e.message))

	if base_en_str:
		print_string(base_en_str, out_file)

def parse_wz_guide_json_contents_array(contents_arr, filename, out_file, jsonPath='$.contents'):
	if not isinstance(contents_arr, list):
		raise ValueError('Invalid contents value type - expected list')
	for idx, v in enumerate(contents_arr):
		parse_wz_guide_json_string(v, filename, out_file, jsonPath + '[' + str(idx) + ']')

def parse_wz_guide_json(input_path, output_folder):

	obj = json.load(open(input_path, 'r'))

	if not isinstance(obj, dict):
		raise ValueError('WZ Guide JSON root is not an object')

	if not 'id' in obj:
		raise ValueError('WZ Guide JSON is missing required \"id\"')

	if not 'title' in obj:
		raise ValueError('WZ Guide JSON is missing required \"title\"')

	sanitized_id = obj['id'].replace('::', '-').replace(':', '-')
	output_filename = os.path.join(output_folder, sanitized_id + '.txt')

	with open(output_filename, 'w', encoding='utf-8') as out_file:
		if 'title' in obj:
			try:
				base_title_str = wz_localized_string_get_base_string(obj['title'])
			except ValueError as e:
				raise ValueError('Invalid title string [{0}]: {1}'.format('$.title', e.message))

			if base_title_str:
				out_file.write('// TRANSLATORS:\n')
				out_file.write('// The guide topic title - please maintain original capitalization\n')
				print_string(base_title_str, out_file)

		if 'contents' in obj:
			parse_wz_guide_json_contents_array(obj['contents'], input_path, out_file, '$.contents')

input_path = sys.argv[1]
output_folder = sys.argv[2]
parse_wz_guide_json(input_path, output_folder)