<commit_before>import sys
import json
import time
import inspect
import os
import traceback

JEDI_IMPORT_FAILED=False

try:
	import jedi
except ImportError:
	# Use the bundled jedi
	my_file = inspect.stack()[0][1]
	my_path = os.path.dirname(os.path.abspath(my_file))
	sys.path.append(my_path + '/python_packages')
	try:
		import jedi
	except ImportError:
		JEDI_IMPORT_FAILED=True

class JediCmdline(object):
	def __init__(self, istream, ostream):
		self.istream = istream
		self.ostream = ostream

	def _get_params(self, completion):
		try:
			param_defs = completion.params
		except AttributeError:
			return []
		except:
			# TODO Propagate!
			return []

		params = []
		for param_def in param_defs:
			params.append({
				'name': param_def.name,
				'description': param_def.description
			})

		return params

	def _process_command(self, data):
		if data['cmd'] == 'add_python_path':
			sys.path.append(data['path'])

	def _process_line(self, line):
		data =  json.loads(line)

		if 'cmd' in data:
			self._process_command(data)
			return

		raw=False
		try:
			# TODO path? source_path?
			script = jedi.api.Script(data['source'], data['line'] + 1, data['column'])

			retData = []

			completions = script.completions()
			for completion in completions:
				params = self._get_params(completion)

				retData.append({
					'name': completion.name,
					'complete': completion.complete,
					'description': completion.description,
					'type': completion.type,
					'params': params,
					'docstring': completion.docstring()
				})
		except:
			raw=True
			retData = {
				'reqId': 'debug',
				'debug': True,
				'level': 'error',
				'stacktrace': traceback.format_exc()
			}

		self._write_response(retData, data, raw=raw)

	def _write_response(self, retData, data, raw=False):
		if not raw:
			reqId = data['reqId']
			ret = {'reqId': reqId,
					'prefix': data['prefix'],
					'suggestions': retData}
		else:
			ret = retData
			
		self.ostream.write(json.dumps(ret) + "\n")<commit_msg>Make it work with jedi 0.7<commit_after>import sys
import json
import time
import inspect
import os
import traceback

JEDI_IMPORT_FAILED=False

try:
	import jedi
except ImportError:
	# Use the bundled jedi
	my_file = inspect.stack()[0][1]
	my_path = os.path.dirname(os.path.abspath(my_file))
	sys.path.append(my_path + '/python_packages')
	try:
		import jedi
	except ImportError:
		JEDI_IMPORT_FAILED=True

class JediCmdline(object):
	def __init__(self, istream, ostream):
		self.istream = istream
		self.ostream = ostream

	def _get_params(self, completion):
		try:
			param_defs = completion.params
		except AttributeError:
			return []
		except:
			# TODO Propagate!
			return []

		params = []
		for param_def in param_defs:
			params.append({
				'name': param_def.name,
				'description': param_def.description
			})

		return params

	def _process_command(self, data):
		if data['cmd'] == 'add_python_path':
			sys.path.append(data['path'])

	def _process_line(self, line):
		data =  json.loads(line)

		if 'cmd' in data:
			self._process_command(data)
			return

		raw=False
		try:
			# TODO path? source_path?
			script = jedi.api.Script(data['source'], data['line'] + 1, data['column'])

			retData = []

			completions = script.completions()
			for completion in completions:
				params = self._get_params(completion)

				suggestionData = {
					'name': completion.name,
					'complete': completion.complete,
					'description': completion.description,
					'type': completion.type,
					'params': params,
				}

				# Jedi 0.7 (shipped with ubuntu) does not have this.
				if hasattr(completion, 'docstring'):
					suggestionData['docstring'] = completion.docstring()

				retData.append(suggestionData)
		except:
			raw=True
			retData = {
				'reqId': 'debug',
				'debug': True,
				'level': 'error',
				'stacktrace': traceback.format_exc()
			}

		self._write_response(retData, data, raw=raw)

	def _write_response(self, retData, data, raw=False):
		if not raw:
			reqId = data['reqId']
			ret = {'reqId': reqId,
					'prefix': data['prefix'],
					'suggestions': retData}
		else:
			ret = retData

		self.ostream.write(json.dumps(ret) + "\n")
{'fraction_non_alphanumeric': 0.1305602308247175, 'fraction_numerical': 0.0024044241404183697, 'mean_word_length': 3.6794150731158606, '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': '2797013', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1426, 'n_words': 327}