Project

General

Profile

« Previous | Next » 

Revision 3541

schemas/py_functions.sql: _date(): Re-enabled now that exceptions thrown are properly handled. FormatException: Support raising parsable data_exceptions when provided with the value that was invalid. Date parsing mode: Return date as the value in FormatException so it can be filtered out automatically by column-based import.

View differences:

schemas/py_functions.sql
69 69
ALTER FUNCTION py_functions."__namePart"() OWNER TO bien;
70 70

  
71 71
--
72
-- Name: _date!(text, text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
72
-- Name: _date(text, text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
73 73
--
74 74

  
75
CREATE FUNCTION "_date!"(date text DEFAULT NULL::text, year text DEFAULT NULL::text, month text DEFAULT NULL::text, day text DEFAULT NULL::text) RETURNS text
75
CREATE FUNCTION _date(date text DEFAULT NULL::text, year text DEFAULT NULL::text, month text DEFAULT NULL::text, day text DEFAULT NULL::text) RETURNS text
76 76
    LANGUAGE plpythonu IMMUTABLE
77 77
    AS $$
78 78
global date, year, month, day
......
86 86

  
87 87
class ExceptionWithCause(Exception):
88 88
    def __init__(self, msg, cause):
89
        msg += ': '+e_str(cause)
89
        msg += '\nDETAIL:  '+e_str(cause)
90 90
        Exception.__init__(self, msg)
91 91

  
92 92
utc = dateutil.tz.tzutc()
......
105 105
    return dateutil.parser.parse(str_, default=default)
106 106

  
107 107
class FormatException(ExceptionWithCause):
108
    def __init__(self, cause):
109
        ExceptionWithCause.__init__(self, 'Invalid input value', cause)
108
    def __init__(self, cause, value=None):
109
        msg = 'invalid input value'
110
        if value != None: msg += ': "'+str(value)+'"'
111
        ExceptionWithCause.__init__(self, msg, cause)
110 112

  
111 113
if date != None:
112 114
    str_ = date
......
114 116
    except ValueError:
115 117
        try: date = strtotime(str_)
116 118
        except ImportError: return str_
117
        except ValueError, e: raise FormatException(e)
119
        except ValueError, e: raise FormatException(e, str_)
118 120
    else: date = (datetime.date(int(year), 1, 1) +
119 121
        datetime.timedelta(round((year % 1.)*365)))
120 122
else:
......
148 150
$$;
149 151

  
150 152

  
151
ALTER FUNCTION py_functions."_date!"(date text, year text, month text, day text) OWNER TO bien;
153
ALTER FUNCTION py_functions._date(date text, year text, month text, day text) OWNER TO bien;
152 154

  
153 155
--
154 156
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_functions; Owner: bien

Also available in: Unified diff