# -*- coding: utf-8 -*-
from dcparser import parse, ParseError
-from person import Person
+from converters import Person
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:
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:
import converters
-
__all__ = ('parse', 'ParseError')
-
class ParseError(Exception):
def __init__(self, message):
super(self, Exception).__init__(message)
-
class XMLNamespace(object):
'''Represents XML namespace.'''
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/')
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):
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'
def parse(file_name):
return BookInfo.from_file(file_name)
-
+++ /dev/null
-# -*- 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)
-