fixing up dicts + macros
This commit is contained in:
parent
34e10a4dba
commit
0a86226da0
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -11,3 +11,5 @@
|
||||
|
||||
|
||||
(route "/" [] (render-template "index.html"))
|
||||
|
||||
(post-route "/test" [] (render-template "index.html"))
|
||||
|
33
site/shim.py
33
site/shim.py
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user