From 3e1e503e9fe0a2ad1fe7becb8102e3f760376794 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20St=C4=99pniowski?= Date: Sat, 6 Sep 2008 15:25:17 +0200 Subject: [PATCH] Moved Person class from dcparser.person to dcparser.converters and removed module dcparser.person. Added second argument (previous value) to converters. --- lib/dcparser/__init__.py | 2 +- lib/dcparser/converters.py | 28 ++++++++++++++++++++++++---- lib/dcparser/dcparser.py | 14 ++------------ lib/dcparser/person.py | 24 ------------------------ 4 files changed, 27 insertions(+), 41 deletions(-) delete mode 100644 lib/dcparser/person.py diff --git a/lib/dcparser/__init__.py b/lib/dcparser/__init__.py index 793f5270d..6912580df 100644 --- a/lib/dcparser/__init__.py +++ b/lib/dcparser/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- from dcparser import parse, ParseError -from person import Person +from converters import Person diff --git a/lib/dcparser/converters.py b/lib/dcparser/converters.py index 773aeddcb..b49c41735 100644 --- a/lib/dcparser/converters.py +++ b/lib/dcparser/converters.py @@ -3,14 +3,34 @@ from datetime import date import time import re -from person import Person + +class Person(object): + """Single person with last name and a list of first names.""" + def __init__(self, last_name, *first_names): + self.last_name = last_name + self.first_names = first_names + + + def __eq__(self, right): + return self.last_name == right.last_name and self.first_names == right.first_names + + + def __unicode__(self): + if len(self.first_names) > 0: + return '%s, %s' % (self.last_name, ' '.join(self.first_names)) + else: + return self.last_name + + + def __repr__(self): + return 'Person(last_name=%r, first_names=*%r)' % (self.last_name, self.first_names) -def str_to_unicode(value): +def str_to_unicode(value, previous): return unicode(value) -def str_to_person(value): +def str_to_person(value, previous): comma_count = value.count(',') if comma_count == 0: @@ -24,7 +44,7 @@ def str_to_person(value): return Person(last_name.strip(), *first_names) -def str_to_date(value): +def str_to_date(value, previous): try: t = time.strptime(value, '%Y-%m-%d') except ValueError: diff --git a/lib/dcparser/dcparser.py b/lib/dcparser/dcparser.py index e8a733a73..5df7700c1 100644 --- a/lib/dcparser/dcparser.py +++ b/lib/dcparser/dcparser.py @@ -13,17 +13,14 @@ except ImportError: import converters - __all__ = ('parse', 'ParseError') - class ParseError(Exception): def __init__(self, message): super(self, Exception).__init__(message) - class XMLNamespace(object): '''Represents XML namespace.''' @@ -37,13 +34,12 @@ class XMLNamespace(object): return tag.startswith(str(self)) def __repr__(self): - return 'NS(%r)' % self.uri + 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/') @@ -63,13 +59,10 @@ class BookInfo(object): DC('source.URL') : ('source_url', converters.str_to_unicode), } - @classmethod def from_string(cls, xml): - """docstring for from_string""" from StringIO import StringIO return cls.from_file(StringIO(xml)) - @classmethod def from_file(cls, xml_file): @@ -89,15 +82,13 @@ class BookInfo(object): return book_info - def parse_element(self, element): try: attribute, converter = self.mapping[element.tag] - setattr(self, attribute, converter(element.text)) + setattr(self, attribute, converter(element.text, getattr(self, attribute, None))) except KeyError: pass - def to_xml(self): """XML representation of this object.""" ET._namespace_map[str(self.RDF)] = 'rdf' @@ -118,4 +109,3 @@ class BookInfo(object): def parse(file_name): return BookInfo.from_file(file_name) - diff --git a/lib/dcparser/person.py b/lib/dcparser/person.py deleted file mode 100644 index 16412f768..000000000 --- a/lib/dcparser/person.py +++ /dev/null @@ -1,24 +0,0 @@ -# -*- coding: utf-8 -*- - - -class Person(object): - """Single person with last name and a list of first names.""" - def __init__(self, last_name, *first_names): - self.last_name = last_name - self.first_names = first_names - - - def __eq__(self, right): - return self.last_name == right.last_name and self.first_names == right.first_names - - - def __unicode__(self): - if len(self.first_names) > 0: - return '%s, %s' % (self.last_name, ' '.join(self.first_names)) - else: - return self.last_name - - - def __repr__(self): - return 'Person(last_name=%r, first_names=*%r)' % (self.last_name, self.first_names) - -- 2.20.1