Skip to content

Commit 5792c40

Browse files
committed
chore: Format all code
1 parent 24e0a5f commit 5792c40

File tree

14 files changed

+405
-304
lines changed

14 files changed

+405
-304
lines changed

src/sphinxnotes/snippet/__init__.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
2-
sphinxnotes.snippet
3-
~~~~~~~~~~~~~~~~~~~
2+
sphinxnotes.snippet
3+
~~~~~~~~~~~~~~~~~~~
44
5-
:copyright: Copyright 2020 Shengyu Zhang
6-
:license: BSD, see LICENSE for details.
5+
:copyright: Copyright 2020 Shengyu Zhang
6+
:license: BSD, see LICENSE for details.
77
"""
88

99
from __future__ import annotations
@@ -14,44 +14,45 @@
1414

1515
__version__ = '1.1.1'
1616

17+
1718
class Snippet(object):
1819
"""
1920
Snippet is base class of reStructuredText snippet.
2021
21-
:param nodes: Document nodes that make up this snippet
22+
:param nodes: Document nodes that make up this snippet
2223
"""
2324

2425
#: Source file path of snippet
25-
file:str
26+
file: str
2627

2728
#: Line number range of snippet, in the source file which is left closed
2829
#: and right opened.
29-
lineno:Tuple[int,int]
30+
lineno: Tuple[int, int]
3031

3132
#: The original reStructuredText of snippet
32-
rst:List[str]
33+
rst: List[str]
3334

3435
#: The possible identifier key of snippet, which is picked from nodes'
3536
#: (or nodes' parent's) `ids attr`_.
3637
#:
3738
#: .. _ids attr: https://docutils.sourceforge.io/docs/ref/doctree.html#ids
38-
refid:Optional[str]
39+
refid: Optional[str]
3940

40-
def __init__(self, *nodes:nodes.Node) -> None:
41+
def __init__(self, *nodes: nodes.Node) -> None:
4142
assert len(nodes) != 0
4243

4344
self.file = nodes[0].source
4445

4546
lineno = [float('inf'), -float('inf')]
4647
for node in nodes:
4748
if not node.line:
48-
continue # Skip node that have None line, I dont know why
49+
continue # Skip node that have None line, I dont know why
4950
lineno[0] = min(lineno[0], _line_of_start(node))
5051
lineno[1] = max(lineno[1], _line_of_end(node))
5152
self.lineno = lineno
5253

5354
lines = []
54-
with open(self.file, "r") as f:
55+
with open(self.file, 'r') as f:
5556
start = self.lineno[0] - 1
5657
stop = self.lineno[1] - 1
5758
for line in itertools.islice(f, start, stop):
@@ -73,61 +74,60 @@ def __init__(self, *nodes:nodes.Node) -> None:
7374
break
7475

7576

76-
7777
class Text(Snippet):
7878
#: Text of snippet
79-
text:str
79+
text: str
8080

81-
def __init__(self, node:nodes.Node) -> None:
81+
def __init__(self, node: nodes.Node) -> None:
8282
super().__init__(node)
8383
self.text = node.astext()
8484

8585

8686
class CodeBlock(Text):
8787
#: Language of code block
88-
language:str
88+
language: str
8989
#: Caption of code block
90-
caption:Optional[str]
90+
caption: Optional[str]
9191

92-
def __init__(self, node:nodes.literal_block) -> None:
92+
def __init__(self, node: nodes.literal_block) -> None:
9393
assert isinstance(node, nodes.literal_block)
9494
super().__init__(node)
9595
self.language = node['language']
9696
self.caption = node.get('caption')
9797

9898

9999
class WithCodeBlock(object):
100-
code_blocks:List[CodeBlock]
100+
code_blocks: List[CodeBlock]
101101

102-
def __init__(self, nodes:nodes.Nodes) -> None:
102+
def __init__(self, nodes: nodes.Nodes) -> None:
103103
self.code_blocks = []
104104
for n in nodes.traverse(nodes.literal_block):
105105
self.code_blocks.append(self.CodeBlock(n))
106106

107107

108108
class Title(Text):
109-
def __init__(self, node:nodes.title) -> None:
109+
def __init__(self, node: nodes.title) -> None:
110110
assert isinstance(node, nodes.title)
111111
super().__init__(node)
112112

113113

114114
class WithTitle(object):
115-
title:Optional[Title]
115+
title: Optional[Title]
116116

117-
def __init__(self, node:nodes.Node) -> None:
117+
def __init__(self, node: nodes.Node) -> None:
118118
title_node = node.next_node(nodes.title)
119119
self.title = Title(title_node) if title_node else None
120120

121121

122122
class Section(Snippet, WithTitle):
123-
def __init__(self, node:nodes.section) -> None:
123+
def __init__(self, node: nodes.section) -> None:
124124
assert isinstance(node, nodes.section)
125125
Snippet.__init__(self, node)
126126
WithTitle.__init__(self, node)
127127

128128

129129
class Document(Section):
130-
def __init__(self, node:nodes.document) -> None:
130+
def __init__(self, node: nodes.document) -> None:
131131
assert isinstance(node, nodes.document)
132132
super().__init__(node.next_node(nodes.section))
133133

@@ -136,7 +136,8 @@ def __init__(self, node:nodes.document) -> None:
136136
# Nodes helper #
137137
################
138138

139-
def _line_of_start(node:nodes.Node) -> int:
139+
140+
def _line_of_start(node: nodes.Node) -> int:
140141
assert node.line
141142
if isinstance(node, nodes.title):
142143
if isinstance(node.parent.parent, nodes.document):
@@ -155,7 +156,7 @@ def _line_of_start(node:nodes.Node) -> int:
155156
return node.line
156157

157158

158-
def _line_of_end(node:nodes.Node) -> Optional[int]:
159+
def _line_of_end(node: nodes.Node) -> Optional[int]:
159160
next_node = node.next_node(descend=False, siblings=True, ascend=True)
160161
while next_node:
161162
if next_node.line:
@@ -166,7 +167,9 @@ def _line_of_end(node:nodes.Node) -> Optional[int]:
166167
descend=True,
167168
# If node and its children have not valid line attr, try use line
168169
# of next node
169-
ascend=True, siblings=True)
170+
ascend=True,
171+
siblings=True,
172+
)
170173
# No line found, return the max line of source file
171174
if node.source:
172175
with open(node.source) as f:

src/sphinxnotes/snippet/cache.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
""" sphinxnotes.snippet.cache
2-
~~~~~~~~~~~~~~~~~~~~~~~~~
1+
"""sphinxnotes.snippet.cache
2+
~~~~~~~~~~~~~~~~~~~~~~~~~
33
4-
:copyright: Copyright 2021 Shengyu Zhang
5-
:license: BSD, see LICENSE for details.
4+
:copyright: Copyright 2021 Shengyu Zhang
5+
:license: BSD, see LICENSE for details.
66
"""
77

88
from __future__ import annotations
@@ -12,38 +12,41 @@
1212
from . import Snippet
1313
from .utils.pdict import PDict
1414

15+
1516
@dataclass(frozen=True)
1617
class Item(object):
17-
""" Item of snippet cache. """
18-
snippet:Snippet
19-
tags:List[str]
20-
excerpt:str
21-
titlepath:List[str]
22-
keywords:List[str]
18+
"""Item of snippet cache."""
19+
20+
snippet: Snippet
21+
tags: List[str]
22+
excerpt: str
23+
titlepath: List[str]
24+
keywords: List[str]
25+
2326

27+
DocID = Tuple[str, str] # (project, docname)
28+
IndexID = str # UUID
29+
Index = Tuple[str, str, List[str], List[str]] # (tags, excerpt, titlepath, keywords)
2430

25-
DocID = Tuple[str,str] # (project, docname)
26-
IndexID = str # UUID
27-
Index = Tuple[str,str,List[str],List[str]] # (tags, excerpt, titlepath, keywords)
2831

2932
class Cache(PDict):
3033
"""A DocID -> List[Item] Cache."""
31-
indexes:Dict[IndexID,Index]
32-
index_id_to_doc_id:Dict[IndexID,Tuple[DocID,int]]
33-
doc_id_to_index_ids:Dict[DocID,List[IndexID]]
34-
num_snippets_by_project:Dict[str,int]
35-
num_snippets_by_docid:Dict[DocID,int]
3634

37-
def __init__(self, dirname:str) -> None:
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]
40+
41+
def __init__(self, dirname: str) -> None:
3842
self.indexes = {}
3943
self.index_id_to_doc_id = {}
4044
self.doc_id_to_index_ids = {}
41-
self.num_snippets_by_project= {}
45+
self.num_snippets_by_project = {}
4246
self.num_snippets_by_docid = {}
4347
super().__init__(dirname)
4448

45-
46-
def post_dump(self, key:DocID, items:List[Item]) -> None:
49+
def post_dump(self, key: DocID, items: List[Item]) -> None:
4750
"""Overwrite PDict.post_dump."""
4851

4952
# Remove old indexes and index IDs if exists
@@ -54,10 +57,12 @@ def post_dump(self, key:DocID, items:List[Item]) -> None:
5457
# Add new index to every where
5558
for i, item in enumerate(items):
5659
index_id = self.gen_index_id()
57-
self.indexes[index_id] = (item.tags,
58-
item.excerpt,
59-
item.titlepath,
60-
item.keywords)
60+
self.indexes[index_id] = (
61+
item.tags,
62+
item.excerpt,
63+
item.titlepath,
64+
item.keywords,
65+
)
6166
self.index_id_to_doc_id[index_id] = (key, i)
6267
self.doc_id_to_index_ids[key].append(index_id)
6368

@@ -69,8 +74,7 @@ def post_dump(self, key:DocID, items:List[Item]) -> None:
6974
self.num_snippets_by_docid[key] = 0
7075
self.num_snippets_by_docid[key] += len(items)
7176

72-
73-
def post_purge(self, key:DocID, items:List[Item]) -> None:
77+
def post_purge(self, key: DocID, items: List[Item]) -> None:
7478
"""Overwrite PDict.post_purge."""
7579

7680
# Purge indexes
@@ -86,21 +90,19 @@ def post_purge(self, key:DocID, items:List[Item]) -> None:
8690
if self.num_snippets_by_docid[key] == 0:
8791
del self.num_snippets_by_docid[key]
8892

89-
90-
def get_by_index_id(self, key:IndexID) -> Optional[Item]:
93+
def get_by_index_id(self, key: IndexID) -> Optional[Item]:
9194
"""Like get(), but use IndexID as key."""
92-
doc_id, item_index = self.index_id_to_doc_id.get(key, (None,None))
95+
doc_id, item_index = self.index_id_to_doc_id.get(key, (None, None))
9396
if not doc_id:
9497
return None
9598
return self[doc_id][item_index]
9699

97-
98100
def gen_index_id(self) -> str:
99101
"""Generate unique ID for index."""
100102
import uuid
101-
return uuid.uuid4().hex[:7]
102103

104+
return uuid.uuid4().hex[:7]
103105

104-
def stringify(self, key:DocID, items:List[Item]) -> str:
106+
def stringify(self, key: DocID, items: List[Item]) -> str:
105107
"""Overwrite PDict.stringify."""
106108
return key[1]

0 commit comments

Comments
 (0)