[Assorted-commits] SF.net SVN: assorted: [626] zdb/trunk
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-03-13 21:21:22
|
Revision: 626 http://assorted.svn.sourceforge.net/assorted/?rev=626&view=rev Author: yangzhang Date: 2008-03-13 14:21:27 -0700 (Thu, 13 Mar 2008) Log Message: ----------- added a basic, minimal console ui Modified Paths: -------------- zdb/trunk/run.bash zdb/trunk/src/zdb/AltUi.scala Modified: zdb/trunk/run.bash =================================================================== --- zdb/trunk/run.bash 2008-03-13 21:21:15 UTC (rev 625) +++ zdb/trunk/run.bash 2008-03-13 21:21:27 UTC (rev 626) @@ -16,14 +16,16 @@ then echo "specify 'web' or 'file'"; exit 1 fi -case "$1" in +cmd="$1" +shift +case "$cmd" in build ) fsc -deprecation -d build \ src/zdb/Core.scala \ src/zdb/AltUi.scala \ $COMMONS/commons/*.scala \ $COMMONS/commons/extras/*.scala ;; web ) run scala zdb.WebUi ;; - file ) run scala zdb.FileIo ;; + file ) run rlwrap scala zdb.FileIo "$@" ;; repl ) run rlwrap scala ;; - * ) run scala "$@" ;; + * ) run scala "$1" "$@" ;; esac Modified: zdb/trunk/src/zdb/AltUi.scala =================================================================== --- zdb/trunk/src/zdb/AltUi.scala 2008-03-13 21:21:15 UTC (rev 625) +++ zdb/trunk/src/zdb/AltUi.scala 2008-03-13 21:21:27 UTC (rev 626) @@ -4,24 +4,112 @@ import ZdbUtil._ import ObjUser._ +import commons.Collections._ import commons.Control._ import commons.Io._ import commons.Misc._ import commons.extras._ -import java.io._ +// import scala.collection.jcl._ +import scala.io._ -import scala.collection.jcl._ +/** Command-line interface. */ +object FileIo { -object FileIo { - def main(args: Array[String]) { - loadFilePickle("data/data") - using (TextWriter("output")) { w => - for (obj <- universe.objs) { - w println printSummObj(obj) + /** Find objects such that at least one of its names contains the query. */ + def find(query: String) = { + val results = for ( + obj <- universe.objs + if obj.vals("names").asInstanceOf[StrSetV].s exists (_ contains query) + ) yield { + print(obj) + } + results mkString "\n" + } + /** + * Read a block of input, terminated by a line consisting solely of a + * period. + */ + def readBlock: List[String] = { + val line = Console.readLine + if (line == null || line == ".") Nil + else line :: readBlock + } + /** Read a block and parse it as a struct. */ + def readStruct = { + val lines = readBlock + val pairs = for (line <- lines) yield { + val Seq(key, valstr) = line split (": ", 2) + val vals = valstr split ";" map (_ trim) + (key, vals) + } + val map = Map(pairs: _*) + if (map.size != pairs.length) throw new Exception("duplicate key") + map + } + /** Update by struct, either adding or updating an object. */ + def updateStruct(shouldExist: Boolean) = { + val map = readStruct + val obj = + if (shouldExist) { + deref(getObj(map("names")(0))) + } else { + try { + getObj(map("names")(0)) + throw new Exception("object of that name already exists") + } catch { + case e: Exception => mkObj(map("parents") map getObj) + } } + for ((key, vals) <- map; if key != "parents") { + obj.infoProp(key).typ match { + case _: ObjSetT => obj.setProp(key, mkObjSetV(vals map getObj)) + case _: StrSetT => obj.setProp(key, mkStrSetV(vals)) + } } + validateAll } + /** Show an object in struct format. */ + def show(o: Obj) = { + val lines = for ((key, vals) <- o.vals) yield { + val valstr = vals match { + case StrSetV(s) => s mkString "; " + case ObjSetV(s) => s map deref map nameAndId mkString "; " + } + key + ": " + valstr + } + lines mkString "\n" + } + + def main(argv: Array[String]) { + while (true) { + Console print ">>> " + val line = Console.readLine + if (line == null) return + val ws = words(line.trim) + val args = ws drop 1 + ws(0) match { + case "load-full" => loadFilePickle("data/data") + case "load" => load + case "validate" => validateAll + case "save-full" => { validateAll; saveFilePickle(ws(1)) } + case "save" => { validateAll; save } + case "find" => println(find(args mkString " ")) + case "get-full" => args match { + case Seq(name) => println(print(deref(getObj(name)))) + case Seq(name, refine) => println(print(deref(getObj(name, refine)))) + } + case "get" => args match { + case Seq(name) => println(show(deref(getObj(name)))) + case Seq(name, refine) => println(show(deref(getObj(name, refine)))) + } + case "add" => updateStruct(false) + case "update" => updateStruct(true) + case "" => () + case x => println("unknown command: " + x) + } + } + } } object WebUi { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |