-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default resolution info detection heuristics
- Loading branch information
Showing
10 changed files
with
89 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/org/jetbrains/kotlin/jupyter/libraries/ResolutionInfoProvider.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.jetbrains.kotlin.jupyter.libraries | ||
|
||
import org.jetbrains.kotlin.jupyter.GitHubApiPrefix | ||
import org.jetbrains.kotlin.jupyter.LibrariesDir | ||
import java.io.File | ||
import java.net.URL | ||
|
||
interface ResolutionInfoProvider { | ||
var fallback: LibraryResolutionInfo | ||
|
||
fun get(): LibraryResolutionInfo = fallback | ||
fun get(string: String): LibraryResolutionInfo | ||
} | ||
|
||
object EmptyResolutionInfoProvider : ResolutionInfoProvider { | ||
private val fallbackInfo = LibraryResolutionInfo.ByNothing() | ||
|
||
override var fallback: LibraryResolutionInfo | ||
get() = fallbackInfo | ||
set(_) {} | ||
|
||
override fun get(string: String) = LibraryResolutionInfo.getInfoByRef(string) | ||
} | ||
|
||
class StandardResolutionInfoProvider(override var fallback: LibraryResolutionInfo) : ResolutionInfoProvider { | ||
override fun get(string: String): LibraryResolutionInfo { | ||
return tryGetAsRef(string) ?: tryGetAsDir(string) ?: tryGetAsFile(string) ?: tryGetAsURL(string) ?: fallback | ||
} | ||
|
||
private fun tryGetAsRef(ref: String): LibraryResolutionInfo? { | ||
val response = khttp.get("$GitHubApiPrefix/contents/$LibrariesDir?ref=$ref") | ||
return if (response.statusCode == 200) LibraryResolutionInfo.getInfoByRef(ref) else null | ||
} | ||
|
||
private fun tryGetAsDir(dirName: String): LibraryResolutionInfo? { | ||
val file = File(dirName) | ||
return if (file.isDirectory) LibraryResolutionInfo.ByDir(file) else null | ||
} | ||
|
||
private fun tryGetAsFile(fileName: String): LibraryResolutionInfo? { | ||
val file = File(fileName) | ||
return if (file.isFile) LibraryResolutionInfo.ByFile(file) else null | ||
} | ||
|
||
private fun tryGetAsURL(url: String): LibraryResolutionInfo? { | ||
val response = khttp.get(url) | ||
return if (response.statusCode == 200) LibraryResolutionInfo.ByURL(URL(url)) else null | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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