Created
September 15, 2014 12:23
-
-
Save tarruda/d89adc85913a2b634ec1 to your computer and use it in GitHub Desktop.
Nvim persistence service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3, msgpack | |
class NvimPersistenceService(object): | |
def __init__(self, vim): | |
self.connection = sqlite3.connect('.nvim-persisted.db') | |
with self.connection as c: | |
c.execute(''' | |
CREATE TABLE IF NOT EXISTS nvim | |
( | |
key TEXT PRIMARY KEY, | |
value BLOB | |
) | |
''') | |
put_fn = '\n'.join(['fun! Put(key, value)', | |
' return rpcrequest(%d, "put", a:key, a:value)', | |
'endfun']) % vim.channel_id | |
get_fn = '\n'.join(['fun! Get(key)', | |
' return rpcrequest(%d, "get", a:key)', | |
'endfun']) % vim.channel_id | |
del_fn = '\n'.join(['fun! Del(key)', | |
' return rpcrequest(%d, "del", a:key)', | |
'endfun']) % vim.channel_id | |
vim.command(put_fn) | |
vim.command(get_fn) | |
vim.command(del_fn) | |
def put(self, key, value): | |
stmt = 'REPLACE INTO nvim (key, value) VALUES (?, ?)' | |
with self.connection as c: | |
c.execute(stmt, (key, buffer(msgpack.packb(value)))) | |
def get(self, key): | |
stmt = 'SELECT value FROM nvim WHERE key = ?' | |
with self.connection as c: | |
for row in c.execute(stmt, (key,)): | |
return msgpack.unpackb(row[0]) | |
def delete(self, key): | |
stmt = 'DELETE FROM nvim WHERE key = ?' | |
with self.connection as c: | |
c.execute(stmt, (key,)) | |
def on_plugin_teardown(self): | |
self.connection.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment