All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.libraries.LibraryDescriptorsProvider.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.libraries

import org.jetbrains.kotlinx.jupyter.api.KernelLoggerFactory
import org.jetbrains.kotlinx.jupyter.api.getLogger
import org.jetbrains.kotlinx.jupyter.config.catchAll
import org.jetbrains.kotlinx.jupyter.config.kernelClassLoader
import org.jetbrains.kotlinx.jupyter.exceptions.ReplException

interface LibraryDescriptorsProvider {
    fun getDescriptors(): Map

    fun getDescriptorForVersionsCompletion(fullName: String): LibraryDescriptor? {
        return getDescriptors()[fullName]
    }

    fun getDescriptorGlobalOptions(): LibraryDescriptorGlobalOptions = DefaultLibraryDescriptorGlobalOptions
}

open class ResourceLibraryDescriptorsProvider(
    loggerFactory: KernelLoggerFactory,
) : LibraryDescriptorsProvider {
    private val logger = loggerFactory.getLogger(this::class)

    private val librariesFromResources: Map by lazy {
        val listText = kernelClassLoader.getResource("$RESOURCES_LIBRARY_PATH/libraries.list")

        listText
            ?.readText()
            ?.lineSequence()
            .orEmpty()
            .filter { it.isNotEmpty() }
            .mapNotNull { descriptorFile ->
                kernelClassLoader.getResource("$RESOURCES_LIBRARY_PATH/$descriptorFile")?.readText()?.let { text ->
                    val libraryName = descriptorFile.removeSuffix(".json")
                    logger.info("Parsing library $libraryName from resources")
                    logger.catchAll(msg = "Parsing descriptor for library '$libraryName' failed") {
                        libraryName to parseLibraryDescriptor(text)
                    }
                }
            }
            .toMap()
    }

    private val descriptorOptionsFromResources: LibraryDescriptorGlobalOptions by lazy {
        val optionsText =
            kernelClassLoader
                .getResource("$RESOURCES_LIBRARY_PATH/global.options")
                ?.readText() ?: return@lazy DefaultLibraryDescriptorGlobalOptions

        try {
            parseLibraryDescriptorGlobalOptions(optionsText)
        } catch (e: ReplException) {
            DefaultLibraryDescriptorGlobalOptions
        }
    }

    override fun getDescriptors(): Map {
        return librariesFromResources
    }

    override fun getDescriptorGlobalOptions(): LibraryDescriptorGlobalOptions {
        return descriptorOptionsFromResources
    }

    companion object {
        private const val RESOURCES_LIBRARY_PATH = "jupyterLibraries"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy