Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Legacy declaration generator
#!/usr/bin/env python
from twisted.python.reflect import namedAny
template = """
declareLegacyItem(
typeName=%r,
schemaVersion=%d,
attributes=dict(
%s))
"""
_attributeDefaults = [
('allowNone', True),
('default', None),
('defaultFactory', None)]
_attributeDefaultsDict = dict(_attributeDefaults)
def getAttributeAttributes(attr):
attrs = ((name, getattr(attr, name))
for name, default
in _attributeDefaults)
for name, value in attrs:
if value != _attributeDefaultsDict[name]:
if name == 'defaultFactory':
#raise ValueError('defaultFactory is hard: %r' % (attr,))
continue
yield name, value
def emitAttribute((attrName, attr)):
attrs = ['%s=%r' % (key, value)
for key, value
in getAttributeAttributes(attr)]
return '%s=%s(%s)' % (
attrName,
attr.__class__.__name__,
', '.join(attrs))
def legacyItemCode(itemClass):
calculatedAttributes = ',\n '.join(
map(emitAttribute, 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