1
1
#!/usr/bin/env python
2
2
3
+ from __future__ import print_function
4
+
3
5
import os
4
6
import argparse
5
7
import json
@@ -14,7 +16,9 @@ def get_args(*a):
14
16
parser .add_argument ('-e' , '--evil-decode' , action = 'store_true' ,
15
17
help = 'attempt to decode all json found in queue (implies -p) and pretty-print them' )
16
18
parser .add_argument ('-c' , '--with-color' , action = 'store_true' ,
17
- help = 'attempt to pull in pygments and colorize the output of -e (implies -pe)' )
19
+ help = 'attempt to pull in pygments and colorize the output of -e (implies -p and -e)' )
20
+ parser .add_argument ('-m' , '--show-meta' , action = 'store_true' ,
21
+ help = 'attempt to show meta-data as a comment in peek mode or a spurious _META_ field in evil mode' )
18
22
args = parser .parse_args (* a )
19
23
if args .with_color :
20
24
args .evil_decode = True
@@ -30,15 +34,21 @@ def evil_decode(docbytes):
30
34
decoder = json .JSONDecoder ()
31
35
idx = WHITESPACE .match (docbytes , 0 ).end ()
32
36
while idx < len (docbytes ):
33
- obj , end = decoder .raw_decode (docbytes , idx )
34
- yield obj
35
- idx = WHITESPACE .match (docbytes , end ).end ()
37
+ try :
38
+ obj , end = decoder .raw_decode (docbytes , idx )
39
+ yield obj
40
+ idx = WHITESPACE .match (docbytes , end ).end ()
41
+ except ValueError as e :
42
+ print ('docbytes:\n ' , docbytes )
43
+ raise
36
44
37
- def read_entries (dirname , evil = False , color = False ):
45
+ def read_entries (dirname , evil = False , color = False , meta = False ):
38
46
dq = DiskQueue (dirname )
39
- for item in dq .iter_peek ():
47
+ for item , meta in dq .iter_peek ():
40
48
if evil :
41
49
for obj in evil_decode (item ):
50
+ if meta :
51
+ obj ['_META_' ] = meta
42
52
obj = json .dumps (obj , sort_keys = True , indent = 2 )
43
53
if color :
44
54
try :
@@ -49,6 +59,8 @@ def read_entries(dirname, evil=False, color=False):
49
59
pass
50
60
print (obj )
51
61
else :
62
+ if meta :
63
+ print ('# ' , json .dumps (meta ))
52
64
print (item )
53
65
54
66
def main (args ):
@@ -57,11 +69,11 @@ def main(args):
57
69
if not os .path .isdir (dirname ):
58
70
raise Exception ("directory does not exist" )
59
71
if args .peek :
60
- read_entries (dirname , evil = args .evil_decode , color = args .with_color )
72
+ read_entries (dirname , evil = args .evil_decode , color = args .with_color , meta = args . show_meta )
61
73
else :
62
74
show_info (dirname )
63
75
except Exception as e :
64
- print ("# unable to read {}: {}" .format (dirname , e ))
76
+ print ("# Exception while reading {}: {}" .format (dirname , repr ( e ) ))
65
77
66
78
if __name__ == '__main__' :
67
79
try :
0 commit comments