<commit_before>/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
* */
package io.github.mandar2812.dynaml.jupyter

import almond.amm.AmmInterpreter
import almond._
import almond.internals._
import almond.interpreter._
import almond.interpreter.api.{CommHandler, OutputHandler}
import almond.interpreter.input.InputManager
import almond.interpreter.util.AsyncInterpreterOps
import almond.logger.LoggerContext
import almond.protocol.KernelInfo
import ammonite.interp.Parsers
import ammonite.repl.{ReplApiImpl => _, _}
import ammonite.runtime._
import ammonite.util._
import fastparse.Parsed
import io.github.soc.directories.ProjectDirectories

import scala.util.control.NonFatal

/** Holds bits of state for the interpreter, and implements [[almond.interpreter.Interpreter]]. */
final class DynaMLJupyter(
  params: ScalaInterpreterParams = ScalaInterpreterParams(),
  val logCtx: LoggerContext = LoggerContext.nop
) extends Interpreter with AsyncInterpreterOps {

  private val log = logCtx(getClass)

  private val frames0: Ref[List[Frame]] = Ref(List(Frame.createInitial(params.initialClassLoader)))

  private val inspections = new ScalaInterpreterInspections(
    logCtx,
    params.metabrowse,
    params.metabrowseHost,
    params.metabrowsePort,
    ammInterp.compilerManager.pressy.compiler,
    frames0()
  )

  private val colors0: Ref[Colors] = Ref(params.initialColors)

  private var commHandlerOpt = Option.empty[CommHandler]

  private val storage =
    if (params.disableCache)
      Storage.InMemory()
    else
      new Storage.Folder(os.Path(ProjectDirectories.from(null, null, "Almond").cacheDir) / "ammonite")

  private val execute0 = new Execute(
    params.trapOutput,
    storage,
    logCtx,
    params.updateBackgroundVariablesEcOpt,
    commHandlerOpt
  )


  lazy val ammInterp: ammonite.interp.Interpreter = {

    val sessApi = new SessionApiImpl(frames0)

    val replApi =
      new ReplApiImpl(
        execute0,
        storage,
        colors0,
        ammInterp,
        sessApi
      )

    val jupyterApi =
      new JupyterApiImpl(execute0, commHandlerOpt, replApi)

    for (ec <- params.updateBackgroundVariablesEcOpt)
      UpdatableFuture.setup(replApi, jupyterApi, ec)

    AmmInterpreter(
      execute0,
      storage,
      replApi,
      jupyterApi,
      params.predefCode,
      params.predefFiles,
      frames0,
      params.codeWrapper,
      params.extraRepos,
      params.automaticDependencies,<commit_msg>dynaml-notebook:   -- Fix for latest version of almond api.<commit_after>/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
* */
package io.github.mandar2812.dynaml.jupyter

import almond.amm.AmmInterpreter
import almond._
import almond.internals._
import almond.interpreter._
import almond.interpreter.api.{CommHandler, OutputHandler}
import almond.interpreter.input.InputManager
import almond.interpreter.util.AsyncInterpreterOps
import almond.logger.LoggerContext
import almond.protocol.KernelInfo
import ammonite.interp.Parsers
import ammonite.repl.{ReplApiImpl => _, _}
import ammonite.runtime._
import ammonite.util._
import fastparse.Parsed
import io.github.soc.directories.ProjectDirectories

import scala.util.control.NonFatal

/** Holds bits of state for the interpreter, and implements [[almond.interpreter.Interpreter]]. */
final class DynaMLJupyter(
  params: ScalaInterpreterParams = ScalaInterpreterParams(),
  val logCtx: LoggerContext = LoggerContext.nop,
  val silent: Ref[Boolean] = Ref(false)
) extends Interpreter with AsyncInterpreterOps {

  private val log = logCtx(getClass)

  private val frames0: Ref[List[Frame]] = Ref(List(Frame.createInitial(params.initialClassLoader)))

  private val inspections = new ScalaInterpreterInspections(
    logCtx,
    params.metabrowse,
    params.metabrowseHost,
    params.metabrowsePort,
    ammInterp.compilerManager.pressy.compiler,
    frames0()
  )

  private val colors0: Ref[Colors] = Ref(params.initialColors)

  private var commHandlerOpt = Option.empty[CommHandler]

  private val storage =
    if (params.disableCache)
      Storage.InMemory()
    else
      new Storage.Folder(os.Path(ProjectDirectories.from(null, null, "Almond").cacheDir) / "ammonite")

  private val execute0 = new Execute(
    params.trapOutput,
    storage,
    logCtx,
    params.updateBackgroundVariablesEcOpt,
    commHandlerOpt,
    silent
  )


  lazy val ammInterp: ammonite.interp.Interpreter = {

    val sessApi = new SessionApiImpl(frames0)

    val replApi =
      new ReplApiImpl(
        execute0,
        storage,
        colors0,
        ammInterp,
        sessApi
      )

    val jupyterApi =
      new JupyterApiImpl(execute0, commHandlerOpt, replApi, silent)

    for (ec <- params.updateBackgroundVariablesEcOpt)
      UpdatableFuture.setup(replApi, jupyterApi, ec)

    AmmInterpreter(
      execute0,
      storage,
      replApi,
      jupyterApi,
      params.predefCode,
      params.predefFiles,
      frames0,
      params.codeWrapper,
      params.extraRepos,
      params.automaticDependencies,
{'fraction_non_alphanumeric': 0.0738327904451683, 'fraction_numerical': 0.005583992554676594, 'mean_word_length': 3.9447852760736195, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3876987', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1879, 'n_words': 538}