this repo has no description
1import json, sys
2
3 ########################### LOGFILES #########################
4sChunkSep = '---' ## A single line like this separates chunks in a logfile
5
6##############################################################################################
7###################### Logfile I/O #####################
8##############################################################################################
9## PROBLEM: JSON does not allow appending.
10## WORKAROUND: separate [JSON] chunks by '---', inspired by https://github.com/pvorb/jsml.
11## Writing an arbitrary chunk: ensure end-of-line before separator
12def writeLogChunk( wf, ch ):
13 ss = ch.__str__()
14 wf.write( ss )
15 if 0<len(ss) and not ss.endswith( '\n' ):
16 wf.write( '\n' )
17 wf.write( sChunkSep + '\n' )
18
19## Reading an arbitrary log chunk as a string
20def readLogChunk( rf ):
21 ch = ''
22 while True:
23 line = rf.readline()
24 if 0==len(line) or sChunkSep==line.strip():
25 return ch
26 ch += line
27
28## Would return empty from empty chunk even when the original file continues
29def readLogJSONChunk( rf ):
30 chJ = None
31 ch = readLogChunk( rf )
32 if 0<len(ch):
33 try:
34 chJ = json.loads( ch )
35 except:
36 print("\n WARNING: failed to parse JSON chunk '", ch,
37 "' in file pos ", rf.tell(), ". SYSINFO: ", sys.exc_info()[0]
38 , sep='')
39 raise
40 return chJ
41