Workaround for Python 3.8 Asyncio issues with Win 10

This commit is contained in:
Jylpah committed 2020-07-01 17:42:13 +03:00
1 parent a706903b18
commit 7cfdd872c8
4 files changed
+77 -12

No files matched your search

+30 -3
View File
@@ -486,6 +486,14 @@ class PlayerHistogram():
return None return None
class ErrorCatchingArgumentParser(argparse.ArgumentParser):
def exit(self, status=0, message=None):
if status:
if message != None:
raise UserWarning(message)
else:
raise UserWarning()
## main() ------------------------------------------------------------- ## main() -------------------------------------------------------------
async def main(argv): async def main(argv):
@@ -517,6 +525,7 @@ async def main(argv):
DB_CERT = None DB_CERT = None
DB_CA = None DB_CA = None
try:
## Read config ## Read config
if os.path.isfile(FILE_CONFIG): if os.path.isfile(FILE_CONFIG):
config = configparser.ConfigParser() config = configparser.ConfigParser()
@@ -557,8 +566,8 @@ async def main(argv):
except configparser.NoSectionError as err: except configparser.NoSectionError as err:
bu.error(exception=err) bu.error(exception=err)
parser = ErrorCatchingArgumentParser(description='Analyze Blitz replay JSONs from WoTinspector.com')
parser = argparse.ArgumentParser(description='Analyze Blitz replay JSONs from WoTinspector.com')
parser.add_argument('--output', default='plain', choices=['json', 'plain', 'db'], help='Select output mode: JSON, plain text or database') parser.add_argument('--output', default='plain', choices=['json', 'plain', 'db'], help='Select output mode: JSON, plain text or database')
parser.add_argument('-id', dest='account_id', type=int, default=WG_ID, help='WG account_id to analyze') parser.add_argument('-id', dest='account_id', type=int, default=WG_ID, help='WG account_id to analyze')
parser.add_argument('-a', '--account', type=str, default=None, help='WG account nameto analyze. Format: ACCOUNT_NAME@SERVER') parser.add_argument('-a', '--account', type=str, default=None, help='WG account nameto analyze. Format: ACCOUNT_NAME@SERVER')
@@ -577,7 +586,11 @@ async def main(argv):
parser.add_argument('-s', '--silent', action='store_true', default=False, help='Silent mode') parser.add_argument('-s', '--silent', action='store_true', default=False, help='Silent mode')
parser.add_argument('files', metavar='FILE1 [FILE2 ...]', type=str, nargs='+', help='Files/dirs to read. Use \'-\' for STDIN, "db:" for database') parser.add_argument('files', metavar='FILE1 [FILE2 ...]', type=str, nargs='+', help='Files/dirs to read. Use \'-\' for STDIN, "db:" for database')
try:
args = parser.parse_args() args = parser.parse_args()
except Exception as err:
raise
bu.set_log_level(args.silent, args.verbose, args.debug) bu.set_log_level(args.silent, args.verbose, args.debug)
bu.set_progress_step(250) # Set the frequency of the progress dots. bu.set_progress_step(250) # Set the frequency of the progress dots.
@@ -654,10 +667,15 @@ async def main(argv):
bu.debug('Finished. Cleaning up..................') bu.debug('Finished. Cleaning up..................')
except Exception as err: except Exception as err:
bu.error(exception=err) bu.error(exception=err)
except UserWarning as err:
bu.verbose(str(err))
pass
except Exception as err:
bu.error(exception=err)
finally: finally:
## Need to close the aiohttp.session since Python destructors do not support async methods... ## Need to close the aiohttp.session since Python destructors do not support async methods...
await wg.close() if wg != None: await wg.close()
await wi.close() if wi != None: await wi.close()
return None return None
def process_player_dist(results: list, player_stats: dict, stat_id_map: dict): def process_player_dist(results: list, player_stats: dict, stat_id_map: dict):
@@ -1383,4 +1401,13 @@ def get_stat_id_player(stat_id_str: str) -> str:
### main() ### main()
if __name__ == "__main__": if __name__ == "__main__":
# To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
try:
from asyncio import WindowsSelectorEventLoopPolicy
except ImportError:
pass
else:
if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.run(main(sys.argv[1:]), debug=False) asyncio.run(main(sys.argv[1:]), debug=False)
+15 -2
View File
@@ -23,11 +23,16 @@ async def main(argv):
os.chdir(os.path.dirname(sys.argv[0])) os.chdir(os.path.dirname(sys.argv[0]))
## Read config ## Read config
BLITZAPP_FOLDER = '.'
try:
if os.path.isfile(FILE_CONFIG):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(FILE_CONFIG) config.read(FILE_CONFIG)
configOptions = config['EXTRACT_TANKOPEDIA'] configOptions = config['EXTRACT_TANKOPEDIA']
BLITZAPP_FOLDER = configOptions.get('blitz_app_dir', '.') BLITZAPP_FOLDER = configOptions.get('blitz_app_dir', BLITZAPP_FOLDER)
except:
pass
parser = argparse.ArgumentParser(description='Extract Tankopedia data from Blitz game files') parser = argparse.ArgumentParser(description='Extract Tankopedia data from Blitz game files')
parser.add_argument('blitz_app_base', type=str, nargs='?', metavar="BLITZAPP_FOLDER", default=BLITZAPP_FOLDER, help='Base dir of the Blitz App files') parser.add_argument('blitz_app_base', type=str, nargs='?', metavar="BLITZAPP_FOLDER", default=BLITZAPP_FOLDER, help='Base dir of the Blitz App files')
@@ -186,6 +191,14 @@ async def get_tank_type(tagstr : str):
### main() ### main()
if __name__ == "__main__": if __name__ == "__main__":
# To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
try:
from asyncio import WindowsSelectorEventLoopPolicy
except ImportError:
pass
else:
if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
#asyncio.run(main(sys.argv[1:]), debug=True) #asyncio.run(main(sys.argv[1:]), debug=True)
asyncio.run(main(sys.argv[1:])) asyncio.run(main(sys.argv[1:]))
+23 -7
View File
@@ -2,7 +2,15 @@
# Get Tankpedia from WoTinspector.com # Get Tankpedia from WoTinspector.com
import sys, argparse, json, os, inspect, aiohttp, asyncio, re, logging import sys
import argparse
import json
import os
import inspect
import aiohttp
import asyncio
import re
import logging
from pathlib import Path from pathlib import Path
import blitzutils as bu import blitzutils as bu
from blitzutils import WG from blitzutils import WG
@@ -13,15 +21,15 @@ logging.getLogger("asyncio").setLevel(logging.DEBUG)
VERBOSE = False VERBOSE = False
DEBUG = False DEBUG = False
tank_type = [ 'lightTank', 'mediumTank', 'heavyTank', 'AT-SPG' ] tank_type = ['lightTank', 'mediumTank', 'heavyTank', 'AT-SPG']
#@asyncio.coroutine # @asyncio.coroutine
async def get_tankopedia(filename: str): async def get_tankopedia(filename: str):
url="https://wotinspector.com/static/armorinspector/tank_db_blitz.js" url = "https://wotinspector.com/static/armorinspector/tank_db_blitz.js"
async with aiohttp.ClientSession() as client: async with aiohttp.ClientSession() as client:
async with client.get(url) as r: async with client.get(url) as r:
if r.status == 200: if r.status == 200:
WI_tank_db=await r.text() WI_tank_db = await r.text()
WI_tank_db = WI_tank_db.split("\n") WI_tank_db = WI_tank_db.split("\n")
else: else:
print('Error: Could not get valid HTTPS response. HTTP: ' + str(r.status) ) print('Error: Could not get valid HTTPS response. HTTP: ' + str(r.status) )
@@ -64,12 +72,20 @@ async def main(argv):
bu.set_log_level(args.silent, args.verbose, args.debug) bu.set_log_level(args.silent, args.verbose, args.debug)
wi = WoTinspector() wi = WoTinspector()
await wi.get_tankopedia(args.outfile) await wi.get_tankopedia(args.outfile)
await wi.close() await wi.close()
### main() # main()
if __name__ == "__main__": if __name__ == "__main__":
# To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
try:
from asyncio import WindowsSelectorEventLoopPolicy
except ImportError:
pass
else:
if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.run(main(sys.argv[1:]), debug=False) asyncio.run(main(sys.argv[1:]), debug=False)
+9
View File
@@ -241,4 +241,13 @@ def getTitle(replayfile: str, title: str, i : int) -> str:
### main() ------------------------------------------- ### main() -------------------------------------------
if __name__ == "__main__": if __name__ == "__main__":
# To avoid 'Event loop is closed' RuntimeError due to compatibility issue with aiohttp
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
try:
from asyncio import WindowsSelectorEventLoopPolicy
except ImportError:
pass
else:
if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy):
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
asyncio.run(main(sys.argv[1:]), debug=False) asyncio.run(main(sys.argv[1:]), debug=False)