<commit_before>import os, os.path
import requests
import json

import pymoku.version

cache_dir = os.path.expanduser('~/.moku/')
compat_file = os.path.join(cache_dir, 'compat.json')

try:
	os.mkdir(cache_dir)
except OSError:
	pass # Already exists

def pymoku_updates(server, username, password):
	params = { 'build_id__gte' : pymoku.version.build }
	r = requests.get(server + '/versions/update/pymoku/', params=params, auth=(username, password))
	return r.json()

def compatable_configurations(server, username, password):
	params = { 'build_id' : pymoku.version.build }

	try:
		r = requests.get(server + '/versions/compat/pymoku/', params=params, auth=(username, password))
		ret = r.json()
	except:
		if os.path.exists(compat_file):
			ret = json.loads(open(compat_file).read())
		else:
			raise Exception("Can't download compatibility information and none has been cached locally.")

	open(compat_file, 'w').write(json.dumps(ret))

	return ret

def firmware_is_compatible(build_id, server=None, username=None, password=None):
	if build_id == 65535:
		return None # Development build, unable to determine

	try:
		configs = compatable_configurations(server, username, password)
		compat_fw = set([ c['firmware']['build_id'] for c in configs.values() ])
	except:
		return None
	return build_id in compat_fw<commit_msg>HG-1995: Remove strict requirement for requests<commit_after>import os, os.path
import json

try:
	import requests
	use_requests = True
except ImportError:
	use_requests = False

import pymoku.version

cache_dir = os.path.expanduser('~/.moku/')
compat_file = os.path.join(cache_dir, 'compat.json')

try:
	os.mkdir(cache_dir)
except OSError:
	pass # Already exists

def pymoku_updates(server, username, password):
	params = { 'build_id__gte' : pymoku.version.build }
	r = requests.get(server + '/versions/update/pymoku/', params=params, auth=(username, password))
	return r.json()

def compatable_configurations(server, username, password):
	params = { 'build_id' : pymoku.version.build }

	try:
		r = requests.get(server + '/versions/compat/pymoku/', params=params, auth=(username, password))
		ret = r.json()
	except:
		if os.path.exists(compat_file):
			ret = json.loads(open(compat_file).read())
		else:
			raise Exception("Can't download compatibility information and none has been cached locally.")

	open(compat_file, 'w').write(json.dumps(ret))

	return ret

def firmware_is_compatible(build_id, server=None, username=None, password=None):
	if build_id == 65535:
		return None # Development build, unable to determine

	if not use_requests: # Could still look in the cache
		return None

	try:
		configs = compatable_configurations(server, username, password)
		compat_fw = set([ c['firmware']['build_id'] for c in configs.values() ])
	except:
		return None
	return build_id in compat_fw
{'fraction_non_alphanumeric': 0.125044404973357, 'fraction_numerical': 0.00497335701598579, 'mean_word_length': 5.202643171806168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7423982', 'n_tokens_mistral': 978, 'n_tokens_neox': 921, 'n_words': 236}