From d495473c541b6152542544a017ea4fe3b572f9a8 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sat, 21 Sep 2013 02:08:10 +0200 Subject: [PATCH 1/8] Translation of meth from Python to Hy --- hy/contrib/meth.hy | 54 ++++++++++++++++++++++++++++++++++++++++++ hy/contrib/meth.py | 58 ---------------------------------------------- 2 files changed, 54 insertions(+), 58 deletions(-) create mode 100644 hy/contrib/meth.hy delete mode 100644 hy/contrib/meth.py diff --git a/hy/contrib/meth.hy b/hy/contrib/meth.hy new file mode 100644 index 0000000..ca3f739 --- /dev/null +++ b/hy/contrib/meth.hy @@ -0,0 +1,54 @@ +;;; Meth +;; based on paultag's meth library to access a Flask based application + +(defmacro route [name path params code] + "Default get request" + (quasiquote (let [[deco ((getattr app "route") (unquote path))]] + (with-decorator deco + (defn (unquote name) (unquote params) (unquote-splice code)))))) + +(defmacro route-with-methods [name path params code methods] + "Same as route but with an extra methods array to specify HTTP methods" + (quasiquote (let [[deco (kwapply ((getattr app "route") (unquote path)) + {"methods" (unquote methods)})]] + (with-decorator deco + (defn (unquote name) (unquote params) (unquote-splice code)))))) + +;; Some macro examples +(defmacro post-route [name path params code] + "Post request" + `(route-with-methods ~name ~path ~params ~code ["POST"])) + +(defmacro put-route [name path params code] + "Put request" + `(route-with-methods ~name ~path ~params ~code ["PUT"])) + +(defmacro delete-route [name path params code] + "Delete request" + `(route-with-methods ~name ~path ~params ~code ["DELETE"])) + + +;;; Simple example application +;;; Requires to have Flask installed + +;; (import [flask [Flask]]) +;; (setv app (Flask "__main__")) + +;; (require methy) + +;; (print "setup / with GET") +;; (route get-index "/" [] (str "Hy world!")) + +;; (print "setup /post with POST") +;; (post-route post-index "/post" [] (str "Hy post world!")) + +;; (route-with-methods both-index "/both" [] +;; (str "Hy to both worlds!") ["GET" "POST"]) + +;; (.run app) + +;;; Now you can do: +;;; curl 127.0.0.1:5000 +;;; curl -X POST 127.0.0.1:5000/post +;;; curl -X POST 127.0.0.1:5000/both +;;; curl 127.0.0.1:5000/both diff --git a/hy/contrib/meth.py b/hy/contrib/meth.py deleted file mode 100644 index eacd443..0000000 --- a/hy/contrib/meth.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2013 Paul Tagliamonte -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - -from hy.models.expression import HyExpression -from hy.models.symbol import HySymbol -from hy.models.string import HyString -from hy.models.list import HyList -from hy.models.dict import HyDict - -from hy.macros import macro - - -def router(tree, rkwargs=None): - tree = HyExpression(tree) - name = tree.pop(0) - path = tree.pop(0) - tree.insert(0, HySymbol("fn")) - tree = HyExpression([HySymbol("def"), name, tree]) - - route = HyExpression([HySymbol(".route"), HySymbol("app"), path]) - - if rkwargs: - route = HyExpression([HySymbol("kwapply"), route, - HyDict({HyString("methods"): rkwargs})]) - - return HyExpression([HySymbol("with_decorator"), route, tree]) - - -@macro("route") -def route_macro(*tree): - return router(tree) - - -@macro("post_route") -def post_route_macro(*tree): - return router(tree, rkwargs=HyList([HyString("POST")])) - - -@macro("get_route") -def get_route_macro(*tree): - return router(tree, rkwargs=HyList([HyString("GET")])) From 3f1243f88cf94afa76758ad4d30f5def60db80ea Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Sun, 22 Sep 2013 13:10:37 +0200 Subject: [PATCH 2/8] changed to use short version of macros --- hy/contrib/meth.hy | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hy/contrib/meth.hy b/hy/contrib/meth.hy index ca3f739..d4eef65 100644 --- a/hy/contrib/meth.hy +++ b/hy/contrib/meth.hy @@ -3,16 +3,16 @@ (defmacro route [name path params code] "Default get request" - (quasiquote (let [[deco ((getattr app "route") (unquote path))]] - (with-decorator deco - (defn (unquote name) (unquote params) (unquote-splice code)))))) + `(let [[deco ((getattr app "route") ~path)]] + (with-decorator deco + (defn ~name ~params ~@code)))) (defmacro route-with-methods [name path params code methods] "Same as route but with an extra methods array to specify HTTP methods" - (quasiquote (let [[deco (kwapply ((getattr app "route") (unquote path)) - {"methods" (unquote methods)})]] - (with-decorator deco - (defn (unquote name) (unquote params) (unquote-splice code)))))) + `(let [[deco (kwapply ((getattr app "route") ~path) + {"methods" ~methods})]] + (with-decorator deco + (defn ~name ~params ~@code)))) ;; Some macro examples (defmacro post-route [name path params code] From ff7c71b9c934962d992b0c944f39705ffe56f793 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Mon, 23 Sep 2013 01:23:44 +0200 Subject: [PATCH 3/8] comply with hy's tao rules for getattr --- hy/contrib/meth.hy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hy/contrib/meth.hy b/hy/contrib/meth.hy index d4eef65..1f4ae70 100644 --- a/hy/contrib/meth.hy +++ b/hy/contrib/meth.hy @@ -3,13 +3,13 @@ (defmacro route [name path params code] "Default get request" - `(let [[deco ((getattr app "route") ~path)]] + `(let [[deco (.route app ~path)]] (with-decorator deco (defn ~name ~params ~@code)))) (defmacro route-with-methods [name path params code methods] "Same as route but with an extra methods array to specify HTTP methods" - `(let [[deco (kwapply ((getattr app "route") ~path) + `(let [[deco (kwapply (.route app ~path) {"methods" ~methods})]] (with-decorator deco (defn ~name ~params ~@code)))) From a038d3c592f86fda040151f2db55316bfc421b74 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sun, 29 Sep 2013 18:33:27 +0300 Subject: [PATCH 4/8] Remove the redundant pass statement. --- hy/models/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/hy/models/__init__.py b/hy/models/__init__.py index 775862a..42032ca 100644 --- a/hy/models/__init__.py +++ b/hy/models/__init__.py @@ -24,7 +24,6 @@ class HyObject(object): Generic Hy Object model. This is helpful to inject things into all the Hy lexing Objects at once. """ - pass def replace(self, other): if isinstance(other, HyObject): From 0248e426337e5927a5ac1fa45d3243000142969f Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Tue, 8 Oct 2013 05:16:31 +0300 Subject: [PATCH 5/8] Update hacking guide. --- docs/hacking.rst | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/docs/hacking.rst b/docs/hacking.rst index 404f4f3..0d7faba 100644 --- a/docs/hacking.rst +++ b/docs/hacking.rst @@ -2,6 +2,8 @@ Hacking on hy =============== +.. highlight:: bash + Join our hyve! ============== @@ -21,24 +23,32 @@ Hack! Do this: -1. create a `Python virtual environment - `_ -2. (optional) go to https://github.com/paultag/hy and fork it -3. get the source code:: +1. create a `virtual environment + `_:: - $ git clone git://github.com/paultag/hy.git + $ virtualenv venv - (or use your fork) -4. install for hacking:: + and activate it:: - $ python setup.py develop + $ . venv/bin/activate +2. get the source code:: -5. install other develop-y requirements:: + $ git clone https://github.com/hylang/hy.git + + or use your fork:: + + $ git clone git@github.com:/hy.git +3. install for hacking:: + + $ cd hy/ + $ pip install -e . + +4. install other develop-y requirements:: $ pip install -r requirements-dev.txt -6. do awesome things; make someone shriek in delight/disgust at what - you have wrought +5. do awesome things; make someone shriek in delight/disgust at what + you have wrought. Test! @@ -60,7 +70,7 @@ Document! Documentation is located in ``docs/``. We use `Sphinx `_. -To build the docs in html:: +To build the docs in HTML:: $ cd docs $ make html From 221a9b4918cdb37d740ae3a02e12bf2836919e28 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Wed, 9 Oct 2013 12:36:35 +0300 Subject: [PATCH 6/8] Mention virtualenvwrapper in the hacking guide. --- docs/hacking.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/hacking.rst b/docs/hacking.rst index 0d7faba..b6a0963 100644 --- a/docs/hacking.rst +++ b/docs/hacking.rst @@ -31,6 +31,13 @@ Do this: and activate it:: $ . venv/bin/activate + + or use `virtualenvwrapper `_ + to create and manage your virtual environment:: + + $ mkvirtualenv venv + $ workon venv + 2. get the source code:: $ git clone https://github.com/hylang/hy.git From 6364296a7bebe3ec9ba54a1fbf0973738f83c3c9 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Thu, 10 Oct 2013 15:59:19 +0100 Subject: [PATCH 7/8] Documented punycode conversion more thoroughly using a compound case --- docs/language/api.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/language/api.rst b/docs/language/api.rst index 9226bc5..3fe8beb 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -24,8 +24,8 @@ languages. * UTF-8 entities will be encoded using `punycode `_ and prefixed with - `hy_`. For instance, `⚘` will become `hy_w7h`, and `♥` will become - `hy_g6h`. + `hy_`. For instance, `⚘` will become `hy_w7h`, `♥` will become `hy_g6h`, + and `i♥u` will become `hy_iu_t0x`. * Symbols that contain dashes will have them replaced with underscores. For example, `render-template` will become `render_template`. From 656d6461988430de7a9e003ac0d7ef5ba0beff99 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Thu, 10 Oct 2013 17:17:43 -0400 Subject: [PATCH 8/8] change virtualenv name to be `hy' --- docs/hacking.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/hacking.rst b/docs/hacking.rst index b6a0963..77e0036 100644 --- a/docs/hacking.rst +++ b/docs/hacking.rst @@ -35,8 +35,8 @@ Do this: or use `virtualenvwrapper `_ to create and manage your virtual environment:: - $ mkvirtualenv venv - $ workon venv + $ mkvirtualenv hy + $ workon hy 2. get the source code::