-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwords2lang.py
124 lines (105 loc) · 5.1 KB
/
words2lang.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#words2lang.py
'''
The MIT License (MIT)
Copyright (c) 2020 Joseph Bettendorff a.k.a. "Commoble"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
'''
Util for converting nameword data jsons to lang json entries.
Running this script via
`python words2lang.py --path {datapath} --output {outputpath}`
causes the script to look for an output folder at {datapath} relative
to the directory the script is run from, read all nameword jsons,
and generate an appropriate json file at the given relative {outputpath}.
By default, {datapath} is `src/main/resources/data/`,
and {outputpath} is
`src/main/resources/assets/looot_generated_words/lang/en_us.json`
so if the script is running from the root of a mod project folder,
and if an en_us.json is desired,
then the --path and --output args can usually be omitted.
'''
import argparse
import string
import os
import os.path
import json
from pathlib import Path
'''
Returns a dict of headered paths, e.g. the absolute path
**/data/modid/looot/namewords/folder/subfolder/name.json becomes
"modid.looot.namewords.folder.subfolder.name" : path
Paths without the looot/namewords folder are filtered out and not included
'''
def getHeaderedFiles(paths: list) -> dict:
out = dict()
for path in paths:
parts = path.parts
pathSize = len(parts)
for i in range(pathSize):
if parts[i] == "data" and i+3 < pathSize and parts[i+2] == "looot" and parts[i+3] == "namewords":
header = makeHeader(parts[i+1:])
out[header] = path
break
return out
noCaps = set(["of", "the"])
# convert e.g. "of_the_thing" to "of the Thing"
def translate(words: str) -> str:
return " ".join([word if word in noCaps else word.capitalize() for word in words.split("_")])
# converts ["a", "b", "c", "thing.json"] to "a.b.c.thing"
def makeHeader(parts: list) -> str:
unsuffixedJsonName = parts[-1][0:-5] # converts "thing.json" to "thing"
subComponents = [x for x in parts[0:-1]] + [unsuffixedJsonName]
return ".".join(subComponents)
def getInputJson(path) -> dict:
with open(path, "r") as inFile:
inputJson = json.load(inFile) # loads json object as a python Dict
return inputJson
parser = argparse.ArgumentParser(description="Generate lang json entries from looot nameword data files")
parser.add_argument("--path", type=str, dest="path",
default="src/main/resources/data/",
help="Relative path fom the current folder to the data folder to read")
parser.add_argument("--output", type=str, dest="output",
default="src/main/resources/assets/looot_generated_words/lang/en_us.json",
help="Relative path of the output file (will be replaced if present)")
args = parser.parse_args()
cwd = os.getcwd()
dataFolder = os.path.join(cwd, args.path)
outputPath = os.path.join(cwd, args.output)
# we need a list of all of the jsons in the data/[modid]/looot/namewords/ folder, for all modids
# we also need to remember which modid each of these jsons are under
# we want to be able to convert e.g. data/forge/looot/namewords/suffixes/rods/fishing.json
# to some identifier that includes at minimum the modid ("forge" in this example) + everything after namewords
# "forge.looot.namewords.suffixes.rods.fishing" would be the simplest format here
dataDomains = [folder.path for folder in os.scandir(dataFolder) if folder.is_dir()]
# nameWordFolders = [os.path.join(domainFolder, nameWordFolder) for domainFolder in dataDomains]
# nameWordFolders = [f for f in nameWordFolders if os.path.exists(f)]
# now we have a list of all of the absolute paths of the nameword folders (under any modid) in the workspace
# the next thing we want to do is to get *all* of the json files in these folders, along with the relative paths to them
pathsToJsons = list(Path(".").rglob("*.json"))
headeredFiles = getHeaderedFiles(pathsToJsons)
entries = dict()
for header in headeredFiles:
path = headeredFiles[header]
inputJson = getInputJson(path) # gets json object as a python Dict
values = inputJson["values"] # a list
for value in values:
key = '.'.join([header,value])
entries[key] = translate(value)
os.makedirs(os.path.dirname(outputPath), exist_ok=True)
with open(outputPath, "w+") as outFile:
json.dump(entries, outFile, indent='\t')
print("Generated " + str(len(entries)) + " entries in " + outputPath)