(module parse mzscheme
(require (only (planet "ssax.ss" ("lizorkin" "ssax.plt" 1 3)) ssax:xml->sxml)
"shelly.ss"
(lib "match.ss"))
(provide (all-defined))
(define (cache-as-zo input-file output-file)
(sys2 "cp "(path->string input-file)" /tmp/gnucash-expanded.gz")
(when (file-exists? "/tmp/gnucash-expanded")
(delete-file "/tmp/gnucash-expanded"))
(sys2 "gunzip /tmp/gnucash-expanded.gz")
(with-input-from-file "/tmp/gnucash-expanded"
(lambda ()
(let* ([parsed (ssax:xml->sxml (current-input-port) '())])
(with-output-to-file output-file
(lambda ()
(write (compile `',parsed)))
'truncate)))))
(define (read-from-zo zo-file)
(eval (parameterize ([read-accept-compiled #t])
(call-with-input-file zo-file read))))
(define (strip-top-level-goo xml-elt)
(match xml-elt
[`(*TOP* ,top-attribs (gnc-v2 ,count-data (,book-tag (@ . ,book-attribs) . ,content)))
content]))
(define (gnucash-read gnucash-file gnucash-cache-file)
(when (or (not (file-exists? gnucash-cache-file))
(< (file-or-directory-modify-seconds gnucash-cache-file)
(file-or-directory-modify-seconds gnucash-file)))
(cache-as-zo gnucash-file gnucash-cache-file))
(strip-top-level-goo (read-from-zo gnucash-cache-file)))
)