fixing up dicts + macros

This commit is contained in:
Paul R. Tagliamonte 2013-03-10 10:30:03 -04:00
parent 34e10a4dba
commit 0a86226da0
4 changed files with 41 additions and 8 deletions

View File

@ -20,6 +20,7 @@
from hy.models.expression import HyExpression
from hy.models.string import HyString
from hy.models.dict import HyDict
from hy.models.list import HyList
_hy_macros = {}
@ -47,6 +48,11 @@ def process(tree):
ntree.replace(tree)
return ntree
if isinstance(tree, HyDict):
obj = HyDict({process(x): process(tree[x]) for x in tree})
obj.replace(tree)
return obj
if isinstance(tree, HyList):
obj = HyList([process(x) for x in tree])
obj.replace(tree)

View File

@ -25,4 +25,10 @@ class HyDict(HyObject, dict):
"""
HyDict (just a dict)
"""
pass
def replace(self, other):
for x in self:
self[x].replace(other)
x.replace(other)
HyObject.replace(self, other)

View File

@ -11,3 +11,5 @@
(route "/" [] (render-template "index.html"))
(post-route "/test" [] (render-template "index.html"))

View File

@ -4,22 +4,41 @@ import hy # NOQA
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
@macro("route")
def route_macro(tree):
""" Simple routing macro """
def router(tree, rkwargs=None):
tree.pop(0)
path = tree.pop(0)
tree.insert(0, HySymbol("fn"))
route = HyExpression([HySymbol(".route"),
HySymbol("app"),
path])
if rkwargs:
route = HyExpression([HySymbol("kwapply"),
route,
HyDict({HyString("methods"): rkwargs})])
return HyExpression([HySymbol("decorate_with"),
HyExpression([HySymbol(".route"),
HySymbol("app"),
path]), tree])
route,
tree])
@macro("route")
def route_macro(tree):
return router(tree)
@macro("post_route")
def route_macro(tree):
return router(tree, rkwargs=HyList([HyString("POST")]))
from app import app