Fix reading filenames from STDIN

This commit is contained in:
Jylpah committed 2018-10-04 11:24:24 +08:00
1 parent 02e2efc718
commit 5a9f7587b9
4 files changed
+395 -445

No files matched your search

+28 -58
View File
@@ -3,6 +3,7 @@
# Extract tankopedia data in WG API JSON format from Blitz app files (Android APK unzipped)
import sys, argparse, json, os, inspect, aiohttp, asyncio, aiofiles, re, logging, time, xmltodict, collections
import blitzutils as bu
from blitzutils import WG
logging.getLogger("asyncio").setLevel(logging.DEBUG)
@@ -11,31 +12,25 @@ BLITZAPP_STRINGS='/assets/Data/Strings/en.yaml'
BLITZAPP_VEHICLES_DIR='/assets/Data/XML/item_defs/vehicles/'
BLITZAPP_VEHICLE_FILE='/list.xml'
VERBOSE = False
DEBUG = False
wg = WG()
NATION_ID = {}
for i in range(len(wg.nations)):
NATION_ID[wg.nations[i]] = i
wg = None
## main() -------------------------------------------------------------
async def main(argv):
global VERBOSE, DEBUG
global wg
parser = argparse.ArgumentParser(description='Extract Tankopedia data from Blitz game files')
parser.add_argument('blitzAppBase', type=str, metavar="BLITZAPP_FOLDER", default=".", help='Base dir of the Blitz App files')
parser.add_argument('file', type=str, metavar="FILE", help='Write Tankopedia to file')
parser.add_argument('-fm', '--file-maps', dest='file_maps', type=str, default=None, help='File to write map names')
parser.add_argument('tanks', type=str, default='tanks.json',metavar="TANKS_FILE", help='File to write Tankopedia')
parser.add_argument('maps', type=str, default='maps.json', metavar='MAPS_FILE', help='File to write map names')
parser.add_argument('-d', '--debug', action='store_true', default=False, help='Debug mode')
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Verbose mode')
args = parser.parse_args()
VERBOSE = args.verbose
DEBUG = args.debug
if DEBUG: VERBOSE = True
bu.setVerbose(args.verbose)
bu.setDebug(args.debug)
wg = WG()
tasks = []
for nation in wg.nations:
tasks.append(asyncio.create_task(extractTanks(args.blitzAppBase, nation)))
@@ -46,16 +41,15 @@ async def main(argv):
tank_strs, map_strs = await readUserStrs(args.blitzAppBase)
async with aiofiles.open(args.file, 'w', encoding="utf8") as outfile:
tankopedia = {}
async with aiofiles.open(args.tanks, 'w', encoding="utf8") as outfile:
tankopedia = collections.OrderedDict()
tankopedia['status'] = 'ok'
tankopedia['meta'] = { "count": len(tanklist) }
tankopedia['data'], tankopedia['userStr'] = await convertTankNames(tanklist, tank_strs)
#debug(tankopedia)
await outfile.write(json.dumps(tankopedia, ensure_ascii=False, indent=4, sort_keys=False))
if args.file_maps != None:
async with aiofiles.open(args.file_maps, 'w', encoding="utf8") as outfile:
if args.maps != None:
async with aiofiles.open(args.maps, 'w', encoding="utf8") as outfile:
await outfile.write(json.dumps(map_strs, ensure_ascii=False, indent=4, sort_keys=True))
return None
@@ -67,7 +61,7 @@ async def extractTanks(blitzAppBase : str, nation: str):
if not os.path.isfile(list_xml_file):
print('ERROR: cannot open ' + list_xml_file)
return None
debug('Opening file: ' + list_xml_file + ' (Nation: ' + nation + ')')
bu.debug('Opening file: ' + list_xml_file + ' (Nation: ' + nation + ')')
async with aiofiles.open(list_xml_file, 'r') as f:
try:
tankList = xmltodict.parse(await f.read())
@@ -83,7 +77,7 @@ async def extractTanks(blitzAppBase : str, nation: str):
tank['type'] = await getTankType(tank_xml['tags'])
tanks.append(tank)
except Exception as err:
error(err)
bu.error(err)
sys.exit(2)
return tanks
@@ -92,7 +86,7 @@ async def readUserStrs(blitzAppBase : str) -> dict:
tank_strs = {}
map_strs = {}
filename = blitzAppBase + BLITZAPP_STRINGS
debug('Opening file: ' + filename + ' for reading UserStrings')
bu.debug('Opening file: ' + filename + ' for reading UserStrings')
try:
async with aiofiles.open(filename, 'r', encoding="utf8") as f:
p_tank = re.compile('^"(#\\w+?_vehicles:.+?)": "(.+)"$')
@@ -108,7 +102,7 @@ async def readUserStrs(blitzAppBase : str) -> dict:
map_strs[m.group(1)] = m.group(2)
except Exception as err:
error(err)
bu.error(err)
sys.exit(1)
return tank_strs, map_strs
@@ -117,27 +111,31 @@ async def convertTankNames(tanklist : list, tank_strs: dict) -> dict:
"""Convert tank names for Tankopedia"""
tankopedia = {}
userStrs = {}
tank_ids = []
try:
for tank in tanklist:
tank['name'] = tank_strs[tank['userStr']]
userStrs[tank['userStr'].split(':')[1]] = tank['name']
tank.pop('userStr', None)
tankopedia[str(tank['tank_id'])] = tank
tank_ids.append(tank['tank_id']) # for sorting...
tank_ids.sort()
# sorting
tankopedia_sorted = collections.OrderedDict()
for tank_id in tank_ids:
for tank_id in sorted(tankopedia.keys(), key=int):
tankopedia_sorted[str(tank_id)] = tankopedia[str(tank_id)]
userStrs_sorted = collections.OrderedDict()
for userStr in sorted(userStrs.keys()):
userStrs_sorted[userStr] = userStrs[userStr]
except Exception as err:
error(err)
bu.error(err)
sys.exit(1)
return tankopedia_sorted, userStrs
return tankopedia_sorted, userStrs_sorted
async def getTankID(nation: str, tankID : int) -> int:
return (tankID << 8) + (NATION_ID[nation] << 4) + 1
return (tankID << 8) + (wg.nation_id[nation] << 4) + 1
async def getTankType(tagstr : str):
tags = tagstr.split(' ')
@@ -146,34 +144,6 @@ async def getTankType(tagstr : str):
return t_type
return None
def verbose(msg = ""):
"""Print a message"""
if VERBOSE:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
caller = calframe[1][3]
print(caller + '(): ' + msg)
return None
def debug(msg = ""):
"""print a conditional debug message"""
if DEBUG:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
caller = calframe[1][3]
print('DEBUG: ' + caller + '(): ' + msg)
return None
def error(msg = ""):
"""Print an error message"""
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
caller = calframe[1][3]
print('ERROR: ' + caller + '(): ' + msg)
return None
### main()
if __name__ == "__main__":