def __init__(self, include_primary_keys=False, include_foreign_keys=False, sequence_callback=None): self.generator = XSDGenerator( include_primary_keys=include_primary_keys, include_foreign_keys=include_foreign_keys, sequence_callback=sequence_callback)
def __init__(self, include_primary_keys=False, include_foreign_keys=False, sequence_callback=None): self.generator = XSDGenerator( include_primary_keys=include_primary_keys, include_foreign_keys=include_foreign_keys, sequence_callback=sequence_callback )
class XSD(object): """ XSD renderer. An XSD renderer generate an XML schema document from an SQLAlchemy Table object. Configure a XSD renderer using the ``add_renderer`` method on the Configurator object: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD()) Once this renderer has been registered as above , you can use ``xsd`` as the ``renderer`` parameter to ``@view_config`` or to the ``add_view`` method on the Configurator object: .. code-block:: python from myapp.models import Spot @view_config(renderer='xsd') def myview(request): return Spot By default, the XSD renderer will skip columns which are primary keys or foreign keys. If you wish to include primary keys then pass ``include_primary_keys=True`` when creating the XSD object, for example: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD(include_primary_keys=True)) If you wish to include foreign keys then pass ``include_foreign_keys=True`` when creating the XSD object, for example: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD(include_foreign_keys=True)) The XSD renderer adds ``xsd:element`` nodes for the column properties it finds in the class. The XSD renderer will ignore other property types. For example it will ignore relationship properties and association proxies. If you want to add ``xsd:element`` nodes for other elements in the class then use a ``sequence_callback``. For example: .. code-block: python from papyrus.renderers import XSD from papyrus.xsd import tag def callback(tb, cls): attrs = {} attrs['minOccurs'] = str(0) attrs['nillable'] = 'true' attrs['name'] = 'property_name' with tag(tb, 'xsd:element', attrs) as tb: with tag(tb, 'xsd:simpleType') as tb: with tag(tb, 'xsd:restriction', {'base': 'xsd:string'}) as tb: for enum in ('male', 'female'): with tag(tb, 'xsd:enumeration', {'value': enum}): pass config.add_renderer('xsd', XSD(sequence_callback=callback)) The callback receives an ``xml.etree.ElementTree.TreeBuilder`` object and the mapped class being serialized. It is also possible to extend the column property ``xsd::element`` nodes using ``element_callback``, for example to add an annotation/appinfo element: .. code-block: python from papyrus.renderers import XSD from papyrus.xsd import tag def callback(tb, cls): if column.info.get('readonly'): with tag(tb, 'xsd:annotation'): with tag(tb, 'xsd:appinfo'): with tag(tb, 'readonly', {'value': 'true'}): pass config.add_renderer('xsd', XSD(element_callback=callback)) """ def __init__(self, include_primary_keys=False, include_foreign_keys=False, sequence_callback=None, element_callback=None): self.generator = XSDGenerator( include_primary_keys=include_primary_keys, include_foreign_keys=include_foreign_keys, sequence_callback=sequence_callback, element_callback=element_callback ) def __call__(self, table): def _render(cls, system): request = system.get('request') if request is not None: response = request.response response.content_type = 'application/xml' io = self.generator.get_class_xsd(BytesIO(), cls) return io.getvalue() return _render
class XSD(object): """ XSD renderer. An XSD renderer generate an XML schema document from an SQLAlchemy Table object. Configure a XSD renderer using the ``add_renderer`` method on the Configurator object: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD()) Once this renderer has been registered as above , you can use ``xsd`` as the ``renderer`` parameter to ``@view_config`` or to the ``add_view`` method on the Configurator object: .. code-block:: python from myapp.models import Spot @view_config(renderer='xsd') def myview(request): return Spot By default, the XSD renderer will skip columns which are primary keys or foreign keys. If you wish to include primary keys then pass ``include_primary_keys=True`` when creating the XSD object, for example: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD(include_primary_keys=True)) If you wish to include foreign keys then pass ``include_foreign_keys=True`` when creating the XSD object, for example: .. code-block:: python from papyrus.renderers import XSD config.add_renderer('xsd', XSD(include_foreign_keys=True)) The XSD renderer adds ``xsd:element`` nodes for the column properties it finds in the class. The XSD renderer will ignore other property types. For example it will ignore relationship properties and association proxies. If you want to add ``xsd:element`` nodes for other elements in the class then use a ``sequence_callback``. For example: .. code-block: python from papyrus.renderers import XSD from papyrus.xsd import tag def callback(tb, cls): attrs = {} attrs['minOccurs'] = str(0) attrs['nillable'] = 'true' attrs['name'] = 'property_name' with tag(tb, 'xsd:element', attrs) as tb: with tag(tb, 'xsd:simpleType') as tb: with tag(tb, 'xsd:restriction', {'base': 'xsd:string'}) as tb: for enum in ('male', 'female'): with tag(tb, 'xsd:enumeration', {'value': enum}): pass config.add_renderer('xsd', XSD(sequence_callback=callback)) The callback receives an ``xml.etree.ElementTree.TreeBuilder`` object and the mapped class being serialized. """ def __init__(self, include_primary_keys=False, include_foreign_keys=False, sequence_callback=None): self.generator = XSDGenerator( include_primary_keys=include_primary_keys, include_foreign_keys=include_foreign_keys, sequence_callback=sequence_callback) def __call__(self, table): def _render(cls, system): request = system.get('request') if request is not None: response = request.response response.content_type = 'application/xml' io = self.generator.get_class_xsd(BytesIO(), cls) return io.getvalue() return _render