From cbabbdaa7ab6c6be8cd22b77860331444ddf6119 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 30 Dec 2011 10:55:04 +0100 Subject: [PATCH] wrap class methods as functions when used as BookInfo validators --- librarian/dcparser.py | 16 ++++++++++------ librarian/picture.py | 6 +++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/librarian/dcparser.py b/librarian/dcparser.py index 21244ef..a0eaa04 100644 --- a/librarian/dcparser.py +++ b/librarian/dcparser.py @@ -70,7 +70,8 @@ def as_date(text): except ValueError, e: raise ValueError("Unrecognized date format. Try YYYY-MM-DD or YYYY.") -as_person = Person.from_text +def as_person(text): + return Person.from_text(text) def as_unicode(text): if isinstance(text, unicode): @@ -78,12 +79,15 @@ def as_unicode(text): else: return text.decode('utf-8') +def as_wluri_strict(text): + return WLURI.strict(text) + class Field(object): def __init__(self, uri, attr_name, validator=as_unicode, strict=None, multiple=False, salias=None, **kwargs): self.uri = uri self.name = attr_name - self.validator = lambda x: validator(x) - self.strict = lambda x: strict(x) + self.validator = validator + self.strict = strict self.multiple = multiple self.salias = salias @@ -166,7 +170,7 @@ class WorkInfo(object): Field( DCNS('source'), 'source_name', required=False), Field( DCNS('source.URL'), 'source_url', required=False), - Field( DCNS('identifier.url'), 'url', WLURI, strict=WLURI.strict), + Field( DCNS('identifier.url'), 'url', WLURI, strict=as_wluri_strict), Field( DCNS('rights.license'), 'license', required=False), Field( DCNS('rights'), 'license_description'), ) @@ -350,9 +354,9 @@ class BookInfo(WorkInfo): Field( DCNS('contributor.translator'), 'translators', \ as_person, salias='translator', multiple=True, default=[]), Field( DCNS('relation.hasPart'), 'parts', - WLURI, strict=WLURI.strict, multiple=True, required=False), + WLURI, strict=as_wluri_strict, multiple=True, required=False), Field( DCNS('relation.isVariantOf'), 'variant_of', - WLURI, strict=WLURI.strict, required=False), + WLURI, strict=as_wluri_strict, required=False), Field( DCNS('relation.cover_image.url'), 'cover_url', required=False), Field( DCNS('relation.cover_image.attribution'), 'cover_by', required=False), diff --git a/librarian/picture.py b/librarian/picture.py index ce1a551..ee3c61d 100644 --- a/librarian/picture.py +++ b/librarian/picture.py @@ -18,6 +18,9 @@ class WLPictureURI(WLURI): uri = 'http://wolnelektury.pl/katalog/obraz/%s/' % slug return cls(uri) +def as_wlpictureuri_strict(text): + return WLPictureURI.strict(text) + class PictureInfo(WorkInfo): """ @@ -33,7 +36,8 @@ class PictureInfo(WorkInfo): Field(DCNS('description.medium'), 'medium', required=False), Field(DCNS('description.dimensions'), 'original_dimensions', required=False), Field(DCNS('format'), 'mime_type', required=False), - Field(DCNS('identifier.url'), 'url', WLPictureURI, strict=WLPictureURI.strict), + Field(DCNS('identifier.url'), 'url', WLPictureURI, + strict=as_wlpictureuri_strict), ) -- 2.20.1