root / rdfalchemy / trunk / rdfalchemy / Literal.py

Revision 127, 4.0 kB (checked in by phil, 7 months ago)

This update to rdfalchemy moves some functions in to a sub-module and has the beginings of a sparql script
It adds some unit tests (for initBinding)

Breaking your Programs:

This update could break your code if you:


from rdfalchemy.sesame2 import SesameGraph?


you now need to import from rdfalchemy.sparql.sesame2
allows for jython or other sesame graph implimentations not dependent on the http sparql endpoint


you should probably be using create_engine instead

Line 
1#!/usr/bin/env python
2# encoding: utf-8
3"""
4Literal.py
5
6Created by Philip Cooper on 2008-02-09.
7Copyright (c) 2008 Openvest. All rights reserved.
8"""
9from rdflib import Namespace, Literal
10from rdflib.Literal import bind as bindLiteral
11from rdflib.Literal import _PythonToXSD
12import re
13import logging
14
15XSD = Namespace(u'http://www.w3.org/2001/XMLSchema#')
16
17################################################################################
18# Let's fix the logging.  This seems like a lot of work...
19# all that it does is not log rebinding errors.
20# We know already about them ... and the warnings confuse the citizenry.
21_log = logging.getLogger("rdflib")
22if not _log.handlers:
23    class rebindingLogFilter(logging.Filter):
24        def filter(self, record):
25            if record.getMessage().find("Rebinding") > -1:
26                return False
27            return True
28   
29    h = logging.StreamHandler()
30    h.addFilter(rebindingLogFilter())
31    _log.addHandler(h)
32   
33################################################################################
34## Let's make toPython return a Decimal if an XSD.decimal in in the triplestore
35try:
36    from decimal import Decimal
37    bindLiteral(XSD.decimal,Decimal)
38    _PythonToXSD.update(dict([(Decimal,(str,XSD.decimal))]))
39except:
40    pass
41   
42################################################################################
43## Default behavior returns untyped literals as literals
44## this brings untyped literals back as unicode strings
45bindLiteral(None,unicode)
46
47################################################################################
48## Default behavior returns string literals as literals
49## this brings  string literals back as unicode strings
50bindLiteral(XSD.string,unicode)
51       
52################################################################################
53## Let's make toPython return a datetime if the literal has fractional seconds
54## Note: dateparser adapted from http://www.mnot.net/python/isodate.py
55## modified to: handle fractional seconds beyond tenths
56##              and to allow pseudo iso i.e. "2001-12-15 22:43:46"
57##                                        vs "2001-12-15T22:43:46"
58import datetime
59
60date_parser = re.compile(r"""^
61    (?P<year>\d{4})
62    (?:-
63        (?P<month>\d{1,2})
64        (?:-
65            (?P<day>\d{1,2})
66            (?:[T ]
67                (?P<hour>\d{1,2})
68                :
69                (?P<minute>\d{1,2})
70                (?::
71                    (?P<second>\d{1,2})
72                    (?P<dec_second>\.\d+)?
73                )?                   
74                (?:Z|(?:
75                        (?P<tz_sign>[+-])
76                        (?P<tz_hour>\d{1,2})
77                        :?
78                        (?P<tz_min>\d{2,2})
79                     )
80                )?
81            )?
82        )?
83    )?
84$""", re.VERBOSE)
85
86def _strToDateTime(s):
87        """ parse a string and return a datetime object. """
88        assert isinstance(s, basestring)
89        r = date_parser.search(s)
90        try:
91            a = r.groupdict('0')
92        except:
93            raise ValueError, 'invalid date string format'
94           
95        dt = datetime.datetime(int(a['year']),
96                               int(a['month']) or 1,
97                               int(a['day']) or 1,
98                               # If not given these will default to 00:00:00.0
99                               int(a['hour']),
100                               int(a['minute']),
101                               int(a['second']),
102                               # Convert into microseconds
103                               int(float(a['dec_second'])*1000000),
104                               )
105        tz_hours_offset = int(a['tz_hour'])
106        tz_mins_offset = int(a['tz_min'])
107        if a.get('tz_sign', '+') == "-":
108            return dt + datetime.timedelta(hours = tz_hours_offset,
109                                           minutes = tz_mins_offset)
110        else:
111            return dt - datetime.timedelta(hours = tz_hours_offset,
112                                           minutes = tz_mins_offset)
113
114bindLiteral(XSD.dateTime,_strToDateTime)
Note: See TracBrowser for help on using the browser.