rel
[librarian.git] / src / librarian / dcparser.py
index 432b580..1976850 100644 (file)
@@ -153,6 +153,9 @@ def as_unicode(text):
     else:
         return TextPlus(text.decode('utf-8'))
 
+def as_bool(text):
+    return text == 'true'
+as_bool.no_lang = True
 
 def as_wluri_strict(text):
     return WLURI.strict(text)
@@ -203,7 +206,7 @@ class Field(object):
                 if validator is None or val[0] is None:
                     return val[0]
                 nv = validator(val[0])
-                if hasattr(val[0], 'lang'):
+                if hasattr(val[0], 'lang') and not hasattr(validator, 'no_lang'):
                     setattr(nv, 'lang', val[0].lang)
                 return nv
         except ValueError as e:
@@ -212,7 +215,7 @@ class Field(object):
                 % (self.uri, e.message)
             )
 
-    def validate(self, fdict, fallbacks=None, strict=False):
+    def validate(self, fdict, fallbacks=None, strict=False, validate_required=True):
         if fallbacks is None:
             fallbacks = {}
         if self.uri not in fdict:
@@ -227,8 +230,10 @@ class Field(object):
                     f = [fallbacks[self.salias]]
                 else:
                     f = self.default
-            else:
+            elif validate_required:
                 raise ValidationError("Required field %s not found" % self.uri)
+            else:
+                return None
         else:
             f = fdict[self.uri]
 
@@ -290,6 +295,8 @@ class WorkInfo(six.with_metaclass(DCInfo, object)):
         Field(PLMETNS('digitisationSponsor'), 'sponsors', multiple=True,
               required=False),
         Field(WLNS('digitisationSponsorNote'), 'sponsor_note', required=False),
+        Field(WLNS('contentWarning'), 'content_warnings', multiple=True,
+              required=False),
         Field(WLNS('developmentStage'), 'stage', required=False),
     )
 
@@ -363,7 +370,7 @@ class WorkInfo(six.with_metaclass(DCInfo, object)):
 
         return cls(desc.attrib, field_dict, *args, **kwargs)
 
-    def __init__(self, rdf_attrs, dc_fields, fallbacks=None, strict=False):
+    def __init__(self, rdf_attrs, dc_fields, fallbacks=None, strict=False, validate_required=True):
         """
         rdf_attrs should be a dictionary-like object with any attributes
         of the RDF:Description.
@@ -376,7 +383,7 @@ class WorkInfo(six.with_metaclass(DCInfo, object)):
 
         for field in self.FIELDS:
             value = field.validate(dc_fields, fallbacks=fallbacks,
-                                   strict=strict)
+                                   strict=strict, validate_required=validate_required)
             setattr(self, 'prop_' + field.name, value)
             self.fmap[field.name] = field
             if field.salias:
@@ -523,6 +530,8 @@ class BookInfo(WorkInfo):
         Field(WLNS('coverClass'), 'cover_class', default=['default']),
         Field(WLNS('coverLogoUrl'), 'cover_logo_urls', multiple=True,
               required=False),
+        Field(WLNS('endnotes'), 'endnotes', as_bool,
+              required=False),
 
         Field('pdf-id',  'isbn_pdf',  required=False),
         Field('epub-id', 'isbn_epub', required=False),