1- """sphinxnotes.snippet.cache
1+ """
2+ sphinxnotes.snippet.cache
23~~~~~~~~~~~~~~~~~~~~~~~~~
34
45:copyright: Copyright 2021 Shengyu Zhang
56:license: BSD, see LICENSE for details.
67"""
78
89from __future__ import annotations
9- from typing import List , Tuple , Dict , Optional
1010from dataclasses import dataclass
1111
1212from .snippets import Snippet
@@ -18,25 +18,25 @@ class Item(object):
1818 """Item of snippet cache."""
1919
2020 snippet : Snippet
21- tags : List [ str ]
21+ tags : str
2222 excerpt : str
23- titlepath : List [str ]
24- keywords : List [str ]
23+ titlepath : list [str ]
24+ keywords : list [str ]
2525
2626
27- DocID = Tuple [str , str ] # (project, docname)
27+ DocID = tuple [str , str ] # (project, docname)
2828IndexID = str # UUID
29- Index = Tuple [str , str , List [str ], List [str ]] # (tags, excerpt, titlepath, keywords)
29+ Index = tuple [str , str , list [str ], list [str ]] # (tags, excerpt, titlepath, keywords)
3030
3131
32- class Cache (PDict ):
33- """A DocID -> List [Item] Cache."""
32+ class Cache (PDict [ DocID , list [ Item ]] ):
33+ """A DocID -> list [Item] Cache."""
3434
35- indexes : Dict [IndexID , Index ]
36- index_id_to_doc_id : Dict [IndexID , Tuple [DocID , int ]]
37- doc_id_to_index_ids : Dict [DocID , List [IndexID ]]
38- num_snippets_by_project : Dict [str , int ]
39- num_snippets_by_docid : Dict [DocID , int ]
35+ indexes : dict [IndexID , Index ]
36+ index_id_to_doc_id : dict [IndexID , tuple [DocID , int ]]
37+ doc_id_to_index_ids : dict [DocID , list [IndexID ]]
38+ num_snippets_by_project : dict [str , int ]
39+ num_snippets_by_docid : dict [DocID , int ]
4040
4141 def __init__ (self , dirname : str ) -> None :
4242 self .indexes = {}
@@ -46,7 +46,7 @@ def __init__(self, dirname: str) -> None:
4646 self .num_snippets_by_docid = {}
4747 super ().__init__ (dirname )
4848
49- def post_dump (self , key : DocID , items : List [Item ]) -> None :
49+ def post_dump (self , key : DocID , value : list [Item ]) -> None :
5050 """Overwrite PDict.post_dump."""
5151
5252 # Remove old indexes and index IDs if exists
@@ -55,7 +55,7 @@ def post_dump(self, key: DocID, items: List[Item]) -> None:
5555 del self .indexes [old_index_id ]
5656
5757 # Add new index to every where
58- for i , item in enumerate (items ):
58+ for i , item in enumerate (value ):
5959 index_id = self .gen_index_id ()
6060 self .indexes [index_id ] = (
6161 item .tags ,
@@ -69,12 +69,12 @@ def post_dump(self, key: DocID, items: List[Item]) -> None:
6969 # Update statistic
7070 if key [0 ] not in self .num_snippets_by_project :
7171 self .num_snippets_by_project [key [0 ]] = 0
72- self .num_snippets_by_project [key [0 ]] += len (items )
72+ self .num_snippets_by_project [key [0 ]] += len (value )
7373 if key not in self .num_snippets_by_docid :
7474 self .num_snippets_by_docid [key ] = 0
75- self .num_snippets_by_docid [key ] += len (items )
75+ self .num_snippets_by_docid [key ] += len (value )
7676
77- def post_purge (self , key : DocID , items : List [Item ]) -> None :
77+ def post_purge (self , key : DocID , value : list [Item ]) -> None :
7878 """Overwrite PDict.post_purge."""
7979
8080 # Purge indexes
@@ -83,17 +83,17 @@ def post_purge(self, key: DocID, items: List[Item]) -> None:
8383 del self .indexes [index_id ]
8484
8585 # Update statistic
86- self .num_snippets_by_project [key [0 ]] -= len (items )
86+ self .num_snippets_by_project [key [0 ]] -= len (value )
8787 if self .num_snippets_by_project [key [0 ]] == 0 :
8888 del self .num_snippets_by_project [key [0 ]]
89- self .num_snippets_by_docid [key ] -= len (items )
89+ self .num_snippets_by_docid [key ] -= len (value )
9090 if self .num_snippets_by_docid [key ] == 0 :
9191 del self .num_snippets_by_docid [key ]
9292
93- def get_by_index_id (self , key : IndexID ) -> Optional [ Item ] :
93+ def get_by_index_id (self , key : IndexID ) -> Item | None :
9494 """Like get(), but use IndexID as key."""
9595 doc_id , item_index = self .index_id_to_doc_id .get (key , (None , None ))
96- if not doc_id :
96+ if not doc_id or not item_index :
9797 return None
9898 return self [doc_id ][item_index ]
9999
@@ -103,6 +103,6 @@ def gen_index_id(self) -> str:
103103
104104 return uuid .uuid4 ().hex [:7 ]
105105
106- def stringify (self , key : DocID , items : List [Item ]) -> str :
106+ def stringify (self , key : DocID , value : list [Item ]) -> str :
107107 """Overwrite PDict.stringify."""
108108 return key [1 ]
0 commit comments