X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/3618eb351066fde7992f64c81a053d0a89db2235..e180078f82a3d7e73857a9544b3b7fdfd475f93f:/lib/dcparser/converters.py?ds=inline diff --git a/lib/dcparser/converters.py b/lib/dcparser/converters.py new file mode 100644 index 000000000..773aeddcb --- /dev/null +++ b/lib/dcparser/converters.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +from datetime import date +import time +import re + +from person import Person + + +def str_to_unicode(value): + return unicode(value) + + +def str_to_person(value): + comma_count = value.count(',') + + if comma_count == 0: + last_name, first_names = value, [] + elif comma_count == 1: + last_name, first_names = value.split(',') + first_names = [name for name in first_names.split(' ') if len(name)] + else: + raise ValueError("value contains more than one comma: %r" % value) + + return Person(last_name.strip(), *first_names) + + +def str_to_date(value): + try: + t = time.strptime(value, '%Y-%m-%d') + except ValueError: + t = time.strptime(value, '%Y') + return date(t[0], t[1], t[2]) + +