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

View File

@ -25,4 +25,10 @@ class HyDict(HyObject, dict):
""" """
HyDict (just a 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")) (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.expression import HyExpression
from hy.models.symbol import HySymbol 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 from hy.macros import macro
@macro("route") def router(tree, rkwargs=None):
def route_macro(tree):
""" Simple routing macro """
tree.pop(0) tree.pop(0)
path = tree.pop(0) path = tree.pop(0)
tree.insert(0, HySymbol("fn")) 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"), return HyExpression([HySymbol("decorate_with"),
HyExpression([HySymbol(".route"), route,
HySymbol("app"), tree])
path]), 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 from app import app