From: Marcin Koziej Date: Sat, 7 Dec 2013 17:17:06 +0000 (+0100) Subject: merging master X-Git-Tag: 1.7~121 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/32177c13e6f1f07741f559601064538a65ce00fd?hp=5b1dcc7d247996752fa566c7150a45037b068565 merging master --- diff --git a/librarian/__init__.py b/librarian/__init__.py index c46d5d1..bf15d13 100644 --- a/librarian/__init__.py +++ b/librarian/__init__.py @@ -65,6 +65,7 @@ class EmptyNamespace(XMLNamespace): return tag # some common namespaces we use +XMLNS = XMLNamespace('http://www.w3.org/XML/1998/namespace') RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/') XINS = XMLNamespace("http://www.w3.org/2001/XInclude") diff --git a/librarian/dcparser.py b/librarian/dcparser.py index 7418f70..a7215a1 100644 --- a/librarian/dcparser.py +++ b/librarian/dcparser.py @@ -10,12 +10,19 @@ import re from librarian.util import roman_to_int from librarian import (ValidationError, NoDublinCore, ParseError, DCNS, RDFNS, - WLURI) + XMLNS, WLURI) import lxml.etree as etree # ElementTree API using libxml2 from lxml.etree import XMLSyntaxError +class TextPlus(unicode): + pass + +class DatePlus(date): + pass + + # ============== # = Converters = # ============== @@ -63,26 +70,47 @@ class Person(object): return 'Person(last_name=%r, first_names=*%r)' % (self.last_name, self.first_names) def as_date(text): + """Dates for digitization of pictures. It seems we need the following: +ranges: '1350-1450', +centuries: "XVIII w.' +half centuries/decades: '2 poł. XVIII w.', 'XVII w., l. 20' +later-then: 'po 1450' +circa 'ok. 1813-1814', 'ok.1876-ok.1886 +turn: 1893/1894 +for now we will translate this to some single date losing information of course. + """ try: # check out the "N. poł X w." syntax if isinstance(text, str): text = text.decode("utf-8") - m = re.match(u"(?:([12]) *poł[.]? )?([MCDXVI]+) *w[.]?", text) + + century_format = u"(?:([12]) *poł[.]? +)?([MCDXVI]+) *w[.,]*(?: *l[.]? *([0-9]+))?" + vague_format = u"(?:po *|ok. *)?([0-9]{4})(-[0-9]{2}-[0-9]{2})?" + + m = re.match(century_format, text) + m2 = re.match(vague_format, text) if m: - - half = m.groups()[0] - if half is not None: + half = m.group(1) + decade = m.group(3) + century = roman_to_int(str(m.group(2))) + if half is not None: + if decade is not None: + raise ValueError("Bad date format. Cannot specify both half and decade of century") half = int(half) - else: - half = 1 - century = roman_to_int(str(m.groups()[1])) - t = ((century*100 + (half-1)*50), 1, 1) + t = ((century*100 + (half-1)*50), 1, 1) + else: + decade = int(decade or 0) + t = ((century*100 + decade), 1, 1) + elif m2: + year = m2.group(1) + mon_day = m2.group(2) + if mon_day: + t = time.strptime(year + mon_day, "%Y-%m-%d") + else: + t = time.strptime(year, '%Y') else: - text = re.sub(r"(po|ok[.]?) *", "", text) - try: - t = time.strptime(text, '%Y-%m-%d') - except ValueError: - t = time.strptime(re.split(r'[-/]', text)[0], '%Y') - return date(t[0], t[1], t[2]) + raise ValueError + + return DatePlus(t[0], t[1], t[2]) except ValueError, e: raise ValueError("Unrecognized date format. Try YYYY-MM-DD or YYYY.") @@ -93,7 +121,7 @@ def as_unicode(text): if isinstance(text, unicode): return text else: - return text.decode('utf-8') + return TextPlus(text.decode('utf-8')) def as_wluri_strict(text): return WLURI.strict(text) @@ -119,7 +147,15 @@ class Field(object): if self.multiple: if validator is None: return val - return [ validator(v) if v is not None else v for v in val ] + new_values = [] + for v in val: + nv = v + if v is not None: + nv = validator(v) + if hasattr(v, 'lang'): + setattr(nv, 'lang', v.lang) + new_values.append(nv) + return new_values elif len(val) > 1: raise ValidationError("Multiple values not allowed for field '%s'" % self.uri) elif len(val) == 0: @@ -127,7 +163,10 @@ class Field(object): else: if validator is None or val[0] is None: return val[0] - return validator(val[0]) + nv = validator(val[0]) + if hasattr(val[0], 'lang'): + setattr(nv, 'lang', val[0].lang) + return nv except ValueError, e: raise ValidationError("Field '%s' - invald value: %s" % (self.uri, e.message)) @@ -247,9 +286,23 @@ class WorkInfo(object): if desc is None: raise NoDublinCore("No DublinCore section found.") + lang = None + p = desc + while p is not None and lang is None: + lang = p.attrib.get(XMLNS('lang')) + p = p.getparent() + for e in desc.getchildren(): fv = field_dict.get(e.tag, []) - fv.append(e.text) + if e.text is not None: + text = e.text + if not isinstance(text, unicode): + text = text.decode('utf-8') + val = TextPlus(text) + val.lang = e.attrib.get(XMLNS('lang'), lang) + else: + val = e.text + fv.append(val) field_dict[e.tag] = fv return cls(desc.attrib, field_dict, *args, **kwargs) diff --git a/setup.py b/setup.py index b20040a..7bcbbc2 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def whole_tree(prefix, path): setup( name='librarian', - version='1.5.1', + version='1.6', description='Converter from WolneLektury.pl XML-based language to XHTML, TXT and other formats', author="Marek Stępniowski", author_email='marek@stepniowski.com', diff --git a/tests/files/text/asnyk_miedzy_nami_expected.txt b/tests/files/text/asnyk_miedzy_nami_expected.txt index 89e310a..650aabf 100644 --- a/tests/files/text/asnyk_miedzy_nami_expected.txt +++ b/tests/files/text/asnyk_miedzy_nami_expected.txt @@ -39,4 +39,4 @@ Tekst opracowany na podstawie: (Asnyk, Adam) El...y (1838-1897), Poezye, t. 3, Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa wykonana przez Bibliotekę Narodową z egzemplarza pochodzącego ze zbiorów BN. -Opracowanie redakcyjne i przypisy: Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska +Opracowanie redakcyjne i przypisy: Adam Fikcyjny, Aleksandra Sekuła, Olga Sutkowska. diff --git a/tests/test_dcparser.py b/tests/test_dcparser.py index a0845a0..b9a28f0 100644 --- a/tests/test_dcparser.py +++ b/tests/test_dcparser.py @@ -46,7 +46,13 @@ def test_serialize(): for fixture in get_all_fixtures('dcparser', '*.xml'): yield check_serialize, fixture -def test_as_date(): +def test_asdate(): assert_equals(dcparser.as_date(u"2010-10-03"), date(2010, 10, 03)) - assert_equals(dcparser.as_date("2011"), date(2011, 1, 1)) - assert_equals(dcparser.as_date("2 poł. XIX w."), date(1950, 1, 1)) + assert_equals(dcparser.as_date(u"2011"), date(2011, 1, 1)) + assert_equals(dcparser.as_date(u"2 poł. XIX w."), date(1950, 1, 1)) + assert_equals(dcparser.as_date(u"XVII w., l. 20"), date(1720, 1, 1)) + assert_equals(dcparser.as_date(u"po 1460"), date(1460, 1, 1)) + assert_equals(dcparser.as_date(u"ok. 1813-1814"), date(1813, 1, 1)) + assert_equals(dcparser.as_date(u"ok.1876-ok.1886"), date(1876, 1, 1)) + assert_equals(dcparser.as_date(u"1893/1894"), date(1893, 1, 1)) +