Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Created February 20, 2015 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithrandi/cde58f5d4d1f88b8b331 to your computer and use it in GitHub Desktop.
Save mithrandi/cde58f5d4d1f88b8b331 to your computer and use it in GitHub Desktop.
Axiom legacy declaration generator
#!/usr/bin/env python
from twisted.python.reflect import namedAny
template = """
declareLegacyItem(
typeName=%r,
schemaVersion=%d,
attributes=dict(
%s))
"""
def legacyItemCode(itemClass):
def _d(value):
if value is None:
return ''
return ', default=%r' % (value)
calculatedAttributes = ',\n '.join(
['%s=%s(allowNone=%r%s)' % (k, v.__class__.__name__, v.allowNone, _d(v.default))
for k, v in itemClass.getSchema()])
fmt = template % (itemClass.typeName, itemClass.schemaVersion,
calculatedAttributes)
return fmt
if __name__ == '__main__':
import sys
f = sys.stdout
f.write(legacyItemCode(namedAny(sys.argv[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment