File: datetime_handler.py2

package info (click to toggle)
cerealizer 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 184 kB
  • ctags: 261
  • sloc: python: 359; makefile: 7
file content (93 lines) | stat: -rw-r--r-- 2,779 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Cerealizer
# Copyright (C) 2008 Steve Benson, Lann Martin
# Copyright (C) 2012 Jean-Baptiste LAMY
#
# This program is free software.
# It is available under the Python licence.


import datetime
import cerealizer

class DatetimeHandler(cerealizer.Handler):
  classname = 'datetime\n'
  
  def dump_obj(self, obj, dumper, s):
    assert issubclass(obj.__class__, datetime.datetime)
    
    # This works based on datetime.__reduce__():
    
    # datetime.__reduce__()[1] is a string of bytes that is an internal
    # representation of a datetime object (the data field
    # PyDateTime_DateTime struct, an unsigned char
    # _PyDateTime_DATETIME_DATASIZE bytes long). This should be platform
    # independent and is validated upon reconstruction of a datetime. 
    # Pickle uses it and guarantees backwards compatibility, so
    # presumably we can use it here too.
    
    # I haven't verified that tzinfo objects have the same properties,
    # which is why they're unsupported right now.

    # This string returned by __reduce__ is encoded because it can
    # return data that interferes with the cerealizer protocol.

    if obj.tzinfo != None:
      raise ValueError("DatetimeHandler doesn't yet know how to handle datetime objects with tzinfo.")
    
    s.write('%s%s\n' % (
      self.classname,  
      obj.__reduce__()[1][0].encode('string_escape')
    ))
  
  def undump_obj(self, dumper, s):
    line = s.readline()
    if len(line)>0 and line[-1]=='\n':
      line = line[:-1]
    
    return datetime.datetime(line.decode('string_escape'))


class DateHandler(cerealizer.Handler):
  classname = 'date\n'
  
  def dump_obj(self, obj, dumper, s):
    assert issubclass(obj.__class__, datetime.date)
    
    s.write('%s%s\n' % (
      self.classname,  
      obj.__reduce__()[1][0].encode('string_escape')
    ))
  
  def undump_obj(self, dumper, s):
    line = s.readline()
    if len(line) > 0 and line[-1] == '\n': line = line[:-1]
    return datetime.date(line.decode('string_escape'))



class TimeHandler(cerealizer.Handler):
  classname = 'time\n'
  
  def dump_obj(self, obj, dumper, s):
    assert issubclass(obj.__class__, datetime.time)
    
    if obj.tzinfo != None:
      raise ValueError("DatetimeHandler doesn't yet know how to handle datetime objects with tzinfo.")
    
    s.write('%s%s\n' % (
      self.classname,  
      obj.__reduce__()[1][0].encode('string_escape')
    ))
  
  def undump_obj(self, dumper, s):
    line = s.readline()
    if len(line)>0 and line[-1]=='\n':
      line = line[:-1]
    
    return datetime.time(line.decode('string_escape'))


cerealizer.register(datetime.datetime, DatetimeHandler())
cerealizer.register(datetime.date    , DateHandler    ())
cerealizer.register(datetime.time    , TimeHandler    ())