Make file loading a bit more robust

Don't fail when a file can't be found.
This commit is contained in:
Tom Willemsen 2012-09-16 14:09:56 +02:00
parent 86f48d9fa4
commit cae98ebdfb

View file

@ -76,28 +76,38 @@
where #t where #t
sort-by #f nil))))) sort-by #f nil)))))
(define (stored-file xdg-env file)
"Try to get FILE stored either in XDG-ENV or the home directory."
(let ((xdg (getenv xdg-env))
(home (getenv "HOME"))
(result #f))
(cond
(xdg
(let ((xdg-stored-file (string-append xdg "/undone/" file)))
(when (file-exists? xdg-stored-file)
(set! result xdg-stored-file))))
(home
(let ((home-stored-file (string-append home "/.undone/" file)))
(when (file-exists? home-stored-file)
(set! result home-stored-file)))))
result))
(define (config-file file)
"Try to get FILE from the configuration file location."
(stored-file "XDG_CONFIG_HOME" file))
(define (data-file file)
"Try to get FILE from the data file location."
(stored-file "XDG_DATA_HOME" file))
(define todo-list-file (define todo-list-file
(let* ((xdg (getenv "XDG_DATA_HOME")) (data-file "todo.scm"))
(home (getenv "HOME"))
(todo-file-name "todo.scm")
(xdg-todo-file (string-append xdg "/undone/" todo-file-name))
(home-todo-file (string-append xdg "/.undone/" todo-file-name)))
(if (and xdg (not (file-exists? home-todo-file)))
xdg-todo-file
home-todo-file)))
(define rc-file (define rc-file
(let* ((xdg (getenv "XDG_CONFIG_HOME")) (config-file "undonerc.scm"))
(home (getenv "HOME"))
(rc-file-name "undonerc.scm")
(xdg-rc-file (string-append xdg "/undone/" rc-file-name))
(home-rc-file (string-append home "/.undone/" rc-file-name)))
(if (and xdg (file-exists? xdg-rc-file))
xdg-rc-file
home-rc-file)))
(define todo-list (define todo-list
(if (file-exists? todo-list-file) (if (and todo-list-file (file-exists? todo-list-file))
(let* ((port (open-input-file todo-list-file)) (let* ((port (open-input-file todo-list-file))
(result (read port))) (result (read port)))
(close-port port) (close-port port)
@ -106,22 +116,6 @@
(define view-list '()) (define view-list '())
(define (data-dir)
"Get the location for data files."
(let ((xdg (getenv "XDG_DATA_HOME")))
(string-append (if xdg
(string-append xdg "/")
(string-append (getenv "HOME") "/."))
"undone")))
(define (config-dir)
"Get the location for configuration files."
(let ((xdg (getenv "XDG_CONFIG_HOME")))
(string-append (if xdg
(string-append xdg "/")
(string-append (getenv "HOME") "/."))
"undone")))
(define (mkdirs path) (define (mkdirs path)
(let ((parent (dirname path))) (let ((parent (dirname path)))
(if (not (file-exists? parent)) (if (not (file-exists? parent))
@ -131,7 +125,6 @@
(mkdir path))) (mkdir path)))
(define (save) (define (save)
"Save the list." "Save the list."
(mkdirs (dirname todo-list-file)) (mkdirs (dirname todo-list-file))
@ -178,12 +171,17 @@
(if (procedure? view-func) (if (procedure? view-func)
(apply view-func '())))) (apply view-func '()))))
(define (main args) (define (load-rc-file)
;; Ugly hack, can't think of a better way right now... "Load the RC file containing custom views and such."
;; Ugly hack, can't thinkg of a better way right now, though...
(save-module-excursion (save-module-excursion
(lambda () (lambda ()
(set-current-module (resolve-module '(undone main))) (set-current-module (resolve-module '(undone main)))
(load rc-file))) (load rc-file))))
(define (main args)
(when (and rc-file (file-exists? rc-file))
(load-rc-file))
(if (> (length args) 1) (if (> (length args) 1)
(case (string->symbol (cadr args)) (case (string->symbol (cadr args))