Skip to content

Commit 01fb08a

Browse files
authored
Merge pull request #376 from mpsonntag/handleSectionType
Change in Section type default behavior
2 parents 4fcfcc9 + 7ae9698 commit 01fb08a

File tree

7 files changed

+115
-238
lines changed

7 files changed

+115
-238
lines changed

odml/base.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,18 +654,31 @@ def get_repository(self):
654654
"""
655655
return self._repository
656656

657-
def create_section(self, name, type="undefined", oid=None):
657+
def create_section(self, name=None, type="n.s.", oid=None, definition=None,
658+
reference=None, repository=None, link=None, include=None):
658659
"""
659660
Creates a new subsection that is a child of this section.
660661
661-
:param name: The name of the section to create.
662-
:param type: The type of the section.
662+
:param name: The name of the section to create. If the name is not
663+
provided, the object id of the Section is assigned as its name.
664+
Section name is a required attribute.
665+
:param type: String providing a grouping description for similar Sections.
666+
Section type is a required attribute and will be set to the string
667+
'n.s.' by default.
663668
:param oid: object id, UUID string as specified in RFC 4122. If no id
664669
is provided, an id will be generated and assigned.
670+
:param definition: String defining this Section.
671+
:param reference: A reference (e.g. an URL) to an external definition
672+
of the Section.
673+
:param repository: URL to a repository in which the Section is defined.
674+
:param link: Specifies a soft link, i.e. a path within the document.
675+
:param include: Specifies an arbitrary URL. Can only be used if *link* is not set.
676+
665677
:return: The new section.
666678
"""
667679
from odml.section import BaseSection
668-
sec = BaseSection(name=name, type=type, oid=oid)
680+
sec = BaseSection(name=name, type=type, definition=definition, reference=reference,
681+
repository=repository, link=link, include=include, oid=oid)
669682
sec.parent = self
670683

671684
return sec

odml/property.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def __init__(self, name=None, values=None, parent=None, unit=None,
140140

141141
for err in validation.Validation(self).errors:
142142
if err.is_error:
143-
msg = "\n\t- %s %s: %s" % (err.obj, err.rank, err.msg)
143+
use_name = err.obj.name if err.obj.id != err.obj.name else None
144+
prop_formatted = "Property[id=%s|%s]" % (err.obj.id, use_name)
145+
msg = "%s\n Validation[%s]: %s" % (prop_formatted, err.rank, err.msg)
144146
print(msg)
145147

146148
def __len__(self):

odml/section.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ class BaseSection(base.Sectionable):
2626
An odML Section.
2727
2828
:param name: string providing the name of the Section. If the name is not
29-
provided, the uuid of the Property is assigned as its name.
29+
provided, the object id of the Section is assigned as its name.
30+
Section name is a required attribute.
3031
:param type: String providing a grouping description for similar Sections.
32+
Section type is a required attribute and will be set to the string
33+
'n.s.' by default.
3134
:param parent: the parent object of the new Section. If the object is not
3235
an odml.Section or an odml.Document, a ValueError is raised.
33-
:param definition: String describing the definition of the Section.
36+
:param definition: String defining this Section.
3437
:param reference: A reference (e.g. an URL) to an external definition
3538
of the Section.
36-
:param repository: URL to a repository where this Section can be found.
39+
:param repository: URL to a repository in which the Section is defined.
3740
:param link: Specifies a soft link, i.e. a path within the document.
3841
:param include: Specifies an arbitrary URL. Can only be used if *link* is not set.
3942
:param oid: object id, UUID string as specified in RFC 4122. If no id is provided,
@@ -49,7 +52,7 @@ class BaseSection(base.Sectionable):
4952

5053
_format = fmt.Section
5154

52-
def __init__(self, name=None, type=None, parent=None,
55+
def __init__(self, name=None, type="n.s.", parent=None,
5356
definition=None, reference=None,
5457
repository=None, link=None, include=None, oid=None):
5558

@@ -84,7 +87,9 @@ def __init__(self, name=None, type=None, parent=None,
8487

8588
for err in validation.Validation(self).errors:
8689
if err.is_error:
87-
msg = "\n\t- %s %s: %s" % (err.obj, err.rank, err.msg)
90+
use_name = err.obj.name if err.obj.id != err.obj.name else None
91+
sec_formatted = "Section[id=%s|%s/%s]" % (err.obj.id, use_name, err.obj.type)
92+
msg = "%s\n Validation[%s]: %s" % (sec_formatted, err.rank, err.msg)
8893
print(msg)
8994

9095
def __repr__(self):

odml/validation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,12 @@ def object_required_attributes(obj):
133133
args = obj.format().arguments
134134
for arg in args:
135135
if arg[1] == 1:
136+
msg = "Missing required attribute '%s'" % (arg[0])
136137
if not hasattr(obj, arg[0]):
137-
msg = "Missing attribute %s for %s" % (arg[0], obj.format().name.capitalize())
138138
yield ValidationError(obj, msg, LABEL_ERROR)
139139
continue
140140
obj_arg = getattr(obj, arg[0])
141141
if not obj_arg and not isinstance(obj_arg, bool):
142-
msg = "%s %s undefined" % (obj.format().name.capitalize(), arg[0])
143142
yield ValidationError(obj, msg, LABEL_ERROR)
144143

145144

@@ -150,12 +149,12 @@ def object_required_attributes(obj):
150149

151150
def section_type_must_be_defined(sec):
152151
"""
153-
Tests that no Section has an undefined type.
152+
Tests that no Section has an unspecified type and adds a warning otherwise.
154153
155154
:param sec: odml.Section.
156155
"""
157-
if sec.type is None or sec.type == '' or sec.type == 'undefined':
158-
yield ValidationError(sec, 'Section type undefined', LABEL_WARNING)
156+
if sec.type and sec.type == "n.s.":
157+
yield ValidationError(sec, "Section type not specified", LABEL_WARNING)
159158

160159

161160
Validation.register_handler('section', section_type_must_be_defined)

test/test_doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def test_create_section(self):
312312
self.assertEqual(len(root.sections), 2)
313313
self.assertEqual(subsec.parent, root)
314314
self.assertEqual(root.sections[name], subsec)
315-
self.assertEqual(root.sections[name].type, "undefined")
315+
self.assertEqual(root.sections[name].type, "n.s.")
316316

317317
name = "subsubsec"
318318
subsec = root.sections[0].create_section(name)

test/test_section.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ def test_create_section(self):
906906
self.assertEqual(len(root.sections), 2)
907907
self.assertEqual(subsec.parent, root)
908908
self.assertEqual(root.sections[name], subsec)
909-
self.assertEqual(root.sections[name].type, "undefined")
909+
self.assertEqual(root.sections[name].type, "n.s.")
910910

911911
name = "subsubsec"
912912
subsec = root.sections[0].create_section(name)

0 commit comments

Comments
 (0)