<commit_before>from urllib import urlopen
from json import loads, dumps
from xml.dom.minidom import parseString, Document
from xml.parsers.expat import ExpatError
from os.path import exists, splitext

from RoleSymmetricGame import *


def readGame(source):
	if isinstance(source, str) and exists(source):
		#source is a filename
		f = open(source)
		data = f.read()
		f.close()
	elif isinstance(source, str) and source.startswith("http://"):
		#source is a url
		u = urlopen(source)
		data = u.read()
		u.close()
	elif hasattr(source, 'read'):
		#source is file-like
		data = source.read()
		source.close()
	else:
		#assume source is already xml or json data
		data = source
	try:
		return readJSON(loads(data))
	except ValueError:
		try:
			return readXML(parseString(data))
		except ExpatError:
			raise IOError("invalid game source")


def readJSON(data):
	if "strategy_array" in data["roles"][0]:
		return readJSON_old(data)
	elif "strategies" in data["roles"][0]:
		return readJSON_v2(data)
	else:
		raise IOError("invalid JSON data: " + str(data))


def readJSON_v2(json_data):
	players = {r["name"] : int(r["count"]) for r in json_data["roles"]}
	strategies = {r["name"] : r["strategies"] for r in json_data["roles"]}<commit_msg>a little more information about "invalid game source" errors<commit_after>from urllib import urlopen
from json import loads, dumps
from xml.dom.minidom import parseString, Document
from xml.parsers.expat import ExpatError
from os.path import exists, splitext

from RoleSymmetricGame import *


def readGame(source):
	if isinstance(source, str) and exists(source):
		#source is a filename
		f = open(source)
		data = f.read()
		f.close()
	elif isinstance(source, str) and source.startswith("http://"):
		#source is a url
		u = urlopen(source)
		data = u.read()
		u.close()
	elif hasattr(source, 'read'):
		#source is file-like
		data = source.read()
		source.close()
	else:
		#assume source is already xml or json data
		data = source
	try:
		return readJSON(loads(data))
	except ValueError:
		try:
			return readXML(parseString(data))
		except ExpatError:
			raise IOError("invalid game source: " + str(source)[:100])


def readJSON(data):
	if "strategy_array" in data["roles"][0]:
		return readJSON_old(data)
	elif "strategies" in data["roles"][0]:
		return readJSON_v2(data)
	else:
		raise IOError("invalid JSON data: " + str(data))


def readJSON_v2(json_data):
	players = {r["name"] : int(r["count"]) for r in json_data["roles"]}
	strategies = {r["name"] : r["strategies"] for r in json_data["roles"]}
{'fraction_non_alphanumeric': 0.12947658402203857, 'fraction_numerical': 0.004329004329004329, 'mean_word_length': 4.751131221719457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '151979', 'n_tokens_mistral': 932, 'n_tokens_neox': 882, 'n_words': 260}