2011/11/01

最近の雑多なコマンド(gauche)

現実逃避駆動スクリプティング。使い捨てでいいんだけど、あえてコマンドにしました的な。

jsonとalistを相互に変換。
$ echo '(("a" . 1)("b" . 2)("c" . (("d" . #(1 2 3)))))' | json2alist -a 
{"a":1,"b":2,"c":{"d":[1,2,3]}}
$ echo '(("a" . 1)("b" . 2)("c" . (("d" . #(1 2 3)))))' | json2alist -a | json2alist
(("a" . 1) ("b" . 2) ("c" ("d" . #(1 2 3))))  
#!/usr/local/bin/gosh
(use rfc.json)
(use gauche.parseopt)
(define (usage)
(print "j2a [a|alist j|json h|help]")
(exit 0))
(define (main args)
(let-args (cdr args)
((alist "a|alist")
(json "j|json" #t)
(help "h|help" => usage)
. rest-args)
(let ((arg (and (not (null? rest-args))
(car rest-args))))
(write (cond (alist (or (and arg (construct-json-string (read-from-string arg)))
(construct-json (read))))
(json (or (and arg (parse-json-string arg))
(parse-json)))
(else (usage)))))))
view raw json2alist.scm hosted with ❤ by GitHub


csvをjsonに。csvをtemplate engineに渡すときにかましたり。
$ echo '1,2,3\n4,5,6\n7,8,9'| csv2json
"{\"title\":\"\",\"lines\":3,\"body\":[[\"1\",\"2\",\"3\"],[\"4\",\"5\",\"6\"],[\"7\",\"8\",\"9\"]]}"    
#!/usr/bin/env gosh
(use rfc.json)
(use text.csv)
(use file.util)
(use util.list)
(use gauche.parseopt)
(define (usage)
(print "usage: sv2j [options] <csv>")
(print "d|delimiter" " - default : ,")
(print "f|file" " - file path")
(print "a|alist" " - to alist")
(print "t|title" " - have a title row")
(print "l|limit")
(print "o|offset")
(print "h|help")
(exit 0))
(define (input->csv delim file)
(port->list (make-csv-reader delim)
(if file
(open-input-file file)
(standard-input-port))))
(define (cut-csv csv limit offset)
(define (cut csv f1 f2 n)
(if n
((if (and n (positive? n)) f1 f2) csv (abs n))
csv))
(cut (cut csv drop* drop-right* offset)
take* take-right* limit))
(define (csv->alist csv title limit offset)
(let* ((title (and title (list->vector (car csv))))
(body (list->vector
(map list->vector
(cut-csv (if title (cdr csv) csv)
limit offset)))))
(list (cons "title" (or title ""))
(cons "lines" (vector-length body))
(cons "body" body))))
(define (main args)
(let-args (cdr args)
((delimiter "d|delimiter=s" ",")
(file "f|file=s")
(alist "a|alist")
(title "t|title")
(limit "l|limit=i")
(offset "o|offset=i")
(help "h|help" => usage)
. rest)
(let ((limit (and limit (x->integer limit)))
(offset (and offset (x->integer offset))))
(let* ((csv (input->csv (string-ref delimiter 0) file))
(ls (csv->alist csv title limit offset)))
(write (if alist
ls
(construct-json-string ls)))))))
view raw csv2json.scm hosted with ❤ by GitHub


jsonからkeyを取得したり、指定したkeyの値を取得したり。
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -k                                           
a                                                                                                  
b                                                                                                  
c                                                                                                  
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -t                                            
object                                                                                             
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -l                                                
3                                                                                                  
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -e c                                              
{"d":[1,2,3]}                                                                                   
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -e c | pryon -e d                                   
[1,2,3]                                                                                         
$ echo '{"a":1,"b":2,"c":{"d":[1,2,3]}}' | pryon -e c | pryon -e d | pryon -i 2                      
3

#!/usr/local/bin/gosh
(use rfc.json) ; parse-json
(use util.list) ; assoc-ref
(use gauche.parseopt) ; let-args
(define (usage)
(print "usage: gson [-t | -l | -k | -e | -i] <json>")
(print " -t | type")
(print " -l | length")
(print " -k | keys")
(print " -e | extract")
(print " -i | index")
(print " -h | help")
(exit 0))
;;
;; print type
;;
(define-method print-type ((ls <list>))
(print "object"))
(define-method print-type ((vect <vector>))
(print "vector"))
;;
;; print keys
;;
(define (print-keys json)
(for-each (compose print car) json))
;;
;; print length
;;
(define-method print-length ((ls <list>))
(print (length ls)))
(define-method print-length ((vect <vector>))
(print (vector-length vect)))
(define-method print-length ((str <string>))
(print (string-length str)))
;;
;; extract
;;
(define (print-extracted-json json key)
(print-filtered-value assoc-ref json key))
;;
;; index
;;
(define (print-index-value json i)
(print-filtered-value vector-ref json i))
;;
;; util
;;
(define (print-filtered-value filter json . args)
(let1 val (apply filter json args)
(if (or (list? val)
(vector? val))
(construct-json val)
(write val))))
;;
;; main
;;
(define (main args)
(let-args (cdr args)
((-type "t|type")
(-length "l|length")
(-keys "k|keys")
(-extract "e|extract=s")
(-index "i|index=i")
(-help "h|help" => usage)
. rest)
(let ((json (parse-json (current-input-port))))
(cond (-type (print-type json))
(-keys (print-keys json))
(-length (print-length json))
(-extract (print-extracted-json json -extract))
(-index (print-index-value json -index))
(else (exit 0))))))
view raw pryon.scm hosted with ❤ by GitHub
最後のやつは、jshonってののライト版的ノリで。ちょっと便利。というかjshonがうまいこと動いてくれなかったので仕方なく。

プログラミングGauche

0 件のコメント:

コメントを投稿