-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathncdu_data_writer.py
More file actions
71 lines (51 loc) · 1.74 KB
/
Copy pathncdu_data_writer.py
File metadata and controls
71 lines (51 loc) · 1.74 KB
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
from __future__ import absolute_import
import time
import ujson as json
class NcduDataWriter(object):
"""
Write ncdu formatted data files (like ncdu -o)
I couldn't find any good streaming JSON writers for python, so this is a bit hackish, but safe.
All JSON objects are written with json.dump(), the only stuff I write directly to the file are
ints, newlines, commas and square brackets
"""
def __init__(self, output, root):
"""
:type output: io.RawIOBase
:type root: str
"""
self.output = output
self.depth = 0
self.output.write('[1,0,')
json.dump({'progname': 'ncdu-s3', 'progver': '0.1', 'timestamp': int(time.time())}, self.output)
# ncdu data format must begin with a directory
self.dir_enter(root)
def __enter__(self):
return self
def __exit__(self, *_):
self.close()
def dir_enter(self, name):
"""
:type name: str
"""
self.depth += 1
self.output.write(",\n")
self.output.write('[')
json.dump({'name': name}, self.output)
def dir_leave(self):
if self.depth > 0:
self.depth -= 1
self.output.write(']')
def file_entry(self, name, size, last_modified):
"""
:type name: str
:type size: int
:type last_modified: datetime
"""
self.output.write(",\n")
unixtime = int(time.mktime(last_modified.timetuple()))
json.dump({'name': name, 'dsize': size, 'mtime': unixtime}, self.output)
def close(self):
for i in xrange(self.depth):
self.dir_leave()
# close the format JSON document we opened in our constructor
self.output.write(']')