mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-01 18:49:38 +12:00
Add cache for text counting
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
parent
78614e9344
commit
55b2e04bbf
|
@ -4,6 +4,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from enum import Enum, unique
|
from enum import Enum, unique
|
||||||
from manuskript.data.goal import Goal
|
from manuskript.data.goal import Goal
|
||||||
from manuskript.data.labels import LabelHost, Label
|
from manuskript.data.labels import LabelHost, Label
|
||||||
|
@ -120,12 +121,20 @@ class OutlineText(OutlineItem):
|
||||||
OutlineItem.__init__(self, path, outline)
|
OutlineItem.__init__(self, path, outline)
|
||||||
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
|
self.cache = dict()
|
||||||
|
|
||||||
def textCount(self, counterKind: CounterKind = None) -> int:
|
def textCount(self, counterKind: CounterKind = None) -> int:
|
||||||
if counterKind is None:
|
if counterKind is None:
|
||||||
counterKind = self.goalKind()
|
counterKind = self.goalKind()
|
||||||
|
|
||||||
return super().textCount(counterKind) + countText(self.text, counterKind)
|
textHash = hash(self.text)
|
||||||
|
if textHash not in self.cache:
|
||||||
|
self.cache.clear()
|
||||||
|
self.cache[textHash] = True
|
||||||
|
|
||||||
|
if counterKind.name not in self.cache:
|
||||||
|
self.cache[counterKind.name] = super().textCount(counterKind) + countText(self.text, counterKind)
|
||||||
|
return self.cache[counterKind.name]
|
||||||
|
|
||||||
def load(self, optimized: bool = True):
|
def load(self, optimized: bool = True):
|
||||||
metadata, body = self.file.loadMMD(optimized)
|
metadata, body = self.file.loadMMD(optimized)
|
||||||
|
@ -184,9 +193,8 @@ class OutlineFolder(OutlineItem):
|
||||||
folder.items.append(item)
|
folder.items.append(item)
|
||||||
|
|
||||||
if recursive:
|
if recursive:
|
||||||
for item in folder.items:
|
for item in filter(lambda outlineItem: type(outlineItem) is OutlineFolder, folder.items):
|
||||||
if type(item) is OutlineFolder:
|
cls.loadItems(outline, item, recursive)
|
||||||
cls.loadItems(outline, item, recursive)
|
|
||||||
|
|
||||||
def textCount(self, counterKind: CounterKind = None) -> int:
|
def textCount(self, counterKind: CounterKind = None) -> int:
|
||||||
if counterKind is None:
|
if counterKind is None:
|
||||||
|
@ -219,9 +227,8 @@ class OutlineFolder(OutlineItem):
|
||||||
item.save()
|
item.save()
|
||||||
|
|
||||||
if recursive:
|
if recursive:
|
||||||
for item in folder.items:
|
for item in filter(lambda outlineItem: type(outlineItem) is OutlineFolder, folder.items):
|
||||||
if type(item) is OutlineFolder:
|
cls.saveItems(item, recursive)
|
||||||
cls.saveItems(item, recursive)
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.type = "folder"
|
self.type = "folder"
|
||||||
|
@ -314,14 +321,12 @@ class Outline:
|
||||||
|
|
||||||
self.items.append(item)
|
self.items.append(item)
|
||||||
|
|
||||||
for item in self.items:
|
for item in filter(lambda outlineItem: type(outlineItem) is OutlineFolder, self.items):
|
||||||
if type(item) is OutlineFolder:
|
OutlineFolder.loadItems(self, item, True)
|
||||||
OutlineFolder.loadItems(self, item, True)
|
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
item.save()
|
item.save()
|
||||||
|
|
||||||
for item in self.items:
|
for item in filter(lambda outlineItem: type(outlineItem) is OutlineFolder, self.items):
|
||||||
if type(item) is OutlineFolder:
|
OutlineFolder.saveItems(item, True)
|
||||||
OutlineFolder.saveItems(item, True)
|
|
||||||
|
|
Loading…
Reference in a new issue