Skip to content
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

Feature/resource builders #158

Merged
merged 10 commits into from
Mar 9, 2021
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
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ List of supported libraries:
- [kaliningraph](https://github.com/breandan/kaliningraph) - Graph library with a DSL for constructing graphs and visualizing the behavior of graph algorithms
- [khttp](https://github.com/jkcclemens/khttp) - HTTP networking library
- [klaxon](https://github.com/cbeust/klaxon) - JSON parser for Kotlin
- [kmath](https://github.com/mipt-npm/kmath) - Experimental Kotlin mathematical library operating on generic algebras
- [kmath](https://github.com/mipt-npm/kmath) - Experimental Kotlin algebra-based mathematical library
- [koma](https://koma.kyonifer.com/index.html) - Scientific computing library
- [kotlin-dl](https://github.com/JetBrains/KotlinDL) - KotlinDL library which provides Keras-like API for deep learning
- [kotlin-statistics](https://github.com/thomasnield/kotlin-statistics) - Idiomatic statistical operators for Kotlin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.jetbrains.kotlinx.jupyter.api.libraries

import java.io.File
import java.util.logging.Logger

/**
* Build a resource tree using [ResourcesBuilder]. The builder allows to construct `js` and `css` bundles. Each bundle
* could contain multiple files or urls. Each of those could contain multiple fallback paths (for example if URL not found,
* local resource is used.
*/
fun JupyterIntegration.Builder.resources(block: ResourcesBuilder.() -> Unit) {
val resources = ResourcesBuilder().apply(block)
resources.resources.forEach {
resource(it)
}
}

class ResourcesBuilder {
internal val resources = ArrayList<LibraryResource>()

class BundleBuilder {
internal val bundles = ArrayList<ResourceFallbacksBundle>()

private fun checkLocalPath(localPath: String) {
if (!File(localPath).exists()) {
Logger.getLogger("JupyterIntegration").warning("A resource with local file path '$localPath' not found.")
}
}

private fun checkClassPath(classPath: String) {
if (javaClass.getResource(classPath) == null) {
Logger.getLogger("JupyterIntegration").warning("A resource with classpath '$classPath' not found.")
}
}

/**
* Create an url resource with optional embedding (governed by [embed] flag) with optional local file fallback and
* a class-path fallback. If fallbacks are null, they are not used.
*/
@OptIn(ExperimentalStdlibApi::class)
fun url(url: String, localFallBack: String? = null, classpathFallBack: String? = null, embed: Boolean = false) {
val libraryResource = ResourceFallbacksBundle(
buildList {
if (embed) {
add(ResourceLocation(url, ResourcePathType.URL_EMBEDDED))
} else {
add(ResourceLocation(url, ResourcePathType.URL))
}
localFallBack?.let {
checkLocalPath(localFallBack)
add(ResourceLocation(localFallBack, ResourcePathType.LOCAL_PATH))
}
classpathFallBack?.let {
checkClassPath(classpathFallBack)
add(ResourceLocation(classpathFallBack, ResourcePathType.CLASSPATH_PATH))
}
}
)
bundles.add(libraryResource)
}

/**
* Use local resource from file
*/
fun local(localPath: String) {
checkLocalPath(localPath)
bundles.add(
ResourceFallbacksBundle(
listOf(ResourceLocation(localPath, ResourcePathType.LOCAL_PATH))
)
)
}

/**
* Use Jar class-path resource
*/
fun classPath(classPath: String) {
checkClassPath(classPath)
bundles.add(
ResourceFallbacksBundle(
listOf(ResourceLocation(classPath, ResourcePathType.CLASSPATH_PATH))
)
)
}
}

/**
* Create a JS resource bundle
*/
fun js(name: String, block: BundleBuilder.() -> Unit) {
val bundles = BundleBuilder().apply(block).bundles
resources.add(
LibraryResource(
name = name,
type = ResourceType.JS,
bundles = bundles
)
)
}

/**
* Create a Css resource bundle
*/
fun css(name: String, block: BundleBuilder.() -> Unit) {
val bundles = BundleBuilder().apply(block).bundles
resources.add(
LibraryResource(
name = name,
type = ResourceType.CSS,
bundles = bundles
)
)
}
}
17 changes: 10 additions & 7 deletions libraries/kmath.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"description": "Experimental Kotlin mathematical library operating on generic algebras",
"description": "Experimental Kotlin algebra-based mathematical library",
"properties": {
"v": "0.1.3"
"v": "0.2.0"
},
"link": "https://github.com/mipt-npm/kmath",
"repositories": [
"https://dl.bintray.com/mipt-npm/scientifik"
"https://dl.bintray.com/mipt-npm/kscience",
"https://dl.bintray.com/mipt-npm/dev"
],
"dependencies": [
"scientifik:kmath-core-jvm:$v"
"kscience.kmath:kmath-commons:$v",
"kscience.kmath:kmath-for-real:$v"
],
"imports": [
"scientifik.kmath.linear.*",
"scientifik.kmath.operations.*",
"scientifik.kmath.structures.*"
"kscience.kmath.linear.*",
"kscience.kmath.operations.*",
"kscience.kmath.structures.*",
"kscience.kmath.real.*"
]
}
2 changes: 1 addition & 1 deletion libraries/plotly-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"https://dl.bintray.com/mipt-npm/dev"
],
"properties": {
"v": "0.2.0",
"v": "0.3.1",
"port": "8882"
},
"link": "https://github.com/mipt-npm/plotly.kt",
Expand Down
3 changes: 2 additions & 1 deletion libraries/plotly.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"kotlinx.html.*"
],
"repositories": [
"https://repo.kotlin.link",
"https://dl.bintray.com/mipt-npm/dataforge",
"https://dl.bintray.com/mipt-npm/kscience",
"https://dl.bintray.com/mipt-npm/dev"
],
"properties": {
"v": "0.2.0"
"v": "0.3.1"
},
"link": "https://github.com/mipt-npm/plotly.kt",
"description": "An experimental plotly.kt integration module. Supports static plots and HTML dashboards.",
Expand Down