X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/121ab507bfffe249087df81a6dcdc368d9cb11f5..ae2d55ccb56e2634956ada31b51fbee9879cf837:/src/librarian/dcparser.py?ds=sidebyside diff --git a/src/librarian/dcparser.py b/src/librarian/dcparser.py index fe4b3fd..b89abd1 100644 --- a/src/librarian/dcparser.py +++ b/src/librarian/dcparser.py @@ -9,7 +9,7 @@ import six from librarian.util import roman_to_int from librarian import (ValidationError, NoDublinCore, ParseError, DCNS, RDFNS, - XMLNS, WLURI, WLNS, PLMETNS) + XMLNS, WLNS, PLMETNS) import lxml.etree as etree from lxml.etree import XMLSyntaxError @@ -17,16 +17,16 @@ from lxml.etree import XMLSyntaxError from librarian.meta.types.bool import BoolValue from librarian.meta.types.date import DateValue from librarian.meta.types.person import Person -from librarian.meta.types.text import TextValue +from librarian.meta.types.wluri import WLURI +from librarian.meta.types import text class Field(object): - def __init__(self, uri, attr_name, validator=TextValue, strict=None, + def __init__(self, uri, attr_name, value_type=text.TextValue, multiple=False, salias=None, **kwargs): self.uri = uri self.name = attr_name - self.validator = validator - self.strict = strict + self.value_type = value_type self.multiple = multiple self.salias = salias @@ -35,24 +35,12 @@ class Field(object): self.default = kwargs.get('default', [] if multiple else [None]) def validate_value(self, val, strict=False): - if strict and self.strict is not None: - validator = self.strict - else: - validator = self.validator + #if strict: + # value.validate() + try: if self.multiple: - if validator is None: - return val - new_values = [] - for v in val: - nv = v - if v is not None: - #nv = validator(v) - nv = v - if hasattr(v, 'lang'): - setattr(nv, 'lang', v.lang) - new_values.append(nv) - return new_values + return val elif len(val) > 1: raise ValidationError( "Multiple values not allowed for field '%s'" % self.uri @@ -63,13 +51,7 @@ class Field(object): % self.uri ) else: - if validator is None or val[0] is None: - return val[0] - #nv = validator(val[0]) - nv = val[0] - if hasattr(val[0], 'lang') and not hasattr(validator, 'no_lang'): - setattr(nv, 'lang', val[0].lang) - return nv + return val[0] except ValueError as e: raise ValidationError( "Field '%s' - invald value: %s" @@ -219,25 +201,25 @@ class WorkInfo(six.with_metaclass(DCInfo, object)): p = p.getparent() for e in desc.getchildren(): - field = cls.get_field_by_uri(e.tag) + tag = e.tag + if tag == 'meta': + meta_id = e.attrib.get('id') + if meta_id and meta_id.endswith('-id'): + tag = meta_id + + field = cls.get_field_by_uri(tag) if field is None: # Ignore unknown fields. - ### TODO: does it do for isbn? continue - fv = field_dict.get(e.tag, []) + fv = field_dict.get(tag, []) if e.text is not None: - val = field.validator(e.text) + val = field.value_type.from_text(e.text) val.lang = e.attrib.get(XMLNS('lang'), lang) - - if e.tag == 'meta': - meta_id = e.attrib.get('id') - if meta_id and meta_id.endswith('-id'): - field_dict[meta_id] = [val.replace('ISBN-', 'ISBN ')] else: val = e.text fv.append(val) - field_dict[e.tag] = fv + field_dict[tag] = fv return cls(desc.attrib, field_dict, *args, **kwargs) @@ -371,17 +353,17 @@ class WorkInfo(six.with_metaclass(DCInfo, object)): class BookInfo(WorkInfo): FIELDS = ( - Field(DCNS('audience'), 'audiences', salias='audience', multiple=True, + Field(DCNS('audience'), 'audiences', text.Audience, salias='audience', multiple=True, required=False), - Field(DCNS('subject.period'), 'epochs', salias='epoch', multiple=True, + Field(DCNS('subject.period'), 'epochs', text.Epoch, salias='epoch', multiple=True, required=False), - Field(DCNS('subject.type'), 'kinds', salias='kind', multiple=True, + Field(DCNS('subject.type'), 'kinds', text.Kind, salias='kind', multiple=True, required=False), - Field(DCNS('subject.genre'), 'genres', salias='genre', multiple=True, + Field(DCNS('subject.genre'), 'genres', text.Genre, salias='genre', multiple=True, required=False), - Field(WLNS('category.legimi'), 'legimi', required=False), - + Field(WLNS('category.legimi'), 'legimi', text.LegimiCategory, required=False), + Field(WLNS('category.thema'), 'thema', text.ThemaCategory, required=False, multiple=True), Field(DCNS('subject.location'), 'location', required=False), Field(DCNS('contributor.translator'), 'translators',