-
Notifications
You must be signed in to change notification settings - Fork 113
Descriptors in libraries.json #488
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e71460b
added `descriptors` option for libraries.json files. This can contain…
Jolanrensen a3a7fed
updated README for `descriptors` option for libraries.json files.
Jolanrensen b8c9f41
fixes for `descriptors` option for libraries.json files based on feed…
Jolanrensen 2d40bc2
linting
Jolanrensen 42ed64d
removed testIncorrectDescriptors() test as unsupported keys are now i…
Jolanrensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.jetbrains.kotlinx.jupyter.libraries | ||
|
||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost | ||
import org.jetbrains.kotlinx.jupyter.api.LibraryLoader | ||
import org.jetbrains.kotlinx.jupyter.api.Notebook | ||
|
@@ -13,6 +14,7 @@ import org.jetbrains.kotlinx.jupyter.api.libraries.LibrariesInstantiable | |
import org.jetbrains.kotlinx.jupyter.api.libraries.LibrariesProducerDeclaration | ||
import org.jetbrains.kotlinx.jupyter.api.libraries.LibrariesScanResult | ||
import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryDefinition | ||
import org.jetbrains.kotlinx.jupyter.api.libraries.Variable | ||
import org.jetbrains.kotlinx.jupyter.config.errorForUser | ||
import org.jetbrains.kotlinx.jupyter.protocol.api.KernelLoggerFactory | ||
import org.jetbrains.kotlinx.jupyter.protocol.api.getLogger | ||
|
@@ -24,9 +26,11 @@ class LibrariesScanner( | |
loggerFactory: KernelLoggerFactory, | ||
) : LibraryLoader { | ||
private val logger = loggerFactory.getLogger(this::class) | ||
private val jsonParser = Json { ignoreUnknownKeys = true } | ||
|
||
private val processedFQNs = mutableSetOf<TypeName>() | ||
private val discardedFQNs = mutableSetOf<TypeName>() | ||
private val processedDescriptorHashes = mutableSetOf<Int>() | ||
|
||
private fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNamesToLoad( | ||
host: KotlinKernelHost, | ||
|
@@ -46,10 +50,14 @@ class LibrariesScanner( | |
discardedFQNs.add(typeName) | ||
false | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ktlint adds it because of the |
||
null -> typeName !in discardedFQNs && processedFQNs.add(typeName) | ||
} | ||
} | ||
|
||
/** Makes sure each unique descriptor is only loaded once by caching their hashes in [processedDescriptorHashes]. */ | ||
private fun Iterable<JsonObject>.filterDescriptorsToLoad() = filter { processedDescriptorHashes.add(it.hashCode()) } | ||
|
||
fun addLibrariesFromClassLoader( | ||
classLoader: ClassLoader, | ||
host: KotlinKernelHost, | ||
|
@@ -83,24 +91,30 @@ class LibrariesScanner( | |
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) | ||
} | ||
classLoader | ||
.getResources("$KOTLIN_JUPYTER_RESOURCES_PATH/$KOTLIN_JUPYTER_LIBRARIES_FILE_NAME") | ||
.toList() | ||
.map { url -> | ||
val contents = url.readText() | ||
jsonParser.decodeFromString<LibrariesScanResult>(contents) | ||
} | ||
|
||
val definitions = mutableListOf<LibrariesDefinitionDeclaration>() | ||
val producers = mutableListOf<LibrariesProducerDeclaration>() | ||
val descriptors = mutableListOf<JsonObject>() | ||
|
||
for (result in results) { | ||
definitions.addAll(result.definitions) | ||
producers.addAll(result.producers) | ||
descriptors.addAll(result.descriptors) | ||
} | ||
|
||
fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNames() = filterNamesToLoad(host, integrationTypeNameRules) | ||
|
||
return LibrariesScanResult( | ||
definitions.filterNames(), | ||
producers.filterNames(), | ||
descriptors.filterDescriptorsToLoad(), | ||
) | ||
} | ||
|
||
|
@@ -140,6 +154,14 @@ class LibrariesScanner( | |
} | ||
} | ||
} | ||
|
||
scanResult.descriptors.forEach { | ||
val descriptor = parseLibraryDescriptor(it) | ||
val arguments = libraryOptions.map { (name, value) -> Variable(name, value) } | ||
val definition = descriptor.convertToDefinition(arguments) | ||
definitions.add(definition) | ||
} | ||
|
||
return definitions | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.