Skip to content

Commit a11cdb0

Browse files
committed
add a few more options
1 parent 447bb2e commit a11cdb0

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

contrib/disk-queue-info.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
4+
35
import os
46
import argparse
57
import json
@@ -14,7 +16,9 @@ def get_args(*a):
1416
parser.add_argument('-e', '--evil-decode', action='store_true',
1517
help='attempt to decode all json found in queue (implies -p) and pretty-print them')
1618
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')
1822
args = parser.parse_args(*a)
1923
if args.with_color:
2024
args.evil_decode = True
@@ -30,15 +34,21 @@ def evil_decode(docbytes):
3034
decoder = json.JSONDecoder()
3135
idx = WHITESPACE.match(docbytes, 0).end()
3236
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
3644

37-
def read_entries(dirname, evil=False, color=False):
45+
def read_entries(dirname, evil=False, color=False, meta=False):
3846
dq = DiskQueue(dirname)
39-
for item in dq.iter_peek():
47+
for item,meta in dq.iter_peek():
4048
if evil:
4149
for obj in evil_decode(item):
50+
if meta:
51+
obj['_META_'] = meta
4252
obj = json.dumps(obj, sort_keys=True, indent=2)
4353
if color:
4454
try:
@@ -49,6 +59,8 @@ def read_entries(dirname, evil=False, color=False):
4959
pass
5060
print(obj)
5161
else:
62+
if meta:
63+
print('# ', json.dumps(meta))
5264
print(item)
5365

5466
def main(args):
@@ -57,11 +69,11 @@ def main(args):
5769
if not os.path.isdir(dirname):
5870
raise Exception("directory does not exist")
5971
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)
6173
else:
6274
show_info(dirname)
6375
except Exception as e:
64-
print("# unable to read {}: {}".format(dirname, e))
76+
print("# Exception while reading {}: {}".format(dirname, repr(e)))
6577

6678
if __name__ == '__main__':
6779
try:

hubblestack/hec/dq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def iter_peek(self):
169169
''' iterate and return all items in the disk queue (without removing any) '''
170170
for fname in self.files:
171171
with open(fname, 'rb') as fh:
172-
yield self.decompress(fh.read())
172+
yield self.decompress(fh.read()), self.read_meta(fname)
173173

174174
def get(self):
175175
""" get the next item from the queue

0 commit comments

Comments
 (0)