* add part.scm (attachment handling), plot.scm (plot statistics)

This commit is contained in:
djcb 2012-01-13 00:48:28 +02:00
parent 6732fb46af
commit 7d2f5a2307
2 changed files with 131 additions and 0 deletions

72
guile/mu/part.scm Normal file
View File

@ -0,0 +1,72 @@
;;
;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;;
;; This program is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation; either version 3, or (at your option) any
;; later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
(define-module (mu part)
:use-module (oop goops)
:use-module (mu)
:use-module (mu message)
:export (;; get-part
;; classes
<mu-part>
;; message function
attachments
parts
;; <mu-part> methods
index
name
mime-type
;; size
save
save-as))
(define-class <mu-part> ()
(msgpath #:init-value #f #:init-keyword #:msgpath)
(index #:init-value #f #:init-keyword #:index)
(name #:init-value #f #:getter name #:init-keyword #:name)
(mime-type #:init-value #f #:getter mime-type #:init-keyword #:mime-type)
(size #:init-value 0 #:getter size #:init-keyword #:size))
(define-method (get-parts (msg <mu-message>) (files-only <boolean>))
"Get the part for MSG as a list of <mu-part> objects; if FILES-ONLY is #t,
only get the part with file names."
(map (lambda (part)
(make <mu-part>
#:msgpath (list-ref part 0)
#:index (list-ref part 1)
#:name (list-ref part 2)
#:mime-type (list-ref part 3)
#:size (list-ref part 4)))
(mu:get-parts (slot-ref msg 'msg) files-only)))
(define-method (attachments (msg <mu-message>))
"Get the attachments for MSG as a list of <mu-part> objects."
(get-parts msg #t))
(define-method (parts (msg <mu-message>))
"Get the MIME-parts for MSG as a list of <mu-part> objects."
(get-parts msg #f))
(define-method (save (part <mu-part>))
"Save PART to a temporary file, and return the file name. If the
part had a filename, the temporary file's file name will be just that;
otherwise a name is made up."
(mu:save-part (slot-ref part 'msgpath) (slot-ref part 'index)))
(define-method (save-as (part <mu-part>) (filepath <string>))
"Save message-part PART to file system path PATH."
(copy-file (save part) filepath))

59
guile/mu/plot.scm Normal file
View File

@ -0,0 +1,59 @@
;;
;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;;
;; This program is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the
;; Free Software Foundation; either version 3, or (at your option) any
;; later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
(define-module (mu plot)
:use-module (mu message)
:use-module (ice-9 popen)
:export ( mu:plot-x11
mu:plot-ascii))
(define (export-pairs pairs)
"Write a temporary file with the list of PAIRS in table format, and
return the file name."
(let* ((datafile (tmpnam))
(output (open datafile (logior O_CREAT O_WRONLY) #O0600)))
(for-each
(lambda(pair)
(display (format #f "~a ~a\n" (car pair) (cdr pair)) output))
pairs)
(close output)
datafile))
(define (mu:plot data title x-label y-label want-ascii)
"Plot DATA with TITLE, X-LABEL and X-LABEL. If WANT-ASCII is #t,
output in plain-text; otherwise use an X11 window."
(let ((datafile (export-pairs data))
(gnuplot (open-pipe "gnuplot -p" OPEN_WRITE)))
(display (string-append
"reset\n"
"set term " (if want-ascii "dumb" "x11") "\n"
"set title \"" title "\"\n"
"set xlabel \"" x-label "\"\n"
"set ylabel \"" y-label "\"\n"
"set boxwidth 0.9\n"
"plot \"" datafile "\" using 2:xticlabels(1) with boxes fs solid\n")
gnuplot)
(close-pipe gnuplot)))
(define* (mu:plot-ascii data #:optional (title "Title") (x-label "X") (y-label "Y"))
"Plot DATA with TITLE, X-LABEL and X-LABEL in plain-text."
(mu:plot data title x-label y-label #t))
(define* (mu:plot-x11 data #:optional (title "Title") (x-label "X") (y-label "Y"))
"Plot DATA with TITLE, X-LABEL and X-LABEL in an X11 window."
(mu:plot data title x-label y-label #f))