<commit_before>import tempfile, os

def edit(data):
	fd, tmp = tempfile.mkstemp(prefix = '0publish-')
	try:
		stream = os.fdopen(fd, 'w')
		stream.write(data)
		stream.close()
		editor = os.environ.get('EDITOR', 'vi')
		if os.spawnlp(os.P_WAIT, editor, editor, tmp):
			raise Exception('Editing with $EDITOR ("%s") failed')
		new_data = file(tmp).read()
	finally:
		os.unlink(tmp)
	if new_data == data:
		raise Exception('Data unchanged after edit. Aborting.')
	return new_data<commit_msg>If EDITOR isn't set, try various fallbacks.<commit_after>import tempfile, os
from logging import info, debug

path_dirs = os.environ.get('PATH', '/bin:/usr/bin').split(':')

def available_in_path(command):
	info("Checking for %s in $PATH", command)
	for x in path_dirs:
		debug("Checking for %s/%s", x, command)
		if os.path.isfile(os.path.join(x, command)):
			return True
	return False

def edit(data):
	fd, tmp = tempfile.mkstemp(prefix = '0publish-')
	try:
		stream = os.fdopen(fd, 'w')
		stream.write(data)
		stream.close()
		editor = os.environ.get('EDITOR', None)
		if editor is None:
			info("$EDITOR not set. Trying fallbacks...")
			for editor in ['sensible-editor', 'nano', 'vi']: # ,'ed'] ;-)
				if available_in_path(editor):
					break
			else:
				raise Exception("No editor found. Try setting $EDITOR first.")
		info("Editing tmp file with '%s %s'..." % (editor, tmp))
		if os.spawnlp(os.P_WAIT, editor, editor, tmp):
			raise Exception('Editing with $EDITOR ("%s") failed' % editor)
		new_data = file(tmp).read()
	finally:
		os.unlink(tmp)
	return new_data
{'fraction_non_alphanumeric': 0.16261203585147246, 'fraction_numerical': 0.0012804097311139564, 'mean_word_length': 4.192691029900332, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7157469', 'n_tokens_mistral': 615, 'n_tokens_neox': 570, 'n_words': 153}