Beispiel #1
0
def convert_dates(data, prop, earliest):
    """Converts dates.

    Arguments:
    data Dict - Data for conversion.
    prop Str - Properties dividided with comma.
    earliest Bool - True - the function will set only the earliest date.
    False - the function will set all dates.

    Returns:
    Nothing, the replacement is done in place.
    """
    for p in prop.split(','):
        dates = []
        if exists(data, p):
            v = getprop(data, p)
            if not isinstance(v, dict):
                if is_year_range_list(v):
                    dates.append( {
                        "begin": v[0],
                        "end": v[-1],
                        "displayDate": "%s-%s" % (v[0], v[-1])
                    })
                else:
                    for s in (v if not isinstance(v, basestring) else [v]):
                        for part in s.split(";"):
                            display_date = remove_single_brackets_and_strip(
                                            part
                                            )
                            stripped = clean_date(
                                        remove_all_brackets_and_strip(part)
                                        )
                            if len(stripped) < 4:
                                continue
                            a, b = parse_date_or_range(stripped)
                            if b != DEFAULT_DATETIME_STR:
                                dates.append( {
                                        "begin": a,
                                        "end": b,
                                        "displayDate": display_date
                                    })
            else:
                # Already filled in, probably by mapper
                continue

            dates.sort(key=lambda d: d["begin"] if d["begin"] is not None
                                                else DEFAULT_DATETIME_STR)
            if dates:
                if earliest:
                    value_to_set = dates[0]
                else:
                    value_to_set = dates
                setprop(data, p, value_to_set)
            else:
                delprop(data, p)
Beispiel #2
0
def convert_dates(data, prop, earliest):
    """Converts dates.

    Arguments:
    data Dict - Data for conversion.
    prop Str - Properties dividided with comma.
    earliest Bool - True - the function will set only the earliest date.
    False - the function will set all dates.

    Returns:
    Nothing, the replacement is done in place.
    """
    for p in prop.split(','):
        dates = []
        if exists(data, p):
            v = getprop(data, p)
            if not isinstance(v, dict):
                if is_year_range_list(v):
                    dates.append( {
                        "begin": v[0],
                        "end": v[-1],
                        "displayDate": "%s-%s" % (v[0], v[-1])
                    })
                else:
                    for s in (v if not isinstance(v, basestring) else [v]):
                        for part in s.split(";"):
                            display_date = remove_single_brackets_and_strip(
                                            part
                                            )
                            stripped = clean_date(
                                        remove_all_brackets_and_strip(part)
                                        )
                            if len(stripped) < 4:
                                continue
                            a, b = parse_date_or_range(stripped)
                            if b != DEFAULT_DATETIME_STR:
                                dates.append( {
                                        "begin": a,
                                        "end": b,
                                        "displayDate": display_date
                                    })
            else:
                # Already filled in, probably by mapper
                continue

            dates.sort(key=lambda d: d["begin"] if d["begin"] is not None
                                                else DEFAULT_DATETIME_STR)
            if dates:
                if earliest:
                    value_to_set = dates[0]
                else:
                    value_to_set = dates
                setprop(data, p, value_to_set)
            else:
                delprop(data, p)
Beispiel #3
0
def test_parse_date_or_range():
    DATE_TESTS = {
        "ca. July 1896": ("1896-07", "1896-07"), # fuzzy dates
        "c. 1896": ("1896", "1896"), # fuzzy dates
        "c. 1890-95": ("1890", "1895"), # fuzzy date range
        "1999.11.01": ("1999-11-01", "1999-11-01"), # period delim
        "2012-02-31": ("2012-03-02", "2012-03-02"), # invalid date cleanup
        "12-19-2010": ("2010-12-19", "2010-12-19"), # M-D-Y
        "5/7/2012": ("2012-05-07", "2012-05-07"), # slash delim MDY
        "1999 - 2004": ("1999", "2004"), # year range
        "1999-2004": ("1999", "2004"), # year range without spaces
        " 1999 - 2004 ": ("1999", "2004"), # range whitespace
    }
    for i in DATE_TESTS:
        i = clean_date(i)
        res = parse_date_or_range(i)
        assert res == DATE_TESTS[i], "For input '%s', expected '%s' but got '%s'"%(i,DATE_TESTS[i],res)
Beispiel #4
0
def test_parse_date_or_range():
    DATE_TESTS = {
        "ca. July 1896": ("1896-07", "1896-07"), # fuzzy dates
        "c. 1896": ("1896", "1896"), # fuzzy dates
        "c. 1890-95": ("1890", "1895"), # fuzzy date range
        "1999.11.01": ("1999-11-01", "1999-11-01"), # period delim
        "2012-02-31": ("2012-03-02", "2012-03-02"), # invalid date cleanup
        "12-19-2010": ("2010-12-19", "2010-12-19"), # M-D-Y
        "5/7/2012": ("2012-05-07", "2012-05-07"), # slash delim MDY
        "1999 - 2004": ("1999", "2004"), # year range
        "1999-2004": ("1999", "2004"), # year range without spaces
        " 1999 - 2004 ": ("1999", "2004"), # range whitespace
    }
    for i in DATE_TESTS:
        i = clean_date(i)
        res = parse_date_or_range(i)
        assert res == DATE_TESTS[i], "For input '%s', expected '%s' but got '%s'"%(i,DATE_TESTS[i],res)