<commit_before># pylint: disable=E0401,C0111,R0903

import os
import re
import json
import sys
import platform
import subprocess
import time

from deoplete.sources.base import Base
# from deoplete.util import load_external_module
from logging import getLogger

PY2 = int(sys.version[0]) == 2

if PY2:
    import urllib2 as request
    from urllib2 import HTTPError
else:  # Py3
    from urllib import request
    from urllib.error import HTTPError


opener = request.build_opener(request.ProxyHandler({}))
current = __file__
# load_external_module(current, 'sources/deoplete_ternjs')
# from profiler import timeit

logger = getLogger(__name__)
windows = platform.system() == "Windows"


class RequestError(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return self.message


class Source(Base):

    def __init__(self, vim):
        super(Source, self).__init__(vim)

        self.name = 'ternjs'
        self.mark = '[ternjs]'
        self.input_pattern = r'\.\w*'
        self.rank = 700
        self.filetypes = ['javascript', 'jsx', 'javascript.jsx']

        self._project_directory = None
        self.port = None
        self.localhost = (windows and "127.0.0.1") or "localhost"
        self.proc = None
        self.last_failed = 0
        self.cached = {'row': -1, 'end': -1}
        self._tern_command = 'tern'
        self._tern_arguments = ''
        self._tern_timeout = 1
        self._tern_show_signature = True
        self._tern_first_request = False
        self._tern_last_length = 0

        if vim.eval('exists("g:tern_request_timeout")'):
            self._tern_timeout = float(vim.eval("g:tern_request_timeout"))

        if vim.eval('exists("g:tern_show_signature_in_pum")'):
            self._tern_show_signature = vim.eval('g:tern_show_signature_in_pum') != '0'

    def __del__(self):
        self.stop_server()

    def start_server(self):
        if not self._tern_command:
            return None

        if time.time() - self.last_failed < 30:
            return None

        self._search_tern_project_dir()
        env = None

        if platform.system() == 'Darwin':
            env = os.environ.copy()
            env['PATH'] += ':/usr/local/bin'

        self.proc = subprocess.Popen(self._tern_command + ' ' + self._tern_arguments,
                                     cwd=self._project_directory, env=env,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     shell=True)
        output = ""

        while True:
            line = self.proc.stdout.readline().decode("utf-8")
            if not line:
                print("Failed to start server" + (output and ":\n" + output))
                self.last_failed = time.time()
                return None

            match = re.match("Listening on port (\\d+)", line)
            if match:
                self.port = int(match.group(1))
                return
            else:
                output += line

    def stop_server(self):
        if self.proc is None:
            return

        self.proc.stdin.close()
        self.proc.wait()
        self.proc = None

    def _search_tern_project_dir(self):
        if not self._project_directory:
            directory = self.vim.eval("expand('%:p:h')")

            if PY2:
                directory = directory.decode(self.vim.eval('&encoding'))

            if not os.path.isdir(directory):
                return ''

            if directory:
                self._project_directory = directory
                while True:<commit_msg>Start only one server<commit_after># pylint: disable=E0401,C0111,R0903

import os
import re
import json
import sys
import platform
import subprocess
import time

from deoplete.sources.base import Base
# from deoplete.util import load_external_module
from logging import getLogger

PY2 = int(sys.version[0]) == 2

if PY2:
    import urllib2 as request
    from urllib2 import HTTPError
else:  # Py3
    from urllib import request
    from urllib.error import HTTPError


opener = request.build_opener(request.ProxyHandler({}))
current = __file__
# load_external_module(current, 'sources/deoplete_ternjs')
# from profiler import timeit

logger = getLogger(__name__)
windows = platform.system() == "Windows"


class RequestError(Exception):

    def __init__(self, message):
        self.message = message

    def __str__(self):
        return self.message


class Source(Base):

    def __init__(self, vim):
        super(Source, self).__init__(vim)

        self.name = 'ternjs'
        self.mark = '[ternjs]'
        self.input_pattern = r'\.\w*'
        self.rank = 700
        self.filetypes = ['javascript', 'jsx', 'javascript.jsx']

        self._project_directory = None
        self.port = None
        self.localhost = (windows and "127.0.0.1") or "localhost"
        self.proc = None
        self.last_failed = 0
        self.cached = {'row': -1, 'end': -1}
        self._tern_command = 'tern'
        self._tern_arguments = ''
        self._tern_timeout = 1
        self._tern_show_signature = True
        self._tern_first_request = False
        self._tern_last_length = 0
        self._trying_to_start = False

        if vim.eval('exists("g:tern_request_timeout")'):
            self._tern_timeout = float(vim.eval("g:tern_request_timeout"))

        if vim.eval('exists("g:tern_show_signature_in_pum")'):
            self._tern_show_signature = vim.eval('g:tern_show_signature_in_pum') != '0'

    def __del__(self):
        self.stop_server()

    def start_server(self):
        if self._trying_to_start:
            return

        if not self._tern_command:
            return None

        if time.time() - self.last_failed < 30:
            return None

        self._trying_to_start = True
        self._search_tern_project_dir()
        env = None

        if platform.system() == 'Darwin':
            env = os.environ.copy()
            env['PATH'] += ':/usr/local/bin'

        self.proc = subprocess.Popen(self._tern_command + ' ' + self._tern_arguments,
                                     cwd=self._project_directory, env=env,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.STDOUT,
                                     shell=True)
        output = ""

        while True:
            line = self.proc.stdout.readline().decode("utf-8")
            if not line:
                print("Failed to start server" + (output and ":\n" + output))
                self.last_failed = time.time()
                self._trying_to_start = False
                return None

            match = re.match("Listening on port (\\d+)", line)
            if match:
                self.port = int(match.group(1))
                return
            else:
                output += line

    def stop_server(self):
        if self.proc is None:
            return

        self.proc.stdin.close()
        self.proc.wait()
        self.proc = None

    def _search_tern_project_dir(self):
        if not self._project_directory:
            directory = self.vim.eval("expand('%:p:h')")

            if PY2:
                directory = directory.decode(self.vim.eval('&encoding'))

            if not os.path.isdir(directory):
                return ''

            if directory:
                self._project_directory = directory
                while True:
{'fraction_non_alphanumeric': 0.10440499671268902, 'fraction_numerical': 0.010256410256410256, 'mean_word_length': 1.869105997736703, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1072820', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2162, 'n_words': 485}