Define view functions separately from classes

Using `lambda' the way it was being used is not pretty.
This commit is contained in:
Tom Willemse 2013-12-26 01:05:37 +01:00
parent 9788df48a9
commit 8e599478f7

111
hypo.hy
View file

@ -65,29 +65,25 @@ If no lexer is found fallback onto the text lexer."
(catch [ClassNotFound] (catch [ClassNotFound]
(get-lexer-by-name "text")))) (get-lexer-by-name "text"))))
(defclass raw [] (defun get-raw [self name]
[[GET (lambda [self name] (let ((dirname (+ "files/" (os.path.dirname name)))
(let ((dirname (+ "files/" (os.path.dirname name))) (repo (and (os.path.exists dirname)
(repo (and (os.path.exists dirname) (Gittle dirname)))
(Gittle dirname))) (resp (if repo
(resp (if repo (get (.commit-file repo "HEAD" (os.path.basename name))
(get (.commit-file "data"))))
repo "HEAD" (os.path.basename name)) (or resp (no-such-file))))
"data"))))
(or resp (no-such-file))))]])
(defclass download [] (defun get-attachment [self name]
[[GET (lambda [self name] (let ((dirname (+ "files/" (os.path.dirname name)))
(let ((dirname (+ "files/" (os.path.dirname name))) (repo (and (os.path.exists dirname)
(repo (and (os.path.exists dirname) (Gittle dirname))))
(Gittle dirname)))) (if repo
(if repo (progn
(progn (web.header "Content-Disposition"
(web.header "Content-Disposition" (+ "attachment; filename=\"" name "\""))
(+ "attachment; filename=\"" name "\"")) (get (.commit-file repo "HEAD" (os.path.basename name)) "data"))
(get (.commit-file repo "HEAD" (no-such-file))))
(os.path.basename name)) "data"))
(no-such-file))))]])
(defun render-file [hash repo ref filename] (defun render-file [hash repo ref filename]
(if (not (os.path.isdir filename)) (if (not (os.path.isdir filename))
@ -107,39 +103,50 @@ If no lexer is found fallback onto the text lexer."
(kwapply (render.main) args)) (kwapply (render.main) args))
"")) ""))
(defclass html [] (defun get-html [self name]
[[GET (lambda [self name] (let ((dirname (+ "files/" name))
(let ((dirname (+ "files/" name)) (repo (and (os.path.exists dirname)
(repo (and (os.path.exists dirname) (Gittle dirname))))
(Gittle dirname)))) (if repo
(if repo (car (list-comp (render-file name repo "HEAD" f)
(car (list-comp (render-file name repo "HEAD" f) [f (.iterkeys (.commit-tree repo "HEAD"))]
[f (.iterkeys (.commit-tree repo "HEAD"))] (not (or (= f ".")
(not (or (= f ".") (= f "..")))))
(= f ".."))))) (no-such-file))))
(no-such-file))))]
[DELETE (lambda [self name] (defun delete-dir [self name]
(let ((dirname (+ "files/" name))) (let ((dirname (+ "files/" name)))
(if (os.path.exists dirname) (if (os.path.exists dirname)
(shutil.rmtree dirname) (shutil.rmtree dirname)
(no-such-file))))]]) (no-such-file))))
(defun upload-file [self name]
(let ((h (hashes name))
(dirname (+ "files/" (get h 0))))
(os.mkdir dirname)
(with [f (file (+ dirname "/" name) "w")]
(.write f (web.data)))
(let ((repo (Gittle.init dirname)))
(.stage repo [(str name)])
(kwapply (repo.commit)
{"name" "Hypo"
"email" "hypo@ryuslash.org"
"message" "Initial commit"}))
(setv web.ctx.status (str "201 Created"))
(+ web.ctx.home "/" *prefix* (get h 0) "\n")))
(defclass raw []
[[GET get-raw]])
(defclass download []
[[GET get-attachment]])
(defclass html []
[[GET get-html]
[DELETE delete-dir]])
(defclass upload [] (defclass upload []
[[PUT (lambda [self name] [[PUT upload-file]])
(let ((h (hashes name))
(dirname (+ "files/" (get h 0))))
(os.mkdir dirname)
(with [f (file (+ dirname "/" name) "w")]
(.write f (web.data)))
(let ((repo (Gittle.init dirname)))
(.stage repo [(str name)])
(kwapply (repo.commit)
{"name" "Hypo"
"email" "hypo@ryuslash.org"
"message" "Initial commit"}))
(setv web.ctx.status (str "201 Created"))
(+ web.ctx.home "/" *prefix* (get h 0) "\n")))]])
(defclass index [] (defclass index []
[[GET (lambda [self] (render.index))]]) [[GET (lambda [self] (render.index))]])