Skip to content

integrationTypeNameRules support for LibrariesScanner #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,13 @@ class PatternNameAcceptanceRule(
* 3) returns `null` if all acceptance results are `null` or the iterable is empty
*/
fun <T> Iterable<AcceptanceRule<T>>.accepts(obj: T): Boolean? {
return mapNotNull { it.accepts(obj) }.lastOrNull()
return unionAcceptance(map { it.accepts(obj) })
}

fun unionAcceptance(results: Iterable<Boolean?>): Boolean? {
return results.filterNotNull().lastOrNull()
}

fun unionAcceptance(vararg result: Boolean?): Boolean? {
return unionAcceptance(result.toList())
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryDefinition
import org.jetbrains.kotlinx.jupyter.config.errorForUser
import org.jetbrains.kotlinx.jupyter.config.getLogger
import org.jetbrains.kotlinx.jupyter.exceptions.ReplException
import org.jetbrains.kotlinx.jupyter.util.AcceptanceRule
import org.jetbrains.kotlinx.jupyter.util.accepts
import org.jetbrains.kotlinx.jupyter.util.unionAcceptance

class LibrariesScanner(val notebook: Notebook) {
private val processedFQNs = mutableSetOf<TypeName>()
private val discardedFQNs = mutableSetOf<TypeName>()

private fun <T, I : LibrariesInstantiable<T>> Iterable<I>.filterNamesToLoad(host: KotlinKernelHost): List<I> {
private fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNamesToLoad(
host: KotlinKernelHost,
integrationTypeNameRules: Iterable<AcceptanceRule<TypeName>>,
): List<I> {
return filter {
val typeName = it.fqn
val acceptance = host.acceptsIntegrationTypeName(typeName)
val acceptance = unionAcceptance(
host.acceptsIntegrationTypeName(typeName),
integrationTypeNameRules.accepts(typeName)
)
log.debug("Acceptance result for $typeName: $acceptance")
when (acceptance) {
true -> processedFQNs.add(typeName)
Expand All @@ -37,15 +46,15 @@ class LibrariesScanner(val notebook: Notebook) {
}
}

fun addLibrariesFromClassLoader(classLoader: ClassLoader, host: KotlinKernelHost) {
val scanResult = scanForLibraries(classLoader, host)
fun addLibrariesFromClassLoader(classLoader: ClassLoader, host: KotlinKernelHost, integrationTypeNameRules: List<AcceptanceRule<TypeName>> = listOf()) {
val scanResult = scanForLibraries(classLoader, host, integrationTypeNameRules)
log.debug("Scanning for libraries is done. Detected FQNs: ${Json.encodeToString(scanResult)}")
val libraries = instantiateLibraries(classLoader, scanResult, notebook)
log.debug("Number of detected definitions: ${libraries.size}")
host.addLibraries(libraries)
}

private fun scanForLibraries(classLoader: ClassLoader, host: KotlinKernelHost): LibrariesScanResult {
private fun scanForLibraries(classLoader: ClassLoader, host: KotlinKernelHost, integrationTypeNameRules: List<AcceptanceRule<TypeName>> = listOf()): LibrariesScanResult {
val results = classLoader.getResources("$KOTLIN_JUPYTER_RESOURCES_PATH/$KOTLIN_JUPYTER_LIBRARIES_FILE_NAME").toList().map { url ->
val contents = url.readText()
Json.decodeFromString<LibrariesScanResult>(contents)
Expand All @@ -59,9 +68,11 @@ class LibrariesScanner(val notebook: Notebook) {
producers.addAll(result.producers)
}

fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNames() = filterNamesToLoad(host, integrationTypeNameRules)

return LibrariesScanResult(
definitions.filterNamesToLoad(host),
producers.filterNamesToLoad(host),
definitions.filterNames(),
producers.filterNames(),
)
}

Expand Down