Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions odml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,18 +654,31 @@ def get_repository(self):
"""
return self._repository

def create_section(self, name, type="undefined", oid=None):
def create_section(self, name=None, type="n.s.", oid=None, definition=None,
reference=None, repository=None, link=None, include=None):
"""
Creates a new subsection that is a child of this section.

:param name: The name of the section to create.
:param type: The type of the section.
:param name: The name of the section to create. If the name is not
provided, the object id of the Section is assigned as its name.
Section name is a required attribute.
:param type: String providing a grouping description for similar Sections.
Section type is a required attribute and will be set to the string
'n.s.' by default.
:param oid: object id, UUID string as specified in RFC 4122. If no id
is provided, an id will be generated and assigned.
:param definition: String defining this Section.
:param reference: A reference (e.g. an URL) to an external definition
of the Section.
:param repository: URL to a repository in which the Section is defined.
:param link: Specifies a soft link, i.e. a path within the document.
:param include: Specifies an arbitrary URL. Can only be used if *link* is not set.

:return: The new section.
"""
from odml.section import BaseSection
sec = BaseSection(name=name, type=type, oid=oid)
sec = BaseSection(name=name, type=type, definition=definition, reference=reference,
repository=repository, link=link, include=include, oid=oid)
sec.parent = self

return sec
4 changes: 3 additions & 1 deletion odml/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def __init__(self, name=None, values=None, parent=None, unit=None,

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

def __len__(self):
Expand Down
15 changes: 10 additions & 5 deletions odml/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ class BaseSection(base.Sectionable):
An odML Section.

:param name: string providing the name of the Section. If the name is not
provided, the uuid of the Property is assigned as its name.
provided, the object id of the Section is assigned as its name.
Section name is a required attribute.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"... the object id of the Section..."

Copy link
Contributor Author

@mpsonntag mpsonntag Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste at its best it seems. Thanks for the catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not show up as updated since the line below is highlighted. Also changed it in odml/base.py.

:param type: String providing a grouping description for similar Sections.
Section type is a required attribute and will be set to the string
'n.s.' by default.
:param parent: the parent object of the new Section. If the object is not
an odml.Section or an odml.Document, a ValueError is raised.
:param definition: String describing the definition of the Section.
:param definition: String defining this Section.
:param reference: A reference (e.g. an URL) to an external definition
of the Section.
:param repository: URL to a repository where this Section can be found.
:param repository: URL to a repository in which the Section is defined.
:param link: Specifies a soft link, i.e. a path within the document.
:param include: Specifies an arbitrary URL. Can only be used if *link* is not set.
:param oid: object id, UUID string as specified in RFC 4122. If no id is provided,
Expand All @@ -49,7 +52,7 @@ class BaseSection(base.Sectionable):

_format = fmt.Section

def __init__(self, name=None, type=None, parent=None,
def __init__(self, name=None, type="n.s.", parent=None,
definition=None, reference=None,
repository=None, link=None, include=None, oid=None):

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

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

def __repr__(self):
Expand Down
9 changes: 4 additions & 5 deletions odml/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ def object_required_attributes(obj):
args = obj.format().arguments
for arg in args:
if arg[1] == 1:
msg = "Missing required attribute '%s'" % (arg[0])
if not hasattr(obj, arg[0]):
msg = "Missing attribute %s for %s" % (arg[0], obj.format().name.capitalize())
yield ValidationError(obj, msg, LABEL_ERROR)
continue
obj_arg = getattr(obj, arg[0])
if not obj_arg and not isinstance(obj_arg, bool):
msg = "%s %s undefined" % (obj.format().name.capitalize(), arg[0])
yield ValidationError(obj, msg, LABEL_ERROR)


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

def section_type_must_be_defined(sec):
"""
Tests that no Section has an undefined type.
Tests that no Section has an unspecified type and adds a warning otherwise.

:param sec: odml.Section.
"""
if sec.type is None or sec.type == '' or sec.type == 'undefined':
yield ValidationError(sec, 'Section type undefined', LABEL_WARNING)
if sec.type and sec.type == "n.s.":
yield ValidationError(sec, "Section type not specified", LABEL_WARNING)


Validation.register_handler('section', section_type_must_be_defined)
Expand Down
2 changes: 1 addition & 1 deletion test/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def test_create_section(self):
self.assertEqual(len(root.sections), 2)
self.assertEqual(subsec.parent, root)
self.assertEqual(root.sections[name], subsec)
self.assertEqual(root.sections[name].type, "undefined")
self.assertEqual(root.sections[name].type, "n.s.")

name = "subsubsec"
subsec = root.sections[0].create_section(name)
Expand Down
2 changes: 1 addition & 1 deletion test/test_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ def test_create_section(self):
self.assertEqual(len(root.sections), 2)
self.assertEqual(subsec.parent, root)
self.assertEqual(root.sections[name], subsec)
self.assertEqual(root.sections[name].type, "undefined")
self.assertEqual(root.sections[name].type, "n.s.")

name = "subsubsec"
subsec = root.sections[0].create_section(name)
Expand Down
Loading