Repository: unt-libraries/pyuntl

Issue:
Add hidden/unhidden helpers to pyuntl
I was thinking that it might be useful to add a few common methods and properties to the python version of a UNTL record. 

I was thinking we could add two properties. 
`record.is_hidden` and `record.is_unhidden` that would return a `True` or `False` depending on the record. 

We could also add two methods, `record.make_hidden()` and `record.make_unhidden()` that could be used to adjust these values. 

I'm open to different language, formatting, and whatnot on the methods or property names. 


Discussion:

Patch:
diff --git a/pyuntl/untl_structure.py b/pyuntl/untl_structure.py
index b47c3fc..25b2487 100644
--- a/pyuntl/untl_structure.py
+++ b/pyuntl/untl_structure.py
@@ -1,5 +1,6 @@
 import socket
 import json
+import sys
 import urllib.request
 from lxml.etree import Element, SubElement, tostring
 from pyuntl import UNTL_XML_ORDER, VOCABULARIES_URL
@@ -457,6 +458,42 @@ class Metadata(UNTLElement):
         # Create the form object.
         return FormGenerator(**kwargs)
 
+    def make_hidden(self):
+        """Make an unhidden record hidden."""
+        for element in self.children:
+            if element.tag == 'meta' and element.qualifier == 'hidden':
+                # Make the element hidden.
+                if element.content == 'False':
+                    element.content = 'True'
+                return None
+        # Create a hidden meta element if it doesn't exist.
+        hidden_element = PYUNTL_DISPATCH['meta'](qualifier='hidden', content='True')
+        self.children.append(hidden_element)
+
+    def make_unhidden(self):
+        """Make a hidden record unhidden."""
+        for element in self.children:
+            if element.tag == 'meta' and element.qualifier == 'hidden':
+                # Make the element unhidden.
+                if element.content == 'True':
+                    element.content = 'False'
+                return None
+        # Create a hidden meta element if it doesn't exist.
+        hidden_element = PYUNTL_DISPATCH['meta'](qualifier='hidden', content='False')
+        self.children.append(hidden_element)
+
+    @property
+    def is_hidden(self):
+        """Return True if a UNTL element is hidden."""
+        for element in self.children:
+            if element.tag == 'meta' and element.qualifier == 'hidden':
+                if element.content == 'True':
+                    return True
+                else:
+                    return False
+        sys.stderr.write('A hidden meta element does not exist.')
+        return False
+
 
 class Title(UNTLElement):
     def __init__(self, **kwargs):


Test patch:
diff --git a/tests/untl_structure_test.py b/tests/untl_structure_test.py
index 3e82016..8f08cb6 100644
--- a/tests/untl_structure_test.py
+++ b/tests/untl_structure_test.py
@@ -587,3 +587,67 @@ def test_generate_form_data(_):
     assert isinstance(fg, us.FormGenerator)
     # Check missing children were added.
     assert len(metadata.children) == len(metadata.contained_children)
+
+
+@pytest.mark.parametrize('test_input_content, test_output',
+                         [
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='True', qualifier='hidden')],
+                              True),
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='False', qualifier='hidden')],
+                              False),
+                         ])
+def test_Metadata_is_hidden(test_input_content, test_output):
+    """Check if a UNTL element is hidden."""
+    metadata = us.Metadata()
+    metadata.children = test_input_content
+    assert metadata.is_hidden is test_output
+
+
+def test_Metadata_is_hidden_with_no_meta_hidden_element(capsys):
+    metadata = us.Metadata()
+    metadata.children = [us.Meta(content='DC', qualifier='system')]
+    assert metadata.is_hidden is False
+    captured = capsys.readouterr()
+    assert captured.err == 'A hidden meta element does not exist.'
+
+
+@pytest.mark.parametrize('test_input_elements, test_input_content',
+                         [
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='True', qualifier='hidden')],
+                              True),
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='False', qualifier='hidden')],
+                              False),
+                             ([us.Meta(content='DC', qualifier='system')],
+                              False)
+                         ])
+def test_Metadata_make_hidden(test_input_elements, test_input_content):
+    """Test if a UNTL unhidden element is altered to hidden."""
+    metadata = us.Metadata()
+    metadata.children = test_input_elements
+    assert metadata.is_hidden is test_input_content
+    metadata.make_hidden()
+    assert metadata.is_hidden is True
+
+
+@pytest.mark.parametrize('test_input_elements, test_input_content',
+                         [
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='True', qualifier='hidden')],
+                              True),
+                             ([us.Meta(content='DC', qualifier='system'),
+                               us.Meta(content='False', qualifier='hidden')],
+                              False),
+                             ([us.Meta(content='DC', qualifier='system')],
+                              False)
+                         ])
+def test_Metadata_make_unhidden(test_input_elements, test_input_content):
+    """Test if a UNTL hidden element is altered to unhidden."""
+    metadata = us.Metadata()
+    metadata.children = test_input_elements
+    assert metadata.is_hidden is test_input_content
+    metadata.make_unhidden()
+    assert metadata.is_hidden is False