-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazynbt.py
46 lines (45 loc) · 1.32 KB
/
lazynbt.py
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
import nbt
import os
class File(object):
def __init__(self, filename, pointer=None):
self.filename = filename
self.cache = None
self.pointer = pointer if pointer else []
self.mtime = 0
def _read(self):
mtime = os.path.getmtime(self.filename)
if mtime > self.mtime:
self.cache = nbt.read(self.filename)
self.mtime = mtime
def _write(self):
nbt.write(self.filename, self.cache)
def __repr__(self):
return "lazynbt.File('" + self.filename + "', " + str(self.pointer) + ")"
def prettyprint(self):
self._read()
ret = self.cache
for thing in self.pointer:
ret = ret[thing]
ret.prettyprint()
def __str__(self):
self._read()
ret = self.cache
for thing in self.pointer:
ret = ret[thing]
return str(ret)
def __getitem__(self, item):
self._read()
return File(self.filename, self.pointer + [item])
def __setitem__(self, item, value):
self._read()
obj = self.cache
for thing in self.pointer:
obj = obj[thing]
obj[item] = value
self._write()
def value(self):
self._read()
ret = self.cache
for thing in self.pointer:
ret = ret[thing]
return ret