aboutsummaryrefslogtreecommitdiff
path: root/scripts/shadacat.py
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2015-10-16 01:54:07 -0400
committerJustin M. Keyes <justinkz@gmail.com>2015-10-16 01:54:07 -0400
commit3a970e57dfd48f090f8ccc21567b7974e13d4c68 (patch)
tree5aeaf1cd7a85c17b29276eee88e9881f56ea134c /scripts/shadacat.py
parenta3f048ee06dea15490d7b874d295c3fc850cdc51 (diff)
parentdb6cba7d5759e02379005702c7a9d760137f4389 (diff)
downloadrneovim-3a970e57dfd48f090f8ccc21567b7974e13d4c68.tar.gz
rneovim-3a970e57dfd48f090f8ccc21567b7974e13d4c68.tar.bz2
rneovim-3a970e57dfd48f090f8ccc21567b7974e13d4c68.zip
Merge pull request #2506 from ZyX-I/shada
Replace viminfo with ShaDa files
Diffstat (limited to 'scripts/shadacat.py')
-rwxr-xr-xscripts/shadacat.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/scripts/shadacat.py b/scripts/shadacat.py
new file mode 100755
index 0000000000..4ff493bfbc
--- /dev/null
+++ b/scripts/shadacat.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3.4
+
+import os
+import sys
+import codecs
+
+from enum import Enum
+from datetime import datetime
+from functools import reduce
+
+import msgpack
+
+
+class EntryTypes(Enum):
+ Unknown = -1
+ Missing = 0
+ Header = 1
+ SearchPattern = 2
+ SubString = 3
+ HistoryEntry = 4
+ Register = 5
+ Variable = 6
+ GlobalMark = 7
+ Jump = 8
+ BufferList = 9
+ LocalMark = 10
+ Change = 11
+
+
+def strtrans_errors(e):
+ if not isinstance(e, UnicodeDecodeError):
+ raise NotImplementedError('don’t know how to handle {0} error'.format(
+ e.__class__.__name__))
+ return '<{0:x}>'.format(reduce((lambda a, b: a*0x100+b),
+ list(e.object[e.start:e.end]))), e.end
+
+
+codecs.register_error('strtrans', strtrans_errors)
+
+
+def idfunc(o):
+ return o
+
+
+class CharInt(int):
+ def __repr__(self):
+ return super(CharInt, self).__repr__() + ' (\'%s\')' % chr(self)
+
+
+ctable = {
+ bytes: lambda s: s.decode('utf-8', 'strtrans'),
+ dict: lambda d: dict((mnormalize(k), mnormalize(v)) for k, v in d.items()),
+ list: lambda l: list(mnormalize(i) for i in l),
+ int: lambda n: CharInt(n) if 0x20 <= n <= 0x7E else n,
+}
+
+
+def mnormalize(o):
+ return ctable.get(type(o), idfunc)(o)
+
+
+fname = sys.argv[1]
+poswidth = len(str(os.stat(fname).st_size or 1000))
+
+
+with open(fname, 'rb') as fp:
+ unpacker = msgpack.Unpacker(file_like=fp, read_size=1)
+ max_type = max(typ.value for typ in EntryTypes)
+ while True:
+ try:
+ pos = fp.tell()
+ typ = unpacker.unpack()
+ except msgpack.OutOfData:
+ break
+ else:
+ timestamp = unpacker.unpack()
+ time = datetime.fromtimestamp(timestamp)
+ length = unpacker.unpack()
+ if typ > max_type:
+ entry = fp.read(length)
+ typ = EntryTypes.Unknown
+ else:
+ entry = unpacker.unpack()
+ typ = EntryTypes(typ)
+ print('%*u %13s %s %5u %r' % (
+ poswidth, pos, typ.name, time.isoformat(), length, mnormalize(entry)))