From 1f1676c35415f3026d3ef436715c414320789a64 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Wed, 27 Mar 2013 22:19:27 -0400 Subject: [PATCH] Adding in meth --- hy/contrib/__init__.py | 0 hy/contrib/meth.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 hy/contrib/__init__.py create mode 100644 hy/contrib/meth.py diff --git a/hy/contrib/__init__.py b/hy/contrib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hy/contrib/meth.py b/hy/contrib/meth.py new file mode 100644 index 0000000..d980053 --- /dev/null +++ b/hy/contrib/meth.py @@ -0,0 +1,38 @@ +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.pop(0) + 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("decorate_with"), 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")]))