Skip to content

Commit

Permalink
Component descriptions: Add a required attribute to preferences and p…
Browse files Browse the repository at this point in the history
…arameters. See #291
  • Loading branch information
aarranz committed Jun 23, 2017
1 parent 084f745 commit 8f0e519
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 15 deletions.
33 changes: 31 additions & 2 deletions src/wirecloud/commons/tests/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def setUpClass(cls):
'default': 'value',
'value': None,
'multiuser': False,
'required': False,
},
{
'name': 'pref2',
Expand All @@ -144,6 +145,7 @@ def setUpClass(cls):
'default': '',
'value': '5',
'multiuser': False,
'required': False,
}
],
'properties': [
Expand Down Expand Up @@ -264,6 +266,7 @@ def setUpClass(cls):
'default': 'value',
'value': None,
'multiuser': False,
'required': False,
},
{
'name': 'pref2',
Expand All @@ -275,6 +278,7 @@ def setUpClass(cls):
'default': '',
'value': '5',
'multiuser': False,
'required': False,
}
],
'properties': [
Expand Down Expand Up @@ -937,8 +941,26 @@ def setUpClass(cls):
{'type': 'feature', 'name': 'PubSub'}
],
'params': [
{'name': 'param1', 'label': 'Param 1', 'type': 'text'},
{'name': 'param2', 'label': 'Param 2', 'type': 'password'}
{
'name': 'param1',
'label': 'Param 1',
'type': 'text',
'default': '',
'description': 'param 1 description',
'readonly': True,
'required': True,
'value': 'param 1 value',
},
{
'name': 'param2',
'label': 'Param 2',
'type': 'password',
'default': '',
'description': 'param 2 description',
'readonly': False,
'required': False,
'value': None,
}
],
'preferences': {
'columns': '8'
Expand Down Expand Up @@ -1135,6 +1157,7 @@ def setUpClass(cls):
'default': '',
'value': None,
'multiuser': False,
'required': False,
},
{
'name': 'pref2',
Expand All @@ -1146,6 +1169,7 @@ def setUpClass(cls):
'default': 'value',
'value': '5',
'multiuser': False,
'required': False,
}
],
'properties': [
Expand Down Expand Up @@ -1340,6 +1364,7 @@ def setUpClass(cls):
'default': '',
'value': None,
'multiuser': False,
'required': False,
},
{
'name': 'pref2',
Expand All @@ -1351,6 +1376,7 @@ def setUpClass(cls):
'default': '',
'value': None,
'multiuser': False,
'required': False,
},
],
'properties': [],
Expand Down Expand Up @@ -1586,6 +1612,9 @@ def test_json_parser_minimal_endpoint_info(self):

def test_json_parser_minimal_preference_info(self):

# Check that a component description providing the minimal info for a
# preference is parsed as expected

json_description = read_template('minimal_preference_info.json')
template = TemplateParser(json_description)
processed_info = template.get_resource_info()
Expand Down
17 changes: 17 additions & 0 deletions src/wirecloud/commons/utils/template/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def _init(self):
self._check_boolean_fields(('readonly', 'secure'), place=preference, default=False)
self._check_string_fields(('value',), place=preference, null=True, default=None)
preference['multiuser'] = False
self._check_boolean_fields('required', place=preference, default=False)

for prop in self._info['properties']:
self._check_string_fields(('name', 'type'), place=prop, required=True)
Expand All @@ -222,6 +223,13 @@ def _init(self):
elif self._info['type'] == 'mashup':

self._check_array_fields(('params', 'embedded'))
for preference in self._info['params']:
self._check_string_fields(('name', 'type'), place=preference, required=True)
self._check_string_fields(('label', 'description', 'default'), place=preference)
self._check_boolean_fields('readonly', place=preference, default=False)
self._check_string_fields(('value',), place=preference, null=True, default=None)
self._check_boolean_fields('required', place=preference, default=True)

for component in self._info['embedded']:
if isinstance(component, dict):
self._check_string_fields(('vendor', 'name', 'version', 'src'), place=component, required=True)
Expand Down Expand Up @@ -292,6 +300,15 @@ def _init(self):
self._check_string_fields(('label', 'description', 'friendcode'), place=output_endpoint)
self._add_translation_index(output_endpoint['label'], type='outputendpoint', variable=output_endpoint['name'], field='label')
self._add_translation_index(output_endpoint['description'], type='outputendpoint', variable=output_endpoint['name'], field='description')
else:

for preference in self._info['params']:
self._add_translation_index(preference['label'], type='vdef', variable=preference['name'], field='label')
self._add_translation_index(preference['description'], type='vdef', variable=preference['name'], field='description')

if preference['type'] == 'list':
for option_index, option in enumerate(preference['options']):
self._add_translation_index(option['label'], type='upo', variable=preference['name'], option=option_index)

# Requirements
self._check_array_fields(('requirements',))
Expand Down
11 changes: 9 additions & 2 deletions src/wirecloud/commons/utils/template/parsers/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ def _parse_component_info(self):
'value': self._get_field(WIRE, 'value', preference, required=False, default=None),
'secure': self._get_field(WIRE, 'secure', preference, required=False).lower() == 'true',
'multiuser': False,
'required': self._get_field(WIRE, 'required', preference, required=False).lower() == 'true',
}
if preference_info['type'] == 'list':
preference_info['options'] = []
Expand Down Expand Up @@ -665,10 +666,16 @@ def _parse_workspace_info(self):
ordered_params = sorted(self._graph.objects(self._rootURI, WIRE_M['hasMashupParam']), key=lambda raw_param: possible_int(self._get_field(WIRE, 'index', raw_param, required=False)))
self._info['params'] = []
for param in ordered_params:
var_name = self._get_field(DCTERMS, 'title', param, required=True)
self._info['params'].append({
'name': self._get_field(DCTERMS, 'title', param),
'label': self._get_field(RDFS, 'label', param),
'name': var_name,
'label': self._get_translation_field(RDFS, 'label', param, var_name + '_label', required=True, type='vdef', variable=var_name, field='label'),
'type': self._get_field(WIRE, 'type', param),
'description': self._get_translation_field(RDFS, 'description', param, var_name + '_description', required=False, type='vdef', variable=var_name, field='description'),
'readonly': self._get_field(WIRE, 'readonly', param, required=False).lower() == 'true',
'default': self._get_field(WIRE, 'default', param, required=False),
'value': self._get_field(WIRE, 'value', param, required=False, default=None),
'required': self._get_field(WIRE, 'required', param, required=False, default="true").lower() == 'true',
})

self._info['embedded'] = []
Expand Down
14 changes: 10 additions & 4 deletions src/wirecloud/commons/utils/template/parsers/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ def _parse_component_preferences(self):
'default': text_type(preference.get('default', '')),
'value': preference.get('value'),
'secure': preference.get('secure', 'false').lower() == 'true',
'multiuser': False
'multiuser': False,
'required': preference.get('required', 'false').lower() == 'true',
}

if preference_info['type'] == 'list':
Expand Down Expand Up @@ -511,9 +512,14 @@ def _parse_workspace_info(self):
self._info['params'] = []
for param in self._xpath(PARAM_XPATH, self._doc):
self._info['params'].append({
'name': param.get('name'),
'label': param.get('label'),
'type': param.get('type'),
'name': text_type(param.get('name')),
'type': text_type(param.get('type')),
'label': text_type(param.get('label', '')),
'description': text_type(param.get('description', '')),
'readonly': param.get('readonly', 'false').lower() == 'true',
'default': text_type(param.get('default', '')),
'value': param.get('value'),
'required': param.get('required', 'true').lower() == 'true',
})

self._info['embedded'] = []
Expand Down
21 changes: 14 additions & 7 deletions src/wirecloud/commons/utils/template/schemas/xml_schema.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,32 @@
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="value" type="xs:string">
<xs:annotation>
<xs:documentation>
Initial value. If not specified, the default value will be used as the initial value.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="readonly" use="optional" type="xs:boolean">
<xs:annotation>
<xs:documentation>
Whether this preference can be modified.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="required" use="optional" type="xs:boolean">
<xs:annotation>
<xs:documentation>
If false, this field is allowed to be empty/blank. Default is true.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>

<xs:complexType name="Preference">
<xs:complexContent>
<xs:extension base="macd:MashupPreference">
<xs:attribute name="value" type="xs:string">
<xs:annotation>
<xs:documentation>
Initial value. If not specified, the default value will be used as the initial value.
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="secure" type="xs:boolean" use="optional">
<xs:annotation>
<xs:documentation>
Expand Down
17 changes: 17 additions & 0 deletions src/wirecloud/commons/utils/template/writers/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ def write_mashup_params(graph, resource_uri, template_info):
graph.add((param_node, WIRE['index'], rdflib.Literal(str(param_index))))
graph.add((param_node, RDFS['label'], rdflib.Literal(param['label'])))
graph.add((param_node, WIRE['type'], rdflib.Literal(param['type'])))
graph.add((param_node, RDFS['description'], rdflib.Literal(param['description'])))

if param.get('readonly', False) is True:
graph.add((param_node, WIRE['readonly'], rdflib.Literal('true')))

if param.get('default') not in (None, ''):
graph.add((param_node, WIRE['default'], rdflib.Literal(param.get('default'))))

if param.get('value'):
graph.add((param_node, WIRE['value'], rdflib.Literal(param['value'])))

if param.get('required', True) is False:
graph.add((param_node, WIRE['required'], rdflib.Literal('false')))

graph.add((resource_uri, WIRE_M['hasMashupParam'], param_node))


Expand Down Expand Up @@ -515,6 +529,9 @@ def build_rdf_graph(template_info):
if pref.get('secure'):
graph.add((pref_node, WIRE['secure'], rdflib.Literal(pref.get('secure'))))

if pref.get('required', False) is True:
graph.add((pref_node, WIRE['required'], rdflib.Literal('true')))

if pref.get('options'):
for option_index, option in enumerate(pref['options']):
option_node = rdflib.BNode()
Expand Down
4 changes: 4 additions & 0 deletions src/wirecloud/commons/utils/template/writers/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def write_mashup_tree(doc, resources, options):
pref_element = etree.SubElement(preferences, 'preference', name=pref['name'])
addAttributes(pref, pref_element, ('type', 'label', 'description', 'default'))
addAttribute(pref, pref_element, 'readonly', default='false', type='boolean')
addAttribute(pref, pref_element, 'required', default='true', type='boolean')

if pref['value'] is not None:
pref_element.set('value', pref['value'])

# Embedded resources
if len(options['embedded']) > 0:
Expand Down

0 comments on commit 8f0e519

Please sign in to comment.