Generate docs using new properties Schema class

Change-Id: Ic7849a7aaeeb88379695557f7992330f83638c48
This commit is contained in:
Zane Bitter 2013-08-28 18:35:39 +02:00
parent f075b4ed7d
commit fcd91d895d

View File

@ -15,6 +15,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from heat.engine import resources from heat.engine import resources
from heat.engine import properties
from heat.openstack.common.gettextutils import _ from heat.openstack.common.gettextutils import _
from docutils import nodes from docutils import nodes
@ -40,6 +41,9 @@ class ResourcePages(Directive):
self.resource_class = resource_class self.resource_class = resource_class
section = self._section(content, resource_type, '%s') section = self._section(content, resource_type, '%s')
self.props_schemata = properties.schemata(
self.resource_class.properties_schema)
cls_doc = resource_class.__doc__ cls_doc = resource_class.__doc__
if cls_doc: if cls_doc:
para = nodes.paragraph('', cls_doc) para = nodes.paragraph('', cls_doc)
@ -63,31 +67,29 @@ class ResourcePages(Directive):
return section return section
def _prop_syntax_example(self, prop): def _prop_syntax_example(self, prop):
if not prop or not prop.get('Type'): if not prop:
return 'Value' return 'Value'
prop_type = prop.get('Type') if prop.type == properties.LIST:
if prop_type == 'List': schema = lambda i: prop.schema[i] if prop.schema else None
sub_prop = prop.get('Schema') sub_type = [self._prop_syntax_example(schema(i))
sub_type = self._prop_syntax_example(sub_prop) for i in range(2)]
return '[%s, %s, ...]' % (sub_type, sub_type) return '[%s, %s, ...]' % tuple(sub_type)
elif prop_type == 'Map': elif prop.type == properties.MAP:
sub_prop = prop.get('Schema', {}) def sub_props():
sub_props = [] for sub_key, sub_value in prop.schema.items():
for sub_key, sub_value in sub_prop.items(): if sub_value.implemented:
if sub_value.get('Implemented', True): yield '"%s": %s' % (
sub_props.append('"%s": %s' % ( sub_key, self._prop_syntax_example(sub_value))
sub_key, self._prop_syntax_example(sub_value))) return '{%s}' % (', '.join(sub_props()) if prop.schema else '...')
return '{%s}' % ', '.join(sub_props or ['...'])
else: else:
return prop_type return prop.type
def contribute_hot_syntax(self, parent): def contribute_hot_syntax(self, parent):
section = self._section(parent, _('HOT Syntax'), '%s-hot') section = self._section(parent, _('HOT Syntax'), '%s-hot')
schema = self.resource_class.properties_schema
props = [] props = []
for prop_key in sorted(schema.keys()): for prop_key in sorted(self.props_schemata.keys()):
prop = schema[prop_key] prop = self.props_schemata[prop_key]
if prop.get('Implemented', True): if prop.implemented:
props.append('%s: %s' % (prop_key, props.append('%s: %s' % (prop_key,
self._prop_syntax_example(prop))) self._prop_syntax_example(prop)))
@ -105,11 +107,10 @@ resources:
def contribute_yaml_syntax(self, parent): def contribute_yaml_syntax(self, parent):
section = self._section(parent, _('YAML Syntax'), '%s-yaml') section = self._section(parent, _('YAML Syntax'), '%s-yaml')
schema = self.resource_class.properties_schema
props = [] props = []
for prop_key in sorted(schema.keys()): for prop_key in sorted(self.props_schemata.keys()):
prop = schema[prop_key] prop = self.props_schemata[prop_key]
if prop.get('Implemented', True): if prop.implemented:
props.append('%s: %s' % (prop_key, props.append('%s: %s' % (prop_key,
self._prop_syntax_example(prop))) self._prop_syntax_example(prop)))
@ -127,12 +128,11 @@ Resources:
def contribute_json_syntax(self, parent): def contribute_json_syntax(self, parent):
section = self._section(parent, _('JSON Syntax'), '%s-json') section = self._section(parent, _('JSON Syntax'), '%s-json')
schema = self.resource_class.properties_schema
props = [] props = []
for prop_key in sorted(schema.keys()): for prop_key in sorted(self.props_schemata.keys()):
prop = schema[prop_key] prop = self.props_schemata[prop_key]
if prop.get('Implemented', True): if prop.implemented:
props.append('"%s": %s' % (prop_key, props.append('"%s": %s' % (prop_key,
self._prop_syntax_example(prop))) self._prop_syntax_example(prop)))
template = '''{ template = '''{
@ -155,66 +155,46 @@ Resources:
'', nodes.term('', prop_key)) '', nodes.term('', prop_key))
prop_list.append(prop_item) prop_list.append(prop_item)
prop_type = prop.get('Type') prop_item.append(nodes.classifier('', prop.type))
classifier = prop_type
if prop.get('MinValue'):
classifier += _(' from %s') % prop.get('MinValue')
if prop.get('MaxValue'):
classifier += _(' up to %s') % prop.get('MaxValue')
if prop.get('MinLength'):
classifier += _(' from length %s') % prop.get('MinLength')
if prop.get('MaxLength'):
classifier += _(' up to length %s') % prop.get('MaxLength')
prop_item.append(nodes.classifier('', classifier))
definition = nodes.definition() definition = nodes.definition()
prop_item.append(definition) prop_item.append(definition)
if not prop.get('Implemented', True): if not prop.implemented:
para = nodes.inline('', _('Not implemented.')) para = nodes.inline('', _('Not implemented.'))
warning = nodes.note('', para) warning = nodes.note('', para)
definition.append(warning) definition.append(warning)
return return
description = prop.get('Description') if prop.description:
if description: para = nodes.paragraph('', prop.description)
para = nodes.paragraph('', description)
definition.append(para) definition.append(para)
if prop.get('Required'): if prop.required:
para = nodes.paragraph('', _('Required property.')) para = nodes.paragraph('', _('Required property.'))
elif prop.get('Default'): elif prop.default is not None:
para = nodes.paragraph( para = nodes.paragraph(
'', '',
_('Optional property, defaults to "%s".') % _('Optional property, defaults to "%s".') % prop.default)
prop.get('Default'))
else: else:
para = nodes.paragraph('', _('Optional property.')) para = nodes.paragraph('', _('Optional property.'))
definition.append(para) definition.append(para)
if prop.get('AllowedPattern'): for constraint in prop.constraints:
para = nodes.paragraph('', _( para = nodes.paragraph('', str(constraint))
'Value must match pattern: %s') % prop.get('AllowedPattern'))
definition.append(para)
if prop.get('AllowedValues'):
allowed = [str(a) for a in prop.get('AllowedValues')
if a is not None]
para = nodes.paragraph('', _(
'Allowed values: %s') % ', '.join(allowed))
definition.append(para) definition.append(para)
sub_schema = None sub_schema = None
if prop.get('Schema') and prop_type == 'Map': if prop.schema and prop.type == properties.MAP:
para = nodes.emphasis('', _('Map properties:')) para = nodes.emphasis('', _('Map properties:'))
definition.append(para) definition.append(para)
sub_schema = prop.get('Schema') sub_schema = prop.schema
elif prop_type == 'List' and prop.get('Schema', {}).get('Schema'): elif prop.schema and prop.type == properties.LIST:
para = nodes.emphasis( para = nodes.emphasis(
'', _('List contains maps with the properties:')) '', _('List contents:'))
definition.append(para) definition.append(para)
sub_schema = prop.get('Schema').get('Schema') sub_schema = prop.schema
if sub_schema: if sub_schema:
sub_prop_list = nodes.definition_list() sub_prop_list = nodes.definition_list()
@ -224,14 +204,13 @@ Resources:
self.contribute_property(sub_prop_list, sub_prop_key, sub_prop) self.contribute_property(sub_prop_list, sub_prop_key, sub_prop)
def contribute_properties(self, parent): def contribute_properties(self, parent):
schema = self.resource_class.properties_schema if not self.props_schemata:
if not schema:
return return
section = self._section(parent, _('Properties'), '%s-props') section = self._section(parent, _('Properties'), '%s-props')
prop_list = nodes.definition_list() prop_list = nodes.definition_list()
section.append(prop_list) section.append(prop_list)
for prop_key in sorted(schema.keys()): for prop_key in sorted(self.props_schemata.keys()):
prop = schema[prop_key] prop = self.props_schemata[prop_key]
self.contribute_property(prop_list, prop_key, prop) self.contribute_property(prop_list, prop_key, prop)
def contribute_attributes(self, parent): def contribute_attributes(self, parent):