Project

General

Profile

1
--
2
-- PostgreSQL database dump
3
--
4

    
5
SET statement_timeout = 0;
6
SET client_encoding = 'UTF8';
7
SET standard_conforming_strings = off;
8
SET check_function_bodies = false;
9
SET client_min_messages = warning;
10
SET escape_string_warning = off;
11

    
12
--
13
-- Name: py_functions; Type: SCHEMA; Schema: -; Owner: bien
14
--
15

    
16
CREATE SCHEMA py_functions;
17

    
18

    
19
ALTER SCHEMA py_functions OWNER TO bien;
20

    
21
SET search_path = py_functions, pg_catalog;
22

    
23
--
24
-- Name: _date(text, text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
25
--
26

    
27
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
28
    LANGUAGE plpythonu IMMUTABLE
29
    AS $$
30
global date, year, month, day
31

    
32
import datetime
33
import dateutil.tz
34

    
35
def e_msg(e): return e.args[0].rstrip()
36

    
37
def e_str(e): return e.__class__.__name__+': '+e_msg(e)
38

    
39
def raise_invalid_value(cause, value):
40
    raise ValueError('invalid input value: "'+str(value)+'"\nDETAIL:  '
41
        +e_str(cause))
42

    
43
utc = dateutil.tz.tzutc()
44

    
45
def naive2utc(datetime_):
46
    assert datetime_.tzinfo == None
47
    return datetime_.replace(tzinfo=utc)
48

    
49
def from_timestamp(utc_timestamp):
50
    return naive2utc(datetime.datetime.utcfromtimestamp(utc_timestamp))
51

    
52
epoch = from_timestamp(0)
53

    
54
def strtotime(str_, default=epoch):
55
    import dateutil.parser
56
    return dateutil.parser.parse(str_, default=default)
57

    
58
if date != None:
59
    str_ = date
60
    try: year = float(str_)
61
    except ValueError:
62
        try: date = strtotime(str_)
63
        except ImportError: return str_
64
        except ValueError, e: raise_invalid_value(e, str_)
65
    else: date = (datetime.date(int(year), 1, 1) +
66
        datetime.timedelta(round((year % 1.)*365)))
67
else:
68
    # Year is required
69
    if year == None:
70
        if month == None and day == None: return None # entire date is empty
71
        else: raise AssertionError(
72
            'null value in column "year" violates not-null constraint')
73
    
74
    # Convert month name to number
75
    if month != None and not month.isdigit(): # month is name
76
        month = str(strtotime(month).month)
77
    
78
    if month == None: month = 1
79
    if day == None: day = 1
80
    year, month, day = map(int, (year, month, day))
81
    
82
    for try_num in xrange(2):
83
        try:
84
            date = datetime.date(year, month, day)
85
            break
86
        except ValueError, e:
87
            if try_num > 0: raise # exception still raised after retry
88
            msg = e_msg(e)
89
            if msg == 'month must be in 1..12': # try swapping month and day
90
                month, day = day, month
91
            else: raise
92

    
93
return str(date)
94
$$;
95

    
96

    
97
ALTER FUNCTION py_functions._date(date text, year text, month text, day text) OWNER TO bien;
98

    
99
--
100
-- Name: _dateRangeEnd(text); Type: FUNCTION; Schema: py_functions; Owner: bien
101
--
102

    
103
CREATE FUNCTION "_dateRangeEnd"(value text) RETURNS text
104
    LANGUAGE sql IMMUTABLE STRICT
105
    AS $_$
106
SELECT (py_functions.parse_date_range($1))[2]
107
$_$;
108

    
109

    
110
ALTER FUNCTION py_functions."_dateRangeEnd"(value text) OWNER TO bien;
111

    
112
--
113
-- Name: _dateRangeStart(text); Type: FUNCTION; Schema: py_functions; Owner: bien
114
--
115

    
116
CREATE FUNCTION "_dateRangeStart"(value text) RETURNS text
117
    LANGUAGE sql IMMUTABLE STRICT
118
    AS $_$
119
SELECT (py_functions.parse_date_range($1))[1]
120
$_$;
121

    
122

    
123
ALTER FUNCTION py_functions."_dateRangeStart"(value text) OWNER TO bien;
124

    
125
--
126
-- Name: _namePart(text, text, text); Type: FUNCTION; Schema: py_functions; Owner: bien
127
--
128

    
129
CREATE FUNCTION "_namePart"(first text DEFAULT NULL::text, middle text DEFAULT NULL::text, last text DEFAULT NULL::text) RETURNS text
130
    LANGUAGE plpythonu IMMUTABLE
131
    AS $$
132
params = dict(first=first, middle=middle, last=last)
133

    
134
_name_parts_slices_items = [
135
    ('first', slice(None, 1)),
136
    ('middle', slice(1, -1)),
137
    ('last', slice(-1, None)),
138
]
139
name_parts_slices = dict(_name_parts_slices_items)
140
name_parts = [name for name, slice_ in _name_parts_slices_items]
141

    
142
def _name(items):
143
    items = dict(items)
144
    parts = []
145
    for part in name_parts:
146
        if part in items: parts.append(items[part])
147
    return ' '.join(parts)
148

    
149
out_items = []
150
for part, value in params.iteritems():
151
    if value == None: continue
152
    
153
    try: slice_ = name_parts_slices[part]
154
    except KeyError: pass # a non-value column
155
    else: out_items.append((part, ' '.join(value.split(' ')[slice_])))
156

    
157
return _name(out_items)
158
$$;
159

    
160

    
161
ALTER FUNCTION py_functions."_namePart"(first text, middle text, last text) OWNER TO bien;
162

    
163
--
164
-- Name: parse_date_range(text); Type: FUNCTION; Schema: py_functions; Owner: bien
165
--
166

    
167
CREATE FUNCTION parse_date_range(str_ text) RETURNS text[]
168
    LANGUAGE plpythonu IMMUTABLE STRICT
169
    AS $$
170
import re
171

    
172
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
173

    
174
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
175

    
176
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
177

    
178
range_sep='-'
179
part_sep=' '
180

    
181
default = (str_, None)
182
# range_sep might be used as date part separator instead
183
if str_.find(part_sep) < 0: return default
184

    
185
start, sep, end = str_.partition(range_sep)
186
if sep == '': return default # not a range
187
start, end = (single_space(d).split(part_sep) for d in (start, end))
188

    
189
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
190
if len(start) == 2 and (len(end) == 1 or (
191
        len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
192
        and could_be_year(end[-1])
193
    )):
194
    end.insert(0, start[0]) # make end fully specified
195
ct_diff = len(end) - len(start)
196
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
197
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
198
# Other forms are invalid and will be left as-is
199

    
200
return [part_sep.join(d) for d in (start, end)]
201
$$;
202

    
203

    
204
ALTER FUNCTION py_functions.parse_date_range(str_ text) OWNER TO bien;
205

    
206
--
207
-- PostgreSQL database dump complete
208
--
209

    
(8-8/20)