Skip to content

Commit b75e3d2

Browse files
committed
feat: Record document dependent files
1 parent 730e885 commit b75e3d2

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/sphinxnotes/snippet/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import annotations
1010
from typing import List, Tuple, Optional, TYPE_CHECKING
1111
import itertools
12+
from os import path
1213

1314
from docutils import nodes
1415

@@ -29,7 +30,7 @@ class Snippet(object):
2930
# :rst:role:`doc`.
3031
docname: str
3132

32-
#: Source file path of snippet
33+
#: Absolute path of the source file.
3334
file: str
3435

3536
#: Line number range of snippet, in the source file which is left closed
@@ -136,10 +137,21 @@ def __init__(self, node: nodes.section) -> None:
136137

137138

138139
class Document(Section):
140+
#: A set of absolute paths of dependent files for document.
141+
#: Obtained from :attr:`BuildEnvironment.dependencies`.
142+
deps: set[str]
143+
139144
def __init__(self, node: nodes.document) -> None:
140145
assert isinstance(node, nodes.document)
141146
super().__init__(node.next_node(nodes.section))
142147

148+
# Record document's dependent files
149+
self.deps = set()
150+
env: BuildEnvironment = node.settings.env
151+
for dep in env.dependencies[self.docname]:
152+
# Relative to documentation root -> Absolute path of file system.
153+
self.deps.add(path.join(env.srcdir, dep))
154+
143155

144156
################
145157
# Nodes helper #

src/sphinxnotes/snippet/cli.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from xdg.BaseDirectory import xdg_config_home
1919
from sphinx.util.matching import patmatch
2020

21-
from . import __version__
21+
from . import __version__, Document
2222
from .config import Config
2323
from .cache import Cache, IndexID, Index
2424
from .table import tablify, COLUMNS
@@ -114,6 +114,9 @@ def main(argv: List[str] = sys.argv[1:]):
114114
getparser.add_argument(
115115
'--file', '-f', action='store_true', help='get source file path of snippet'
116116
)
117+
getparser.add_argument(
118+
'--deps', action='store_true', help='get dependent files of document'
119+
)
117120
getparser.add_argument(
118121
'--line-start',
119122
action='store_true',
@@ -234,17 +237,36 @@ def _on_command_list(args: argparse.Namespace):
234237

235238

236239
def _on_command_get(args: argparse.Namespace):
240+
# Wrapper for warning when nothing is printed
241+
printed = False
242+
243+
def p(*args, **opts):
244+
nonlocal printed
245+
printed = True
246+
print(*args, **opts)
247+
237248
for index_id in args.index_id:
238249
item = args.cache.get_by_index_id(index_id)
239250
if not item:
240-
print('no such index ID', file=sys.stderr)
251+
p('no such index ID', file=sys.stderr)
241252
sys.exit(1)
242253
if args.text:
243-
print('\n'.join(item.snippet.rst))
254+
p('\n'.join(item.snippet.rst))
244255
if args.docname:
245-
print(item.snippet.docname)
256+
p(item.snippet.docname)
246257
if args.file:
247-
print(item.snippet.file)
258+
p(item.snippet.file)
259+
if args.deps:
260+
if not isinstance(item.snippet, Document):
261+
print(
262+
f'{type(item.snippet)} ({index_id}) is not a document',
263+
file=sys.stderr,
264+
)
265+
sys.exit(1)
266+
if len(item.snippet.deps) == 0:
267+
p('') # prevent print nothing warning
268+
for dep in item.snippet.deps:
269+
p(dep)
248270
if args.url:
249271
# HACK: get doc id in better way
250272
doc_id, _ = args.cache.index_id_to_doc_id.get(index_id)
@@ -258,11 +280,15 @@ def _on_command_get(args: argparse.Namespace):
258280
url = posixpath.join(base_url, doc_id[1] + '.html')
259281
if item.snippet.refid:
260282
url += '#' + item.snippet.refid
261-
print(url)
283+
p(url)
262284
if args.line_start:
263-
print(item.snippet.lineno[0])
285+
p(item.snippet.lineno[0])
264286
if args.line_end:
265-
print(item.snippet.lineno[1])
287+
p(item.snippet.lineno[1])
288+
289+
if not printed:
290+
print('please specify at least one argument', file=sys.stderr)
291+
sys.exit(1)
266292

267293

268294
def _on_command_integration(args: argparse.Namespace):

src/sphinxnotes/snippet/picker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def pick(
4040
logger.debug('Skipped document with nosearch metadata')
4141
return []
4242

43-
snippets = []
43+
snippets: list[tuple[Snippet, nodes.section]] = []
4444

4545
# Pick document
4646
toplevel_section = doctree.next_node(nodes.section)

0 commit comments

Comments
 (0)