#lang racket
(require (only-in (planet clements/sxml2) ssax:xml->sxml))
(provide (all-defined-out))
(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 ()
(parameterize ([current-namespace (make-base-namespace)])
(write (compile `',parsed))))
#:exists
'truncate)))))
(define (read-from-zo zo-file)
(parameterize ([current-namespace (make-base-namespace)])
(eval (parameterize ([read-accept-compiled #t])
(file->value zo-file)))))
(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)))
(define (sys2 . a)
(define line (apply string-append a))
(printf "~s\n" line)
(system line))