VimwikiTask: Wrap UUID in a ShortUUID object to allow simple compression

This commit is contained in:
Tomas Babej 2015-03-27 23:29:45 +01:00
parent ccfd8d3883
commit 0391c7fcd4
2 changed files with 26 additions and 1 deletions

View file

@ -17,10 +17,13 @@ class TaskCache(object):
def __getitem__(self, key):
# String keys refer to the Task objects
if type(key) in (str, unicode):
key = vwtask.ShortUUID(key)
if type(key) == vwtask.ShortUUID:
task = self.task_cache.get(key)
if task is None:
task = self.tw.tasks.get(uuid=key)
task = self.tw.tasks.get(uuid=key.value)
self.task_cache[key] = task
return task
@ -42,6 +45,7 @@ class TaskCache(object):
def __setitem__(self, key, value):
# String keys refer to the Task objects
if type(key) in (str, unicode):
key = vwtask.ShortUUID(key)
self.task_cache[key] = value
# Integer keys (line numbers) refer to the VimwikiTask objects

View file

@ -16,6 +16,26 @@ def convert_priority_to_tw_format(priority):
return {0: None, 1: 'L', 2: 'M', 3: 'H'}[priority]
class ShortUUID(object):
def __init__(self, value):
# Use str reprentation of the value, first 8 chars
self.value = str(value)[:8]
def __eq__(self, other):
# For full UUIDs, our value is shorter
# For short, the lengths are the same
return other.startswith(self.value)
def __str__(self):
return self.value
def __hash__(self):
return self.value.__hash__()
def startswith(self, part):
return self.value.startswith(part)
class VimwikiTask(object):
# Lists all data keys that are reflected in Vim representation
buffer_keys = ('indent', 'description', 'uuid', 'completed_mark',
@ -30,6 +50,7 @@ class VimwikiTask(object):
self.vim_data = dict(indent='', completed_mark=' ', line_number=None)
self._buffer_data = None
self.__unsaved_task = None
self.uuid = ShortUUID(uuid) if uuid is not None else None
def __getitem__(self, key):
if key in self.vim_data.keys():