forked from Lexxie9952/fcw.org-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpdata_gen.py
executable file
·83 lines (70 loc) · 3.2 KB
/
helpdata_gen.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: iso-8859-1 -*-
'''
Freeciv - Copyright (C) 2009-2014 - Andreas Røsdal [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
from os import path
import argparse
import sys
import configparser
import json
import re
parser = argparse.ArgumentParser(
description='Generate .js help data based on freeciv project')
parser.add_argument('-f', '--freeciv', required=True, help='path to (original) freeciv project')
parser.add_argument('-o', '--outdir', required=True, help='path to webapp output directory')
args = parser.parse_args()
webapp_dir = args.outdir
freeciv_dir = args.freeciv
def removeComments(string):
string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,string);
string = re.sub(re.compile("//.*?\n" ) ,"" ,string);
string = re.sub(re.compile("([a-zA-Z]);" ) ,"\g<1>," ,string);
string = re.sub(re.compile(";.*?\n" ) ,"" ,string);
return string
def config_read(file):
print(("Parsing " + file));
config = configparser.ConfigParser(strict=False)
with open (file, "r") as myfile:
config_text=myfile.read();
# These changes are required so that the Python config parser can read Freeciv spec files.
config_text = config_text.replace("*include", "#*include");
config_text = config_text.replace("\\\"", "'");
config_text = config_text.replace("\n\"", "\n \"");
config_text = config_text.replace("\n}", "\n }");
config_text = config_text.replace("%", "%%");
config_text = config_text.replace("\n1", "\n 1");
config_text = removeComments(config_text);
config_text = config_text.replace("\\\n", "");
config_text = config_text.replace("\n_(\"", "_(\"");
config_text = config_text.replace("_(\"", "");
config_text = config_text.replace("\"),", "<br><br>");
config_text = config_text.replace("\")", "");
config_text = config_text.replace("\\n", "<br>");
#print(config_text);
config.read_string(config_text);
#print((config.sections()));
return config;
input_name = path.join(freeciv_dir, "data", "helpdata.txt")
config = config_read(input_name)
thedict = {}
for section in config.sections():
thedict[section] = {}
for key, val in config.items(section):
# skip these hidden help sections, since they are not in use.
if (section in ["help_connecting", "help_languages", "help_governor",
"help_chatline", "help_about", "help_worklist_editor", "help_copying"]): continue;
thedict[section][key] = val
output_name = path.join(webapp_dir, 'javascript', 'freeciv-helpdata.js')
f = open(output_name, 'w')
f.write("var helpdata_order = " + json.dumps(config.sections(), indent=2, separators=(',', ': ')) + ";\n")
f.write("var helpdata = " + json.dumps(thedict, indent=2, separators=(',', ': ')) + ";\n")
print("Generated " + output_name)