-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_backup.py
44 lines (39 loc) · 1.1 KB
/
node_backup.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
import time
import base64
import textwrap
p = c.p
if not p.h.startswith('@bk '):
nd = g.findNodeAnywhere(c, '@backup-nodes')
if nd:
nd = nd.insertAsNthChild(0)
else:
nd = p.insertAfter()
ts = time.strftime("%Y-%m-%d %H:%M:%S")
nd.h = "@bk %s %s" % (p.h, ts)
s = '\n'.join(textwrap.wrap(
base64.b64encode(p.b),
break_long_words=True
))
nd.b = "# backup %s - %s\n# %s\n\n%s" % (
ts, p.h, p.get_UNL(with_proto=True), s)
g.es("%s - backed up"%ts)
c.redraw()
c.bodyWantsFocusNow()
else:
hdr, s = p.b.split('\n\n', 1)
if hdr.startswith("# backup"): # decode
hdr = hdr.replace('backup', 'BACKUP')
p.b = "%s\n\n%s" % (
hdr,
base64.b64decode(s.strip())
)
else: # encode again
hdr = hdr.replace('BACKUP', 'backup')
s = '\n'.join(textwrap.wrap(
base64.b64encode(s.strip()),
break_long_words=True
))
p.b = "%s\n\n%s" % (hdr, s)
c.redraw()
c.frame.body.setInsertPoint(0)
c.bodyWantsFocusNow()