mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
scalafmtAll
This commit is contained in:
@ -21,7 +21,9 @@ object AccountId {
|
||||
val user = input.substring(n + 1)
|
||||
Ident
|
||||
.fromString(coll)
|
||||
.flatMap(collId => Ident.fromString(user).map(userId => AccountId(collId, userId)))
|
||||
.flatMap(collId =>
|
||||
Ident.fromString(user).map(userId => AccountId(collId, userId))
|
||||
)
|
||||
case _ =>
|
||||
invalid
|
||||
}
|
||||
|
@ -18,7 +18,11 @@ object File {
|
||||
def mkTempDir[F[_]: Sync](parent: Path, prefix: String): F[Path] =
|
||||
mkDir(parent).map(p => Files.createTempDirectory(p, prefix))
|
||||
|
||||
def mkTempFile[F[_]: Sync](parent: Path, prefix: String, suffix: Option[String] = None): F[Path] =
|
||||
def mkTempFile[F[_]: Sync](
|
||||
parent: Path,
|
||||
prefix: String,
|
||||
suffix: Option[String] = None
|
||||
): F[Path] =
|
||||
mkDir(parent).map(p => Files.createTempFile(p, prefix, suffix.orNull))
|
||||
|
||||
def deleteDirectory[F[_]: Sync](dir: Path): F[Int] = Sync[F].delay {
|
||||
@ -26,7 +30,10 @@ object File {
|
||||
Files.walkFileTree(
|
||||
dir,
|
||||
new SimpleFileVisitor[Path]() {
|
||||
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
|
||||
override def visitFile(
|
||||
file: Path,
|
||||
attrs: BasicFileAttributes
|
||||
): FileVisitResult = {
|
||||
Files.deleteIfExists(file)
|
||||
count.incrementAndGet()
|
||||
FileVisitResult.CONTINUE
|
||||
@ -59,11 +66,12 @@ object File {
|
||||
def withTempDir[F[_]: Sync](parent: Path, prefix: String): Resource[F, Path] =
|
||||
Resource.make(mkTempDir(parent, prefix))(p => delete(p).map(_ => ()))
|
||||
|
||||
def listFiles[F[_]: Sync](pred: Path => Boolean, dir: Path): F[List[Path]] = Sync[F].delay {
|
||||
val javaList =
|
||||
Files.list(dir).filter(p => pred(p)).collect(java.util.stream.Collectors.toList())
|
||||
javaList.asScala.toList.sortBy(_.getFileName.toString)
|
||||
}
|
||||
def listFiles[F[_]: Sync](pred: Path => Boolean, dir: Path): F[List[Path]] =
|
||||
Sync[F].delay {
|
||||
val javaList =
|
||||
Files.list(dir).filter(p => pred(p)).collect(java.util.stream.Collectors.toList())
|
||||
javaList.asScala.toList.sortBy(_.getFileName.toString)
|
||||
}
|
||||
|
||||
def readAll[F[_]: Sync: ContextShift](
|
||||
file: Path,
|
||||
|
@ -31,7 +31,8 @@ object JobState {
|
||||
/** Finished with success */
|
||||
case object Success extends JobState {}
|
||||
|
||||
val all: Set[JobState] = Set(Waiting, Scheduled, Running, Stuck, Failed, Cancelled, Success)
|
||||
val all: Set[JobState] =
|
||||
Set(Waiting, Scheduled, Running, Stuck, Failed, Cancelled, Success)
|
||||
val queued: Set[JobState] = Set(Waiting, Scheduled, Stuck)
|
||||
val done: Set[JobState] = Set(Failed, Cancelled, Success)
|
||||
|
||||
|
@ -40,7 +40,9 @@ case class LenientUri(
|
||||
withQueryPlain(name, URLEncoder.encode(value, "UTF-8"))
|
||||
|
||||
def withQueryPlain(name: String, value: String): LenientUri =
|
||||
copy(query = query.map(q => q + "&" + name + "=" + value).orElse(Option(s"$name=$value")))
|
||||
copy(query =
|
||||
query.map(q => q + "&" + name + "=" + value).orElse(Option(s"$name=$value"))
|
||||
)
|
||||
|
||||
def withFragment(f: String): LenientUri =
|
||||
copy(fragment = Some(f))
|
||||
@ -56,7 +58,10 @@ case class LenientUri(
|
||||
)
|
||||
}
|
||||
|
||||
def readURL[F[_]: Sync: ContextShift](chunkSize: Int, blocker: Blocker): Stream[F, Byte] =
|
||||
def readURL[F[_]: Sync: ContextShift](
|
||||
chunkSize: Int,
|
||||
blocker: Blocker
|
||||
): Stream[F, Byte] =
|
||||
Stream
|
||||
.emit(Either.catchNonFatal(new URL(asString)))
|
||||
.covary[F]
|
||||
@ -135,7 +140,8 @@ object LenientUri {
|
||||
case "/" => RootPath
|
||||
case "" => EmptyPath
|
||||
case _ =>
|
||||
NonEmptyList.fromList(stripLeading(str, '/').split('/').toList.map(percentDecode)) match {
|
||||
NonEmptyList
|
||||
.fromList(stripLeading(str, '/').split('/').toList.map(percentDecode)) match {
|
||||
case Some(nl) => NonEmptyPath(nl)
|
||||
case None => sys.error(s"Invalid url: $str")
|
||||
}
|
||||
|
@ -56,25 +56,32 @@ object MimeType {
|
||||
def parsePrimary: Either[String, (String, String)] =
|
||||
str.indexOf('/') match {
|
||||
case -1 => Left(s"Invalid mediatype: $str")
|
||||
case n => Right(str.take(n) -> str.drop(n + 1))
|
||||
case n => Right(str.take(n) -> str.drop(n + 1))
|
||||
}
|
||||
|
||||
def parseSub(s: String): Either[String, (String, String)] =
|
||||
s.indexOf(';') match {
|
||||
case -1 => Right((s, ""))
|
||||
case n => Right((s.take(n), s.drop(n)))
|
||||
case n => Right((s.take(n), s.drop(n)))
|
||||
}
|
||||
|
||||
def parseParams(s: String): Map[String, String] =
|
||||
s.split(';').map(_.trim).filter(_.nonEmpty).toList.flatMap(p => p.split("=", 2).toList match {
|
||||
case a :: b :: Nil => Some((a, b))
|
||||
case _ => None
|
||||
}).toMap
|
||||
s.split(';')
|
||||
.map(_.trim)
|
||||
.filter(_.nonEmpty)
|
||||
.toList
|
||||
.flatMap(p =>
|
||||
p.split("=", 2).toList match {
|
||||
case a :: b :: Nil => Some((a, b))
|
||||
case _ => None
|
||||
}
|
||||
)
|
||||
.toMap
|
||||
|
||||
for {
|
||||
pt <- parsePrimary
|
||||
st <- parseSub(pt._2)
|
||||
pa = parseParams(st._2)
|
||||
pa = parseParams(st._2)
|
||||
} yield MimeType(pt._1, st._1, pa)
|
||||
}
|
||||
|
||||
|
@ -47,14 +47,17 @@ object SystemCommand {
|
||||
for {
|
||||
_ <- writeToProcess(stdin, proc, blocker)
|
||||
term <- Sync[F].delay(proc.waitFor(cmd.timeout.seconds, TimeUnit.SECONDS))
|
||||
_ <- if (term) logger.debug(s"Command `${cmd.cmdString}` finished: ${proc.exitValue}")
|
||||
_ <- if (term)
|
||||
logger.debug(s"Command `${cmd.cmdString}` finished: ${proc.exitValue}")
|
||||
else
|
||||
logger.warn(
|
||||
s"Command `${cmd.cmdString}` did not finish in ${cmd.timeout.formatExact}!"
|
||||
)
|
||||
_ <- if (!term) timeoutError(proc, cmd) else Sync[F].pure(())
|
||||
out <- if (term) inputStreamToString(proc.getInputStream, blocker) else Sync[F].pure("")
|
||||
err <- if (term) inputStreamToString(proc.getErrorStream, blocker) else Sync[F].pure("")
|
||||
_ <- if (!term) timeoutError(proc, cmd) else Sync[F].pure(())
|
||||
out <- if (term) inputStreamToString(proc.getInputStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
err <- if (term) inputStreamToString(proc.getErrorStream, blocker)
|
||||
else Sync[F].pure("")
|
||||
} yield Result(proc.exitValue, out, err)
|
||||
}
|
||||
}
|
||||
@ -122,12 +125,17 @@ object SystemCommand {
|
||||
proc: Process,
|
||||
blocker: Blocker
|
||||
): F[Unit] =
|
||||
data.through(io.writeOutputStream(Sync[F].delay(proc.getOutputStream), blocker)).compile.drain
|
||||
data
|
||||
.through(io.writeOutputStream(Sync[F].delay(proc.getOutputStream), blocker))
|
||||
.compile
|
||||
.drain
|
||||
|
||||
private def timeoutError[F[_]: Sync](proc: Process, cmd: Config): F[Unit] =
|
||||
Sync[F].delay(proc.destroyForcibly()).attempt *> {
|
||||
Sync[F].raiseError(
|
||||
new Exception(s"Command `${cmd.cmdString}` timed out (${cmd.timeout.formatExact})")
|
||||
new Exception(
|
||||
s"Command `${cmd.cmdString}` timed out (${cmd.timeout.formatExact})"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -24,11 +24,18 @@ object ThreadFactories {
|
||||
): Resource[F, ExecutionContextExecutorService] =
|
||||
Resource.make(Sync[F].delay(c))(ec => Sync[F].delay(ec.shutdown))
|
||||
|
||||
def cached[F[_]: Sync](tf: ThreadFactory): Resource[F, ExecutionContextExecutorService] =
|
||||
def cached[F[_]: Sync](
|
||||
tf: ThreadFactory
|
||||
): Resource[F, ExecutionContextExecutorService] =
|
||||
executorResource(
|
||||
ExecutionContext.fromExecutorService(Executors.newCachedThreadPool(tf))
|
||||
)
|
||||
|
||||
def fixed[F[_]: Sync](n: Int, tf: ThreadFactory): Resource[F, ExecutionContextExecutorService] =
|
||||
executorResource(ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(n, tf)))
|
||||
def fixed[F[_]: Sync](
|
||||
n: Int,
|
||||
tf: ThreadFactory
|
||||
): Resource[F, ExecutionContextExecutorService] =
|
||||
executorResource(
|
||||
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(n, tf))
|
||||
)
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ case class Timestamp(value: Instant) {
|
||||
def minus(d: Duration): Timestamp =
|
||||
Timestamp(value.minusNanos(d.nanos))
|
||||
|
||||
def - (d: Duration): Timestamp =
|
||||
def -(d: Duration): Timestamp =
|
||||
minus(d)
|
||||
|
||||
def minusHours(n: Long): Timestamp =
|
||||
@ -35,7 +35,7 @@ case class Timestamp(value: Instant) {
|
||||
|
||||
def asString: String = value.toString
|
||||
|
||||
def < (other: Timestamp): Boolean =
|
||||
def <(other: Timestamp): Boolean =
|
||||
this.value.isBefore(other.value)
|
||||
}
|
||||
|
||||
|
@ -26,17 +26,25 @@ object Implicits {
|
||||
|
||||
implicit val byteVectorReader: ConfigReader[ByteVector] =
|
||||
ConfigReader[String].emap(reason { str =>
|
||||
if (str.startsWith("hex:")) ByteVector.fromHex(str.drop(4)).toRight("Invalid hex value.")
|
||||
if (str.startsWith("hex:"))
|
||||
ByteVector.fromHex(str.drop(4)).toRight("Invalid hex value.")
|
||||
else if (str.startsWith("b64:"))
|
||||
ByteVector.fromBase64(str.drop(4)).toRight("Invalid Base64 string.")
|
||||
else ByteVector.encodeUtf8(str).left.map(ex => s"Invalid utf8 string: ${ex.getMessage}")
|
||||
else
|
||||
ByteVector
|
||||
.encodeUtf8(str)
|
||||
.left
|
||||
.map(ex => s"Invalid utf8 string: ${ex.getMessage}")
|
||||
})
|
||||
|
||||
implicit val caleventReader: ConfigReader[CalEvent] =
|
||||
ConfigReader[String].emap(reason(CalEvent.parse))
|
||||
|
||||
|
||||
def reason[A: ClassTag](f: String => Either[String, A]): String => Either[FailureReason, A] =
|
||||
def reason[A: ClassTag](
|
||||
f: String => Either[String, A]
|
||||
): String => Either[FailureReason, A] =
|
||||
in =>
|
||||
f(in).left.map(str => CannotConvert(in, implicitly[ClassTag[A]].runtimeClass.toString, str))
|
||||
f(in).left.map(str =>
|
||||
CannotConvert(in, implicitly[ClassTag[A]].runtimeClass.toString, str)
|
||||
)
|
||||
}
|
||||
|
@ -33,13 +33,21 @@ object NerLabelSpanTest extends SimpleTestSuite {
|
||||
)
|
||||
|
||||
val spans = NerLabelSpan.build(labels)
|
||||
assertEquals(spans, Vector(
|
||||
NerLabel("Derek Jeter", NerTag.Person, 0, 11),
|
||||
NerLabel("Derek Jeter", NerTag.Person, 68, 79),
|
||||
NerLabel("Syrup Production Old Sticky Pancake Company", NerTag.Organization, 162, 205),
|
||||
NerLabel("Maple Lane", NerTag.Location, 210, 220),
|
||||
NerLabel("Little League", NerTag.Organization, 351, 364),
|
||||
NerLabel("Derek Jeter", NerTag.Person, 1121, 1132)
|
||||
))
|
||||
assertEquals(
|
||||
spans,
|
||||
Vector(
|
||||
NerLabel("Derek Jeter", NerTag.Person, 0, 11),
|
||||
NerLabel("Derek Jeter", NerTag.Person, 68, 79),
|
||||
NerLabel(
|
||||
"Syrup Production Old Sticky Pancake Company",
|
||||
NerTag.Organization,
|
||||
162,
|
||||
205
|
||||
),
|
||||
NerLabel("Maple Lane", NerTag.Location, 210, 220),
|
||||
NerLabel("Little League", NerTag.Organization, 351, 364),
|
||||
NerLabel("Derek Jeter", NerTag.Person, 1121, 1132)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user