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.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)
|
||||||
|
@ -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)
|
||||||
|
@ -11,3 +11,5 @@
|
|||||||
|
|
||||||
|
|
||||||
(route "/" [] (render-template "index.html"))
|
(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.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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user