Package rdfalchemy :: Module Literal'
[hide private]
[frames] | no frames]

Source Code for Module rdfalchemy.Literal'

  1  #!/usr/bin/env python 
  2  # encoding: utf-8 
  3  """ 
  4  Literal.py 
  5   
  6  Created by Philip Cooper on 2008-02-09. 
  7  Copyright (c) 2008 Openvest. All rights reserved. 
  8  """ 
  9  from rdflib import Namespace, Literal 
 10  from rdflib.Literal import bind as bindLiteral 
 11  from rdflib.Literal import _PythonToXSD 
 12  import re 
 13  import logging 
 14   
 15  XSD = 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") 
 22  if 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 35 try: 36 from decimal import Decimal 37 bindLiteral(XSD.decimal,Decimal) 38 _PythonToXSD.update(dict([(Decimal,(str,XSD.decimal))])) 39 except: 40 pass 41 42 ################################################################################ 43 ## Default behavior returns untyped literals as literals 44 ## this brings untyped literals back as unicode strings 45 bindLiteral(None,unicode) 46 47 ################################################################################ 48 ## Default behavior returns string literals as literals 49 ## this brings string literals back as unicode strings 50 bindLiteral(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" 58 import datetime 59 60 date_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
86 -def _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 114 bindLiteral(XSD.dateTime,_strToDateTime) 115