@@ -16,7 +16,9 @@ import java.io.File
1616import java.io.FileNotFoundException
1717import java.io.InputStream
1818import java.net.URL
19+ import java.nio.file.Path
1920import java.util.ServiceLoader
21+ import kotlin.io.path.extension
2022import kotlin.reflect.KType
2123
2224public sealed interface SupportedFormat {
@@ -37,6 +39,9 @@ public sealed interface SupportedFormatSample {
3739 @JvmInline
3840 public value class DataFile (public val sampleFile : File ) : SupportedFormatSample
3941
42+ @JvmInline
43+ public value class DataPath (public val samplePath : Path ) : SupportedFormatSample
44+
4045 @JvmInline
4146 public value class DataUrl (public val sampleUrl : URL ) : SupportedFormatSample
4247
@@ -56,7 +61,10 @@ public sealed interface SupportedFormatSample {
5661public interface SupportedDataFrameFormat : SupportedFormat {
5762 public fun readDataFrame (stream : InputStream , header : List <String > = emptyList()): DataFrame <* >
5863
59- public fun readDataFrame (file : File , header : List <String > = emptyList()): DataFrame <* >
64+ public fun readDataFrame (file : File , header : List <String > = emptyList()): DataFrame <* > =
65+ readDataFrame(file.toPath(), header)
66+
67+ public fun readDataFrame (path : Path , header : List <String > = emptyList()): DataFrame <* >
6068}
6169
6270/* *
@@ -137,10 +145,10 @@ internal fun guessFormatForExtension(
137145): SupportedFormat ? = formats.firstOrNull { it.acceptsExtension(ext) && (sample == null || it.acceptsSample(sample)) }
138146
139147internal fun guessFormat (
140- file : File ,
148+ path : Path ,
141149 formats : List <SupportedFormat > = supportedFormats,
142- sample : SupportedFormatSample .DataFile ? = SupportedFormatSample .DataFile (file ),
143- ): SupportedFormat ? = guessFormatForExtension(file .extension.lowercase(), formats, sample = sample)
150+ sample : SupportedFormatSample .DataPath ? = SupportedFormatSample .DataPath (path ),
151+ ): SupportedFormat ? = guessFormatForExtension(path .extension.lowercase(), formats, sample = sample)
144152
145153internal fun guessFormat (
146154 url : URL ,
@@ -223,15 +231,15 @@ internal fun DataFrame.Companion.read(
223231}
224232
225233internal fun DataFrame.Companion.read (
226- file : File ,
234+ path : Path ,
227235 format : SupportedDataFrameFormat ? = null,
228236 header : List <String > = emptyList(),
229237 formats : List <SupportedDataFrameFormat > = supportedFormats.filterIsInstance<SupportedDataFrameFormat >(),
230238): ReadAnyFrame {
231- if (format != null ) return format to format.readDataFrame(file , header = header)
239+ if (format != null ) return format to format.readDataFrame(path , header = header)
232240 formats.sortedBy { it.testOrder }.forEach {
233241 try {
234- return it to it.readDataFrame(file , header = header)
242+ return it to it.readDataFrame(path , header = header)
235243 } catch (e: FileNotFoundException ) {
236244 throw e
237245 } catch (e: Exception ) {
@@ -249,16 +257,10 @@ internal data class GeneratedCode(val format: SupportedCodeGenerationFormat, val
249257internal infix fun SupportedCodeGenerationFormat.to (code : Code ) = GeneratedCode (this , code)
250258
251259public fun DataFrame.Companion.read (file : File , header : List <String > = emptyList()): AnyFrame =
252- read(
253- file = file,
254- format = guessFormat(file)?.also {
255- if (it !is SupportedDataFrameFormat ) error(" Format $it does not support reading dataframes" )
256- } as SupportedDataFrameFormat ? ,
257- header = header,
258- ).df
260+ read(file.toPath(), header)
259261
260262public fun DataRow.Companion.read (file : File , header : List <String > = emptyList()): AnyRow =
261- DataFrame .read(file, header).single()
263+ DataFrame .read(file.toPath() , header).single()
262264
263265public fun DataFrame.Companion.read (url : URL , header : List <String > = emptyList()): AnyFrame =
264266 when {
@@ -293,3 +295,19 @@ public fun URL.readDataRow(header: List<String> = emptyList()): AnyRow = DataRow
293295public fun File.readDataFrame (header : List <String > = emptyList()): AnyFrame = DataFrame .read(this , header)
294296
295297public fun File.readDataRow (header : List <String > = emptyList()): AnyRow = DataRow .read(this , header)
298+
299+ public fun DataFrame.Companion.read (path : Path , header : List <String > = emptyList()): AnyFrame =
300+ read(
301+ path = path,
302+ format = guessFormat(path)?.also {
303+ if (it !is SupportedDataFrameFormat ) error(" Format $it does not support reading dataframes" )
304+ } as SupportedDataFrameFormat ? ,
305+ header = header,
306+ ).df
307+
308+ public fun DataRow.Companion.read (path : Path , header : List <String > = emptyList()): AnyRow =
309+ DataFrame .read(path, header).single()
310+
311+ public fun Path.readDataFrame (header : List <String > = emptyList()): AnyFrame = DataFrame .read(this , header)
312+
313+ public fun Path.readDataRow (header : List <String > = emptyList()): AnyRow = DataRow .read(this , header)
0 commit comments