<commit_before>'use babel';

import fs from 'fs';
import jmp from 'jmp';
import v4 from 'uuid/v4';

import Kernel from './kernel';
import InputView from './input-view';
import log from './log';

export default class ZMQKernel extends Kernel {
  constructor(kernelSpec, grammar, connection, connectionFile, kernelProcess) {
    super(kernelSpec, grammar);
    this.connection = connection;
    this.connectionFile = connectionFile;
    this.kernelProcess = kernelProcess;

    this.executionCallbacks = {};

    this._connect();

    if (this.kernelProcess) {
      log('ZMQKernel: @kernelProcess:', this.kernelProcess);

      const getKernelNotificationsRegExp = () => {
        try {
          const pattern = atom.config.get('Hydrogen.kernelNotifications');
          const flags = 'im';
          return new RegExp(pattern, flags);
        } catch (err) {
          return null;
        }
      };

      this.kernelProcess.stdout.on('data', (data) => {
        data = data.toString();

        log('ZMQKernel: stdout:', data);

        const regexp = getKernelNotificationsRegExp();
        if (regexp && regexp.test(data)) {
          atom.notifications.addInfo(this.kernelSpec.display_name, {
            description: data,
            dismissable: true,
          });
        }
      });

      this.kernelProcess.stderr.on('data', (data) => {
        data = data.toString();

        log('ZMQKernel: stderr:', data);

        const regexp = getKernelNotificationsRegExp();
        if (regexp && regexp.test(data)) {
          atom.notifications.addError(this.kernelSpec.display_name, {
            description: data,
            dismissable: true,
          });
        }
      });
    } else {
      log('ZMQKernel: connectionFile:', this.connectionFile);
      atom.notifications.addInfo('Using an existing kernel connection');
    }
  }


  _connect() {
    const scheme = this.connection.signature_scheme.slice('hmac-'.length);
    const { key } = this.connection;

    this.shellSocket = new jmp.Socket('dealer', scheme, key);
    this.controlSocket = new jmp.Socket('dealer', scheme, key);
    this.stdinSocket = new jmp.Socket('dealer', scheme, key);
    this.ioSocket = new jmp.Socket('sub', scheme, key);

    const id = v4();
    this.shellSocket.identity = `dealer${id}`;
    this.controlSocket.identity = `control${id}`;
    this.stdinSocket.identity = `dealer${id}`;
    this.ioSocket.identity = `sub${id}`;

    const address = `${this.connection.transport}://${this.connection.ip}:`;
    this.shellSocket.connect(address + this.connection.shell_port);
    this.controlSocket.connect(address + this.connection.control_port);
    this.ioSocket.connect(address + this.connection.iopub_port);
    this.ioSocket.subscribe('');
    this.stdinSocket.connect(address + this.connection.stdin_port);

    this.shellSocket.on('message', this.onShellMessage.bind(this));
    this.ioSocket.on('message', this.onIOMessage.bind(this));
    this.stdinSocket.on('message', this.onStdinMessage.bind(this));

    this.shellSocket.on('connect', () => log('shellSocket connected'));
    this.controlSocket.on('connect', () => log('controlSocket connected'));
    this.ioSocket.on('connect', () => log('ioSocket connected'));
    this.stdinSocket.on('connect', () => log('stdinSocket connected'));

    try {
      this.shellSocket.monitor();
      this.controlSocket.monitor();
      this.ioSocket.monitor();
      this.stdinSocket.monitor();
    } catch (err) {
      console.error('Kernel:', err);
    }
  }


  interrupt() {
    if (this.kernelProcess) {
      log('ZMQKernel: sending SIGINT');
      this.kernelProcess.kill('SIGINT');
    } else {
      log('ZMQKernel: cannot interrupt an existing kernel');
      atom.notifications.addWarning('Cannot interrupt this kernel');
    }
  }


  _kill() {
    if (this.kernelProcess) {
      log('ZMQKernel: sending SIGKILL');
      this.kernelProcess.kill('SIGKILL');
    } else {
      log('ZMQKernel: cannot kill an existing kernel');<commit_msg>Disable kernel interrupt on windows<commit_after>'use babel';

import fs from 'fs';
import jmp from 'jmp';
import v4 from 'uuid/v4';

import Kernel from './kernel';
import InputView from './input-view';
import log from './log';

export default class ZMQKernel extends Kernel {
  constructor(kernelSpec, grammar, connection, connectionFile, kernelProcess) {
    super(kernelSpec, grammar);
    this.connection = connection;
    this.connectionFile = connectionFile;
    this.kernelProcess = kernelProcess;

    this.executionCallbacks = {};

    this._connect();

    if (this.kernelProcess) {
      log('ZMQKernel: @kernelProcess:', this.kernelProcess);

      const getKernelNotificationsRegExp = () => {
        try {
          const pattern = atom.config.get('Hydrogen.kernelNotifications');
          const flags = 'im';
          return new RegExp(pattern, flags);
        } catch (err) {
          return null;
        }
      };

      this.kernelProcess.stdout.on('data', (data) => {
        data = data.toString();

        log('ZMQKernel: stdout:', data);

        const regexp = getKernelNotificationsRegExp();
        if (regexp && regexp.test(data)) {
          atom.notifications.addInfo(this.kernelSpec.display_name, {
            description: data,
            dismissable: true,
          });
        }
      });

      this.kernelProcess.stderr.on('data', (data) => {
        data = data.toString();

        log('ZMQKernel: stderr:', data);

        const regexp = getKernelNotificationsRegExp();
        if (regexp && regexp.test(data)) {
          atom.notifications.addError(this.kernelSpec.display_name, {
            description: data,
            dismissable: true,
          });
        }
      });
    } else {
      log('ZMQKernel: connectionFile:', this.connectionFile);
      atom.notifications.addInfo('Using an existing kernel connection');
    }
  }


  _connect() {
    const scheme = this.connection.signature_scheme.slice('hmac-'.length);
    const { key } = this.connection;

    this.shellSocket = new jmp.Socket('dealer', scheme, key);
    this.controlSocket = new jmp.Socket('dealer', scheme, key);
    this.stdinSocket = new jmp.Socket('dealer', scheme, key);
    this.ioSocket = new jmp.Socket('sub', scheme, key);

    const id = v4();
    this.shellSocket.identity = `dealer${id}`;
    this.controlSocket.identity = `control${id}`;
    this.stdinSocket.identity = `dealer${id}`;
    this.ioSocket.identity = `sub${id}`;

    const address = `${this.connection.transport}://${this.connection.ip}:`;
    this.shellSocket.connect(address + this.connection.shell_port);
    this.controlSocket.connect(address + this.connection.control_port);
    this.ioSocket.connect(address + this.connection.iopub_port);
    this.ioSocket.subscribe('');
    this.stdinSocket.connect(address + this.connection.stdin_port);

    this.shellSocket.on('message', this.onShellMessage.bind(this));
    this.ioSocket.on('message', this.onIOMessage.bind(this));
    this.stdinSocket.on('message', this.onStdinMessage.bind(this));

    this.shellSocket.on('connect', () => log('shellSocket connected'));
    this.controlSocket.on('connect', () => log('controlSocket connected'));
    this.ioSocket.on('connect', () => log('ioSocket connected'));
    this.stdinSocket.on('connect', () => log('stdinSocket connected'));

    try {
      this.shellSocket.monitor();
      this.controlSocket.monitor();
      this.ioSocket.monitor();
      this.stdinSocket.monitor();
    } catch (err) {
      console.error('Kernel:', err);
    }
  }


  interrupt() {
    if (process.platform === 'win32') {
      atom.notifications.addWarning('Cannot interrupt this kernel', {
        detail: 'Kernel interruption is currently not supported in Windows.',
      });
    } else if (this.kernelProcess) {
      log('ZMQKernel: sending SIGINT');
      this.kernelProcess.kill('SIGINT');
    } else {
      log('ZMQKernel: cannot interrupt an existing kernel');
      atom.notifications.addWarning('Cannot interrupt an existing kernel');
    }
  }


  _kill() {
    if (this.kernelProcess) {
      log('ZMQKernel: sending SIGKILL');
      this.kernelProcess.kill('SIGKILL');
    } else {
      log('ZMQKernel: cannot kill an existing kernel');
{'fraction_non_alphanumeric': 0.14724447681476086, 'fraction_numerical': 0.0009711094925952901, 'mean_word_length': 3.0466601178781927, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '462016', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2389, 'n_words': 471}