##// END OF EJS Templates
separate single-date parsing from walking extraction...
MinRK -
Show More
@@ -37,7 +37,7 b' from IPython.core.profiledir import ProfileDir, ProfileDirError'
37 37
38 38 from IPython.utils.capture import RichOutput
39 39 from IPython.utils.coloransi import TermColors
40 from IPython.utils.jsonutil import rekey, extract_dates
40 from IPython.utils.jsonutil import rekey, extract_dates, parse_date
41 41 from IPython.utils.localinterfaces import localhost, is_local_ip
42 42 from IPython.utils.path import get_ipython_dir
43 43 from IPython.utils.py3compat import cast_bytes, string_types, xrange, iteritems
@@ -675,7 +675,7 b' class Client(HasTraits):'
675 675 if 'date' in parent:
676 676 md['submitted'] = parent['date']
677 677 if 'started' in msg_meta:
678 md['started'] = extract_dates(msg_meta['started'])
678 md['started'] = parse_date(msg_meta['started'])
679 679 if 'date' in header:
680 680 md['completed'] = header['date']
681 681 return md
@@ -1580,7 +1580,7 b' class Client(HasTraits):'
1580 1580 )
1581 1581 md.update(self._extract_metadata(md_msg))
1582 1582 if rec.get('received'):
1583 md['received'] = extract_dates(rec['received'])
1583 md['received'] = parse_date(rec['received'])
1584 1584 md.update(iodict)
1585 1585
1586 1586 if rcontent['status'] == 'ok':
@@ -1843,12 +1843,12 b' class Client(HasTraits):'
1843 1843 has_rbufs = result_buffer_lens is not None
1844 1844 for i,rec in enumerate(records):
1845 1845 # unpack datetime objects
1846 for dtkey in ('header', 'result_header',
1847 'submitted', 'started',
1848 'completed', 'received',
1849 ):
1846 for hkey in ('header', 'result_header'):
1847 if hkey in rec:
1848 rec[hkey] = extract_dates(rec[hkey])
1849 for dtkey in ('submitted', 'started', 'completed', 'received'):
1850 1850 if dtkey in rec:
1851 rec[dtkey] = extract_dates(rec[dtkey])
1851 rec[dtkey] = parse_date(rec[dtkey])
1852 1852 # relink buffers
1853 1853 if has_bufs:
1854 1854 blen = buffer_lens[i]
@@ -62,22 +62,34 b' def rekey(dikt):'
62 62 dikt[nk] = dikt.pop(k)
63 63 return dikt
64 64
65 def parse_date(s):
66 """parse an ISO8601 date string
67
68 If it is None or not a valid ISO8601 timestamp,
69 it will be returned unmodified.
70 Otherwise, it will return a datetime object.
71 """
72 if s is None:
73 return s
74 m = ISO8601_PAT.match(s)
75 if m:
76 # FIXME: add actual timezone support
77 # this just drops the timezone info
78 notz = m.groups()[0]
79 return datetime.strptime(notz, ISO8601)
80 return s
65 81
66 82 def extract_dates(obj):
67 83 """extract ISO8601 dates from unpacked JSON"""
68 84 if isinstance(obj, dict):
69 obj = dict(obj) # don't clobber
85 new_obj = {} # don't clobber
70 86 for k,v in iteritems(obj):
71 obj[k] = extract_dates(v)
87 new_obj[k] = extract_dates(v)
88 obj = new_obj
72 89 elif isinstance(obj, (list, tuple)):
73 90 obj = [ extract_dates(o) for o in obj ]
74 91 elif isinstance(obj, string_types):
75 m = ISO8601_PAT.match(obj)
76 if m:
77 # FIXME: add actual timezone support
78 # this just drops the timezone info
79 notz = m.groups()[0]
80 obj = datetime.strptime(notz, ISO8601)
92 obj = parse_date(obj)
81 93 return obj
82 94
83 95 def squash_dates(obj):
General Comments 0
You need to be logged in to leave comments. Login now