2021-04-15 05:49:32 +00:00
|
|
|
# Copyright (c) 2020-2021 The reone project contributors
|
|
|
|
|
|
|
|
# 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 3 of the License, 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.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-04-22 07:44:50 +00:00
|
|
|
"""Script to count unique JSON values in extracted resources."""
|
2021-04-12 14:01:50 +00:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import json
|
|
|
|
import re
|
2021-04-13 00:43:43 +00:00
|
|
|
import sys
|
2021-04-12 14:01:50 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2021-05-31 02:31:22 +00:00
|
|
|
from reo_shared import *
|
2021-04-12 14:01:50 +00:00
|
|
|
|
2021-05-31 02:31:22 +00:00
|
|
|
extract_dir = r"D:\OpenKotOR\Extract\KotORx"
|
|
|
|
|
|
|
|
if not is_valid_extract_dir(extract_dir):
|
|
|
|
extract_dir = choose_directory("Choose an extraction directory")
|
|
|
|
if not is_valid_extract_dir(extract_dir):
|
|
|
|
exit(1)
|
2021-04-22 07:46:44 +00:00
|
|
|
|
|
|
|
|
2021-04-12 14:01:50 +00:00
|
|
|
def get_unique_json_values(extract_dir, path_pattern, extract_values):
|
|
|
|
|
|
|
|
def count_value(value, values):
|
|
|
|
if value in values:
|
|
|
|
values[value] += 1
|
|
|
|
else:
|
|
|
|
values[value] = 1
|
|
|
|
|
|
|
|
values = dict()
|
|
|
|
|
|
|
|
# Recursively process files in extraction directory, whose path matches a pattern
|
|
|
|
for f in glob.glob("{}/**".format(extract_dir), recursive=True):
|
|
|
|
if re.search(path_pattern, f):
|
2021-04-14 03:24:17 +00:00
|
|
|
with open(f, "r") as fp:
|
2021-04-12 14:01:50 +00:00
|
|
|
obj = json.load(fp)
|
|
|
|
for value in extract_values(obj):
|
|
|
|
count_value(value, values)
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
2021-04-22 07:44:50 +00:00
|
|
|
def count_unique_json_values(extract_dir, path_pattern, extract_values):
|
2021-04-12 14:01:50 +00:00
|
|
|
values = get_unique_json_values(extract_dir, path_pattern, extract_values)
|
|
|
|
if values:
|
|
|
|
print("{} unique values: {}".format(len(values), values))
|
|
|
|
|
|
|
|
|
|
|
|
def extract_values_simple(key, obj):
|
2021-04-22 05:39:07 +00:00
|
|
|
return [obj[key]] if key in obj else []
|
2021-04-12 14:01:50 +00:00
|
|
|
|
|
|
|
|
2021-04-13 00:43:43 +00:00
|
|
|
if len(sys.argv) > 2:
|
|
|
|
# If at least two command line arguments were passed, interpret them as
|
|
|
|
# filepath pattern and JSON key. In this mode we count unique field values
|
|
|
|
# in JSON files.
|
|
|
|
path_pattern = sys.argv[1]
|
|
|
|
json_key = sys.argv[2]
|
2021-04-22 07:44:50 +00:00
|
|
|
count_unique_json_values(extract_dir, path_pattern, partial(extract_values_simple, json_key))
|
2021-04-13 00:43:43 +00:00
|
|
|
else:
|
2021-04-22 07:44:50 +00:00
|
|
|
print("Usage: python count_json_values.py PATH_PATTERN JSON_KEY")
|