Created
October 14, 2013 18:11
-
-
Save groner/6979656 to your computer and use it in GitHub Desktop.
rubedo model schema exporter for prac.doc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf8 | |
import json | |
from collections import OrderedDict | |
import dfiance | |
from dfiance.graph import TypeGraphNode | |
from rubedo.document import ( | |
Document, | |
DocRef, | |
) | |
import vertigo | |
from outofbounds import models | |
from outofbounds import dfiance_interval | |
def main(): | |
print '[\n'+(',\n'.join( | |
json.dumps(dumpmodel(TypeGraphNode(model))) | |
for model in models.registry.mappings | |
))+']\n' | |
@vertigo.bottom_up | |
def dumpmodel(field, path, children, preval): | |
desc = OrderedDict() | |
if isinstance(field, type) and issubclass(field, Document): | |
desc.update(type='document') | |
desc.update(name=field.__name__) | |
desc.update(children=children.values()) | |
return desc | |
if isinstance(field, dfiance.Field): | |
dfier = field.dfier | |
else: | |
dfier = field | |
if isinstance(dfier, dfiance.String): | |
desc.update(type='string') | |
elif isinstance(dfier, dfiance.Number): | |
desc.update(type='number') | |
elif isinstance(dfier, dfiance.Boolean): | |
desc.update(type='bool') | |
elif isinstance(dfier, dfiance.DateTime): | |
desc.update(type='datetime') | |
#elif isinstance(dfier, dfiance_interval.Interval): | |
# desc.update(type='datetime') | |
elif isinstance(dfier, dfiance.SchemaMapping): | |
desc.update(type='mapping', children=children) | |
elif isinstance(dfier, dfiance.List): | |
elt_type = children['elt_type'] | |
# No, elt_type, your real name is not elt_type. | |
del elt_type['name'] | |
desc.update(type='list') | |
desc.update(name=path[-1]) | |
desc.update(element=elt_type) | |
elif isinstance(dfier, DocRef): | |
desc.update(type='ref') | |
desc.update(name=path[-1]) | |
desc.update(schema=dfier.doctype.__name__) | |
else: | |
raise TypeError('wtf is {}‽'.format(dfier)) | |
desc.update(name=path[-1]) | |
return desc | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment