doctest fix
[librarian.git] / librarian / dcparser.py
index 49a33d4..992029e 100644 (file)
@@ -7,7 +7,8 @@ from xml.parsers.expat import ExpatError
 from datetime import date
 import time
 
-from librarian import ValidationError, NoDublinCore, ParseError, DCNS, RDFNS
+from librarian import (ValidationError, NoDublinCore, ParseError, DCNS, RDFNS,
+                       WLURI)
 
 import lxml.etree as etree # ElementTree API using libxml2
 from lxml.etree import XMLSyntaxError
@@ -44,6 +45,11 @@ class Person(object):
     def __eq__(self, right):
         return self.last_name == right.last_name and self.first_names == right.first_names
 
+    def __cmp__(self, other):
+        return cmp((self.last_name, self.first_names), (other.last_name, other.first_names))
+
+    def __hash__(self):
+        return hash((self.last_name, self.first_names))
 
     def __unicode__(self):
         if len(self.first_names) > 0:
@@ -122,6 +128,8 @@ class BookInfo(object):
         Field( DCNS('subject.period'), 'epochs', salias='epoch', multiple=True),
         Field( DCNS('subject.type'), 'kinds', salias='kind', multiple=True),
         Field( DCNS('subject.genre'), 'genres', salias='genre', multiple=True),
+        Field( DCNS('audience'), 'audiences', salias='audience', multiple=True,
+                required=False),
         Field( DCNS('date'), 'created_at', as_date),
         Field( DCNS('date.pd'), 'released_to_public_domain_at', as_date, required=False),
         Field( DCNS('contributor.editor'), 'editors', \
@@ -133,17 +141,14 @@ class BookInfo(object):
         Field( DCNS('publisher'), 'publisher'),
         Field( DCNS('source'), 'source_name', required=False),
         Field( DCNS('source.URL'), 'source_url', required=False),
-        Field( DCNS('identifier.url'), 'url'),
-        Field( DCNS('relation.hasPart'), 'parts', multiple=True, required=False),
+        Field( DCNS('identifier.url'), 'url', WLURI),
+        Field( DCNS('relation.hasPart'), 'parts', WLURI, multiple=True, required=False),
         Field( DCNS('rights.license'), 'license', required=False),
         Field( DCNS('rights'), 'license_description'),
+        Field( DCNS('language'), 'language'),
         Field( DCNS('description'), 'description', required=False),
     )
 
-    @property
-    def slug(self):
-        return self.url.rsplit('/', 1)[1]
-
     @classmethod
     def from_string(cls, xml):
         from StringIO import StringIO
@@ -207,6 +212,11 @@ class BookInfo(object):
             self.fmap[field.name] = field
             if field.salias: self.fmap[field.salias] = field
 
+        self.validate()
+
+    def validate(self):
+        self.url.validate_language(self.language)
+
     def __getattribute__(self, name):
         try:
             field = object.__getattribute__(self, 'fmap')[name]
@@ -263,7 +273,8 @@ class BookInfo(object):
                     if len(v) == 0: continue
                     for x in v:
                         e = etree.Element(field.uri)
-                        e.text = unicode(x)
+                        if x is not None:
+                            e.text = unicode(x)
                         description.append(e)
                 else:
                     e = etree.Element(field.uri)
@@ -283,7 +294,7 @@ class BookInfo(object):
             if v is not None:
                 if field.multiple:
                     if len(v) == 0: continue
-                    v = [ unicode(x) for x in v if v is not None ]
+                    v = [ unicode(x) for x in v if x is not None ]
                 else:
                     v = unicode(v)
 
@@ -299,7 +310,7 @@ class BookInfo(object):
             if v is not None:
                 if field.multiple:
                     if len(v) == 0: continue
-                    v = [ unicode(x) for x in v if v is not None ]
+                    v = [ unicode(x) for x in v if x is not None ]
                 else:
                     v = unicode(v)
                 result[field.name] = v