Skip to content

Commit 08b3483

Browse files
committed
moved text support api into own namespace
1 parent 8dbc3b5 commit 08b3483

10 files changed

Lines changed: 62 additions & 38 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ task javadocJar(type: Jar, dependsOn: javadoc) {
4949

5050
//http://stackoverflow.com/questions/34377367/why-is-gradle-install-replacing-my-version-with-unspecified
5151
group = 'de.mpicbg.scicomp'
52-
version = '1.1.4'
53-
//version = '1.1-SNAPSHOT'
52+
//version = '1.1.4'
53+
version = '1.2-SNAPSHOT'
5454

5555

5656
artifacts {

src/main/kotlin/kscript/KscriptUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlin.system.exitProcess
66
* @author Holger Brandl
77
*/
88

9-
/** Similar to require but without a stacktrace. */
9+
/** Similar to require but without a stacktrace which makes it more suited for CLIs. */
1010
inline fun stopIfNot(value: Boolean, lazyMessage: () -> Any) {
1111
if (!value) {
1212
System.err.println("[ERROR] " + lazyMessage().toString())

src/main/kotlin/kscript/experimental/OneLinerContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package kscript.experimental
22

3-
import kscript.resolveArgFile
3+
import kscript.text.resolveArgFile
44

55
/**
66
* @author Holger Brandl
@@ -9,7 +9,7 @@ import kscript.resolveArgFile
99
abstract class OneLinerContext(args: Array<String>) {
1010

1111
val arg by lazy { resolveArgFile(args) }
12-
// val stdin by lazy { kscript.stdin }
12+
// val stdin by lazy { kscript.support.getStdin }
1313

1414
init {
1515
apply(arg)

src/main/kotlin/kscript/StreamUtil.kt renamed to src/main/kotlin/kscript/text/StreamUtil.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
package kscript
2-
3-
import java.io.*
4-
import java.util.zip.GZIPOutputStream
1+
package kscript.text
52

63

74
/** a `Sequence<String>` iterator for standard input */
85
val stdin by lazy { generateSequence() { readLine() } }
96

10-
fun linesFrom(file: File) = BufferedReader(FileReader(file)).lineSequence()
7+
fun linesFrom(file: java.io.File) = java.io.BufferedReader(java.io.FileReader(file)).lineSequence()
118

129
// just used for testing and development
1310
fun linesFrom(vararg lines: String) = lines.asSequence()
@@ -17,24 +14,34 @@ fun linesFrom(vararg lines: String) = lines.asSequence()
1714
* File argument processor that works similar to awk. If data is available on stdin, use it. If not expect a file argument and read from that one instead.
1815
* */
1916
fun resolveArgFile(args: Array<String>, position: Int = 0): Sequence<String> {
20-
if (stdin.iterator().hasNext()) return stdin
17+
// if (stdin.iterator().hasNext()) return stdin
18+
if (System.`in`.available() > 0) return kscript.text.stdin
2119

22-
stopIfNot(args.isNotEmpty()) { "Missing file or input input stream" }
23-
stopIfNot(args.size >= position) { "arg position ${position} exceeds number of arguments ${args.size} " }
20+
kscript.stopIfNot(args.isNotEmpty()) { "Missing file or input input stream" }
21+
kscript.stopIfNot(args.size >= position) { "arg position ${position} exceeds number of arguments ${args.size} " }
2422

2523
val fileArg = args[position]
2624

2725
// stdinNames: List<String> = listOf("-", "stdin")
2826
// if (stdinNames.contains(fileArg)) return stdin
2927

30-
val inputFile = File(fileArg)
28+
val inputFile = java.io.File(fileArg)
29+
30+
kscript.stopIfNot(inputFile.canRead()) { "Can not read from '${fileArg}'" }
31+
32+
// test for compression and uncompress files automatically
33+
val isCompressedInput = inputFile.name.run { endsWith(".zip") || endsWith(".gz") }
34+
35+
val lineReader = if (isCompressedInput) {
36+
java.io.InputStreamReader(java.util.zip.GZIPInputStream(java.io.FileInputStream(inputFile)))
37+
} else {
38+
java.io.FileReader(inputFile)
39+
}
3140

32-
stopIfNot(inputFile.canRead()) { "Can not read from '${fileArg}'" }
3341

3442
// todo we don't close the buffer with this approach
3543
// BufferedReader(FileReader(inputFile )).use { return it }
36-
37-
return BufferedReader(FileReader(inputFile)).lineSequence()
44+
return java.io.BufferedReader(lineReader).lineSequence()
3845
}
3946

4047

@@ -48,7 +55,7 @@ fun Iterable<String>.print() = forEach { println(it) }
4855

4956
//https://dzone.com/articles/readingwriting-compressed-and
5057
/** Save a list of items into a file. Output can be option ally zipped and a the stringifying operation can be changed from toString to custom operation if needed. */
51-
fun <T> Iterable<T>.saveAs(f: File,
58+
fun <T> Iterable<T>.saveAs(f: java.io.File,
5259
transform: (T) -> String = { it.toString() },
5360
separator: Char = '\n',
5461
overwrite: Boolean = true,
@@ -57,7 +64,7 @@ fun <T> Iterable<T>.saveAs(f: File,
5764
// ensure that file is not yet there or overwrite flag is set
5865
require(!f.isFile || overwrite) { "$f is present already. Use overwrite=true to enforce file replacement." }
5966

60-
val p = if (!compress) PrintWriter(f) else BufferedWriter(OutputStreamWriter(GZIPOutputStream(FileOutputStream(f))))
67+
val p = if (!compress) java.io.PrintWriter(f) else java.io.BufferedWriter(java.io.OutputStreamWriter(java.util.zip.GZIPOutputStream(java.io.FileOutputStream(f))))
6168

6269
toList().forEach { p.write(transform(it) + separator) }
6370

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kscript
1+
package kscript.text
22

33
/**
44
* Utility methods to allow for awk-like data processing using kscript.
@@ -50,7 +50,7 @@ fun Sequence<Row>.add(rule: (Row) -> String): Sequence<Row> {
5050
}
5151

5252

53-
//@Deprecated("use kscript.awk() instead")
53+
//@Deprecated("use kscript.support.awk() instead")
5454
//fun Sequence<String>.splitMap(vararg rules: (Row) -> String, separator: String = "\t", joinWith: String = separator) {
5555
// map { it.split(separator).let { splitLine -> rules.map { it(splitLine) } } }.print()
5656
//}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kscript
1+
package kscript.util
22

33
import org.docopt.Docopt
44
import java.io.File

src/test/kotlin/kscript/examples/AwkComparison.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kscript.examples
22

3-
import kscript.*
43
import kscript.experimental.OneLinerContext
4+
import kscript.text.*
55

66
/**
77
* @author Holger Brandl
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package kscript.examples
22

3-
import kscript.add
4-
import kscript.split
53
import kscript.experimental.OneLinerContext
6-
import kscript.print
4+
import kscript.text.add
5+
import kscript.text.print
6+
import kscript.text.split
77

88
/**
9-
* One-liner kscript example. To ease devlopment simply extend [OneLinerContext] as shown, which will provide
9+
* One-liner kscript example. To ease development simply extend [OneLinerContext] as shown, which will provide
1010
* the same context as `kscript` when running in single line mode.
1111
*
1212
* @author Holger Brandl
@@ -17,7 +17,8 @@ fun main(args: Array<String>) {
1717
object : OneLinerContext(args) {
1818

1919
override fun apply(lines: Sequence<String>) {
20-
lines.split().filter { it[3] == "UA" }.add { it[3] + ":" + it[3] }.print()
20+
lines.split().drop(1).filter { it[9] == "UA" }.add { it[3] + ":" + it[3] }.print()
21+
// kscript 'lines.split().drop(1).filter { it[9] == "UA" }.add { it[3] + ":" + it[3] }.print()'
2122
}
2223
}
2324
}

src/test/kotlin/kscript/test/DocOptTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package kscript.test
22

33
import io.kotlintest.matchers.shouldBe
44
import io.kotlintest.specs.StringSpec
5-
import kscript.DocOpt
5+
import kscript.util.DocOpt
66
import java.io.File
77

88
/**
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
package kscript.test
22

3+
import io.kotlintest.matchers.should
4+
import io.kotlintest.matchers.shouldBe
5+
import io.kotlintest.matchers.shouldEqual
6+
import io.kotlintest.matchers.startWith
37
import io.kotlintest.specs.StringSpec
4-
import kscript.*
8+
import kscript.text.linesFrom
9+
import kscript.text.print
10+
import kscript.text.resolveArgFile
11+
import kscript.text.split
512
import java.io.File
613

714
/**
815
* @author Holger Brandl
916
*/
1017
class SupportApiTest : StringSpec() { init {
1118

12-
"allow to use stdin for filtering and mapping" {
13-
stdin.filter { "^de0[-0]*".toRegex().matches(it) }.map { it + "foo:" }.print()
19+
// "allow to use stdin for filtering and mapping" {
20+
// kscript.text.stdin.filter { "^de0[-0]*".toRegex().matches(it) }.map { it + "foo:" }.print()
21+
// }
22+
23+
"extract field with column filter" {
24+
linesFrom(File("src/test/resources/flights_head.txt")).split().
25+
filter { it[8] == "N14228" }.
26+
map { it[10] }.
27+
toList().
28+
apply {
29+
size shouldEqual 1
30+
first() shouldEqual "EWR"
31+
}
32+
1433
}
1534

16-
"length should return size of string" {
17-
// "hello".length shouldBe 5
18-
// stopIfNot("FOO"=="BAR"){"condition not met"}
19-
println("current dir is " + File(".").absolutePath)
20-
linesFrom(File("src/test/resources/flights_head.txt")).map { it.split("\t")[7] }.print()
21-
linesFrom(File("src/test/resources/flights_head.txt")).filter { it.split("\t")[7] == "UA" }.print()
35+
"compressed lines should be unzipped on the fly"{
36+
resolveArgFile(arrayOf("src/test/resources/flights.tsv.gz")).
37+
drop(1).first() should startWith("2013")
2238
}
2339
}
2440
}

0 commit comments

Comments
 (0)