aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2013-12-26 01:05:37 +0100
committerGravatar Tom Willemse2013-12-26 01:05:37 +0100
commit8e599478f7420e8e585cb01037dc776332cc02b6 (patch)
tree8f138927febe24a9bcd41648fb8c993586b41dd3
parent9788df48a9638c6bf37bc161abefcee7f583b64c (diff)
downloadhypo-8e599478f7420e8e585cb01037dc776332cc02b6.tar.gz
hypo-8e599478f7420e8e585cb01037dc776332cc02b6.zip
Define view functions separately from classes
Using `lambda' the way it was being used is not pretty.
-rwxr-xr-xhypo.hy113
1 files changed, 60 insertions, 53 deletions
diff --git a/hypo.hy b/hypo.hy
index b6026df..4e9dcb1 100755
--- a/hypo.hy
+++ b/hypo.hy
@@ -65,29 +65,25 @@ If no lexer is found fallback onto the text lexer."
(catch [ClassNotFound]
(get-lexer-by-name "text"))))
-(defclass raw []
- [[GET (lambda [self name]
- (let ((dirname (+ "files/" (os.path.dirname name)))
- (repo (and (os.path.exists dirname)
- (Gittle dirname)))
- (resp (if repo
- (get (.commit-file
- repo "HEAD" (os.path.basename name))
- "data"))))
- (or resp (no-such-file))))]])
-
-(defclass download []
- [[GET (lambda [self name]
- (let ((dirname (+ "files/" (os.path.dirname name)))
- (repo (and (os.path.exists dirname)
- (Gittle dirname))))
- (if repo
- (progn
- (web.header "Content-Disposition"
- (+ "attachment; filename=\"" name "\""))
- (get (.commit-file repo "HEAD"
- (os.path.basename name)) "data"))
- (no-such-file))))]])
+(defun get-raw [self name]
+ (let ((dirname (+ "files/" (os.path.dirname name)))
+ (repo (and (os.path.exists dirname)
+ (Gittle dirname)))
+ (resp (if repo
+ (get (.commit-file repo "HEAD" (os.path.basename name))
+ "data"))))
+ (or resp (no-such-file))))
+
+(defun get-attachment [self name]
+ (let ((dirname (+ "files/" (os.path.dirname name)))
+ (repo (and (os.path.exists dirname)
+ (Gittle dirname))))
+ (if repo
+ (progn
+ (web.header "Content-Disposition"
+ (+ "attachment; filename=\"" name "\""))
+ (get (.commit-file repo "HEAD" (os.path.basename name)) "data"))
+ (no-such-file))))
(defun render-file [hash repo ref 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))
""))
+(defun get-html [self name]
+ (let ((dirname (+ "files/" name))
+ (repo (and (os.path.exists dirname)
+ (Gittle dirname))))
+ (if repo
+ (car (list-comp (render-file name repo "HEAD" f)
+ [f (.iterkeys (.commit-tree repo "HEAD"))]
+ (not (or (= f ".")
+ (= f "..")))))
+ (no-such-file))))
+
+(defun delete-dir [self name]
+ (let ((dirname (+ "files/" name)))
+ (if (os.path.exists dirname)
+ (shutil.rmtree dirname)
+ (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 (lambda [self name]
- (let ((dirname (+ "files/" name))
- (repo (and (os.path.exists dirname)
- (Gittle dirname))))
- (if repo
- (car (list-comp (render-file name repo "HEAD" f)
- [f (.iterkeys (.commit-tree repo "HEAD"))]
- (not (or (= f ".")
- (= f "..")))))
- (no-such-file))))]
-
- [DELETE (lambda [self name]
- (let ((dirname (+ "files/" name)))
- (if (os.path.exists dirname)
- (shutil.rmtree dirname)
- (no-such-file))))]])
+ [[GET get-html]
+ [DELETE delete-dir]])
(defclass upload []
- [[PUT (lambda [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")))]])
+ [[PUT upload-file]])
(defclass index []
[[GET (lambda [self] (render.index))]])