- def __repr__(self):
- return 'XMLNamespace(%r)' % self.uri
-
- def __str__(self):
- return '%s' % self.uri
-
-
-class BookInfo(object):
- RDF = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
- DC = XMLNamespace('http://purl.org/dc/elements/1.1/')
-
- mapping = {
- DC('creator') : ('author', str_to_person),
- DC('title') : ('title', str_to_unicode),
- DC('subject.period') : ('epoch', str_to_unicode),
- DC('subject.type') : ('kind', str_to_unicode),
- DC('subject.genre') : ('genre', str_to_unicode),
- DC('date') : ('created_at', str_to_date),
- DC('date.pd') : ('released_to_public_domain_at', str_to_date),
- DC('contributor.translator') : ('translator', str_to_person),
- DC('contributor.technical_editor') : ('technical_editor', str_to_person),
- DC('publisher') : ('publisher', str_to_unicode),
- DC('source') : ('source_name', str_to_unicode),
- DC('source.URL') : ('source_url', str_to_unicode),
- DC('identifier.url') : ('url', str_to_unicode),
- DC('relation.hasPart') : ('parts', str_to_unicode_list),
- DC('rights.license') : ('license', str_to_unicode),
- DC('rights') : ('license_description', str_to_unicode),
- }
+ def validate(self, fdict, fallbacks=None, strict=False):
+ if fallbacks is None:
+ fallbacks = {}
+ if self.uri not in fdict:
+ if not self.required:
+ # Accept single value for single fields and saliases.
+ if self.name in fallbacks:
+ if self.multiple:
+ f = fallbacks[self.name]
+ else:
+ f = [fallbacks[self.name]]
+ elif self.salias and self.salias in fallbacks:
+ f = [fallbacks[self.salias]]
+ else:
+ f = self.default
+ else:
+ raise ValidationError("Required field %s not found" % self.uri)
+ else:
+ f = fdict[self.uri]
+
+ return self.validate_value(f, strict=strict)
+
+ def __eq__(self, other):
+ if isinstance(other, Field) and other.name == self.name:
+ return True
+ return False
+
+
+class DCInfo(type):
+ def __new__(mcs, classname, bases, class_dict):
+ fields = list(class_dict['FIELDS'])
+
+ for base in bases[::-1]:
+ if hasattr(base, 'FIELDS'):
+ for field in base.FIELDS[::-1]:
+ try:
+ fields.index(field)
+ except ValueError:
+ fields.insert(0, field)
+
+ class_dict['FIELDS'] = tuple(fields)
+ return super(DCInfo, mcs).__new__(mcs, classname, bases, class_dict)
+
+
+class WorkInfo(object):
+ __metaclass__ = DCInfo
+
+ FIELDS = (
+ Field(DCNS('creator'), 'authors', as_person, salias='author', multiple=True),
+ Field(DCNS('title'), 'title'),
+ Field(DCNS('type'), 'type', required=False, multiple=True),
+
+ Field(DCNS('contributor.editor'), 'editors',
+ as_person, salias='editor', multiple=True, default=[]),
+ Field(DCNS('contributor.technical_editor'), 'technical_editors',
+ as_person, salias='technical_editor', multiple=True, default=[]),
+ Field(DCNS('contributor.funding'), 'funders', salias='funder', multiple=True, default=[]),
+ Field(DCNS('contributor.thanks'), 'thanks', required=False),
+
+ Field(DCNS('date'), 'created_at'),
+ Field(DCNS('date.pd'), 'released_to_public_domain_at', as_date, required=False),
+ Field(DCNS('publisher'), 'publisher'),
+
+ Field(DCNS('language'), 'language'),
+ Field(DCNS('description'), 'description', required=False),
+
+ Field(DCNS('source'), 'source_name', required=False),
+ Field(DCNS('source.URL'), 'source_url', required=False),
+ Field(DCNS('identifier.url'), 'url', WLURI, strict=as_wluri_strict),
+ Field(DCNS('rights.license'), 'license', required=False),
+ Field(DCNS('rights'), 'license_description'),
+
+ Field(PLMETNS('digitisationSponsor'), 'sponsors', multiple=True, default=[]),
+ Field(WLNS('digitisationSponsorNote'), 'sponsor_note', required=False),
+ Field(WLNS('developmentStage'), 'stage', required=False),
+ )