doorstop.core.item.Item._get_issues_tree()   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 37
ccs 10
cts 20
cp 0.5
rs 4.8
c 0
b 0
f 0
cc 12
nop 2
crap 30

How to fix   Complexity   

Complexity

Complex classes like doorstop.core.item.Item._get_issues_tree() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# SPDX-License-Identifier: LGPL-3.0-only
2
3 1
"""Representation of an item in a document."""
4 1
5 1
import functools
6
import os
7 1
import re
8
from typing import Any, List
9 1
10 1
import pyficache
11 1
12
from doorstop import common, settings
13
from doorstop.common import DoorstopError, DoorstopInfo, DoorstopWarning
14 1
from doorstop.core import editor
15 1
from doorstop.core.base import (
16 1
    BaseFileObject,
17
    BaseValidatable,
18 1
    add_item,
19
    auto_load,
20
    auto_save,
21 1
    delete_item,
22
    edit_item,
23 1
)
24
from doorstop.core.types import UID, Level, Prefix, Stamp, Text, to_bool
25
26 1
log = common.logger(__name__)
27 1
28 1
29 1
def _convert_to_yaml(indent, prefix, value):
30 1
    """Convert value to YAML output format.
31 1
32
    :param indent: the indentation level
33
    :param prefix: the length of the prefix before the value, e.g. '- ' for
34 1
    lists or 'key: ' for keys
35
    :param value: the value to convert
36 1
37
    :return: the value converted to YAML output format
38
39 1
    """
40 1
    if isinstance(value, str):
41 1
        length = indent + prefix + len(value)
42 1
        if length > settings.MAX_LINE_LENGTH or '\n' in value:
43 1
            value = Text.save_text(value.strip())
44 1
        else:
45 1
            value = str(value)  # line is short enough as a string
46
    elif isinstance(value, list):
47
        value = [_convert_to_yaml(indent, 2, v) for v in value]
48 1
    elif isinstance(value, dict):
49
        value = {
50
            k: _convert_to_yaml(indent + 2, len(k) + 2, v) for k, v in value.items()
51 1
        }
52
    return value
53 1
54 1
55 1
def requires_tree(func):
56 1
    """Require a tree reference."""
57 1
58 1
    @functools.wraps(func)
59 1
    def wrapped(self, *args, **kwargs):
60
        if not self.tree:
61 1
            name = func.__name__
62
            log.critical("`{}` can only be called with a tree".format(name))
63
            return None
64
        return func(self, *args, **kwargs)
65
66
    return wrapped
67
68 1
69
class Item(BaseValidatable, BaseFileObject):  # pylint: disable=R0902
70 1
    """Represents an item file with linkable text."""
71 1
72
    EXTENSIONS = '.yml', '.yaml'
73 1
74 1
    DEFAULT_LEVEL = Level('1.0')
75 1
    DEFAULT_ACTIVE = True
76 1
    DEFAULT_NORMATIVE = True
77 1
    DEFAULT_DERIVED = False
78 1
    DEFAULT_REVIEWED = Stamp()
79 1
    DEFAULT_TEXT = Text()
80
    DEFAULT_REF = ""
81 1
    DEFAULT_HEADER = Text()
82 1
83 1
    def __init__(self, document, path, root=os.getcwd(), **kwargs):
84
        """Initialize an item from an existing file.
85 1
86 1
        :param path: path to Item file
87 1
        :param root: path to root of project
88 1
89 1
        """
90
        super().__init__()
91 1
        # Ensure the path is valid
92 1
        if not os.path.isfile(path):
93 1
            raise DoorstopError("item does not exist: {}".format(path))
94 1
        # Ensure the filename is valid
95 1
        filename = os.path.basename(path)
96 1
        name, ext = os.path.splitext(filename)
97 1
        try:
98 1
            UID(name).check()
99
        except DoorstopError:
100 1
            msg = "invalid item filename: {}".format(filename)
101 1
            raise DoorstopError(msg) from None
102
        # Ensure the file extension is valid
103 1
        if ext.lower() not in self.EXTENSIONS:
104 1
            msg = "'{0}' extension not in {1}".format(path, self.EXTENSIONS)
105 1
            raise DoorstopError(msg)
106
        # Initialize the item
107 1
        self.path = path
108
        self.root = root
109 1
        self.document = document
110 1
        self.tree = kwargs.get('tree')
111 1
        self.auto = kwargs.get('auto', Item.auto)
112
        # Set default values
113 1
        self._data['level'] = Item.DEFAULT_LEVEL
114
        self._data['active'] = Item.DEFAULT_ACTIVE
115 1
        self._data['normative'] = Item.DEFAULT_NORMATIVE
116 1
        self._data['derived'] = Item.DEFAULT_DERIVED
117 1
        self._data['reviewed'] = Item.DEFAULT_REVIEWED
118
        self._data['text'] = Item.DEFAULT_TEXT
119
        self._data['ref'] = Item.DEFAULT_REF
120
        self._data['links'] = set()
121
        if settings.ENABLE_HEADERS:
122
            self._data['header'] = Item.DEFAULT_HEADER
123
124
    def __repr__(self):
125
        return "Item('{}')".format(self.path)
126
127
    def __str__(self):
128
        if common.verbosity < common.STR_VERBOSITY:
129
            return str(self.uid)
130
        else:
131
            return "{} ({})".format(self.uid, self.relpath)
132
133
    def __lt__(self, other):
134
        if self.level == other.level:
135
            return self.uid < other.uid
136 1
        else:
137 1
            return self.level < other.level
138 1
139
    @staticmethod
140 1
    @add_item
141 1
    def new(
142
        tree, document, path, root, uid, level=None, auto=None
143 1
    ):  # pylint: disable=R0913
144 1
        """Create a new item.
145 1
146 1
        :param tree: reference to the tree that contains this item
147
        :param document: reference to document that contains this item
148 1
149
        :param path: path to directory for the new item
150 1
        :param root: path to root of the project
151
        :param uid: UID for the new item
152 1
153 1
        :param level: level for the new item
154 1
        :param auto: automatically save the item
155
156 1
        :raises: :class:`~doorstop.common.DoorstopError` if the item
157
            already exists
158 1
159
        :return: new :class:`~doorstop.core.item.Item`
160 1
161 1
        """
162 1
        UID(uid).check()
163 1
        filename = str(uid) + Item.EXTENSIONS[0]
164 1
        path2 = os.path.join(path, filename)
165 1
        # Create the initial item file
166 1
        log.debug("creating item file at {}...".format(path2))
167 1
        Item._create(path2, name='item')
168 1
        # Initialize the item
169 1
        item = Item(document, path2, root=root, tree=tree, auto=False)
170 1
        item.level = level if level is not None else item.level  # type: ignore
171 1
        if auto or (auto is None and Item.auto):
172 1
            item.save()
173 1
        # Return the item
174 1
        return item
175 1
176 1
    def _set_attributes(self, attributes):
177
        """Set the item's attributes."""
178 1
        for key, value in attributes.items():
179 1
            if key == 'level':
180 1
                value = Level(value)
181
            elif key == 'active':
182 1
                value = to_bool(value)
183
            elif key == 'normative':
184 1
                value = to_bool(value)
185
            elif key == 'derived':
186
                value = to_bool(value)
187 1
            elif key == 'reviewed':
188
                value = Stamp(value)
189 1
            elif key == 'text':
190
                value = Text(value)
191 1
            elif key == 'ref':
192
                value = value.strip()
193 1
            elif key == 'links':
194
                value = set(UID(part) for part in value)
195 1
            elif key == 'header':
196 1
                value = Text(value)
197
            self._data[key] = value
198
199
    def load(self, reload=False):
200 1
        """Load the item's properties from its file."""
201 1
        if self._loaded and not reload:
202
            return
203
        log.debug("loading {}...".format(repr(self)))
204 1
        # Read text from file
205 1
        text = self._read(self.path)
206 1
        # Parse YAML data from text
207 1
        data = self._load(text, self.path)
208 1
        # Store parsed data
209 1
        self._set_attributes(data)
210 1
        # Set meta attributes
211 1
        self._loaded = True
212 1
213 1
    @edit_item
214 1
    def save(self):
215 1
        """Format and save the item's properties to its file."""
216
        log.debug("saving {}...".format(repr(self)))
217 1
        # Format the data items
218
        data = self._yaml_data()
219 1
        # Dump the data to YAML
220 1
        text = self._dump(data)
221 1
        # Save the YAML to file
222
        self._write(text, self.path)
223 1
        # Set meta attributes
224 1
        self._loaded = True
225 1
        self.auto = True
226
227 1
    # properties #############################################################
228
229
    def _yaml_data(self):
230 1
        """Get all the item's data formatted for YAML dumping."""
231 1
        data = {}
232
        for key, value in self._data.items():
233 1
            if key == 'level':
234
                value = value.yaml
235
            elif key == 'text':
236 1
                value = value.yaml
237
            elif key == 'header':
238 1
                # Handle for case if the header is undefined in YAML
239
                if hasattr(value, 'yaml'):
240
                    value = value.yaml
241 1
                else:
242
                    value = ''
243 1
            elif key == 'ref':
244 1
                value = value.strip()
245
            elif key == 'links':
246
                value = [{str(i): i.stamp.yaml} for i in sorted(value)]
247 1
            elif key == 'reviewed':
248
                value = value.yaml
249 1
            else:
250 1
                value = _convert_to_yaml(0, len(key) + 2, value)
251 1
            data[key] = value
252
        return data
253
254 1
    @property  # type: ignore
255
    @auto_load
256 1
    def data(self):
257
        """Load and get all the item's data formatted for YAML dumping."""
258
        return self._yaml_data()
259 1
260
    @property
261 1
    def uid(self):
262 1
        """Get the item's UID."""
263
        filename = os.path.basename(self.path)
264
        return UID(os.path.splitext(filename)[0])
265
266
    @property
267
    def prefix(self):
268
        """Get the item UID's prefix."""
269
        return self.uid.prefix
270
271
    @property
272
    def number(self):
273
        """Get the item UID's number."""
274
        return self.uid.number
275 1
276
    @property  # type: ignore
277 1
    @auto_load
278 1
    def level(self):
279 1
        """Get the item's level."""
280
        return self._data['level']
281
282 1
    @level.setter  # type: ignore
283
    @auto_save
284 1
    def level(self, value):
285 1
        """Set the item's level."""
286
        self._data['level'] = Level(value)
287
288
    @property
289
    def depth(self):
290
        """Get the item's heading order based on it's level."""
291
        return len(self.level)
292
293
    @property  # type: ignore
294 1
    @auto_load
295
    def active(self):
296 1
        """Get the item's active status.
297 1
298 1
        An inactive item will not be validated. Inactive items are
299
        intended to be used for:
300
301 1
        - future requirements
302
        - temporarily disabled requirements or tests
303 1
        - externally implemented requirements
304 1
        - etc.
305
306
        """
307
        return self._data['active']
308
309
    @active.setter  # type: ignore
310
    @auto_save
311
    def active(self, value):
312
        """Set the item's active status."""
313
        self._data['active'] = to_bool(value)
314
315
    @property  # type: ignore
316 1
    @auto_load
317
    def derived(self):
318 1
        """Get the item's derived status.
319 1
320 1
        A derived item does not have links to items in its parent
321
        document, but should still be linked to by items in its child
322
        documents.
323 1
324
        """
325 1
        return self._data['derived']
326
327
    @derived.setter  # type: ignore
328
    @auto_save
329
    def derived(self, value):
330
        """Set the item's derived status."""
331
        self._data['derived'] = to_bool(value)
332 1
333
    @property  # type: ignore
334 1
    @auto_load
335 1
    def normative(self):
336 1
        """Get the item's normative status.
337
338
        A non-normative item should not have or be linked to.
339 1
        Non-normative items are intended to be used for:
340 1
341 1
        - headings
342 1
        - comments
343 1
        - etc.
344 1
345 1
        """
346
        return self._data['normative']
347 1
348 1
    @normative.setter  # type: ignore
349
    @auto_save
350
    def normative(self, value):
351 1
        """Set the item's normative status."""
352 1
        self._data['normative'] = to_bool(value)
353 1
354 1
    @property
355 1
    def heading(self):
356 1
        """Indicate if the item is a heading.
357 1
358
        Headings have a level that ends in zero and are non-normative.
359 1
360 1
        """
361 1
        return self.level.heading and not self.normative
362
363
    @heading.setter  # type: ignore
364 1
    @auto_save
365
    def heading(self, value):
366 1
        """Set the item's heading status."""
367 1
        heading = to_bool(value)
368
        if heading and not self.heading:
369
            self.level.heading = True
370 1
            self.normative = False
371 1
        elif not heading and self.heading:
372 1
            self.level.heading = False
373 1
            self.normative = True
374
375 1
    @property  # type: ignore
376 1
    @auto_load
377 1
    def cleared(self):
378
        """Indicate if no links are suspect."""
379
        for uid, item in self._get_parent_uid_and_item():
380 1
            if uid.stamp != item.stamp():
381
                return False
382 1
        return True
383 1
384
    @property  # type: ignore
385
    @auto_load
386 1
    def reviewed(self):
387
        """Indicate if the item has been reviewed."""
388 1
        stamp = self.stamp(links=True)
389 1
        if self._data['reviewed'] == Stamp(True):
390 1
            self._data['reviewed'] = stamp
391
        return self._data['reviewed'] == stamp
392
393 1
    @reviewed.setter  # type: ignore
394
    @auto_save
395 1
    def reviewed(self, value):
396 1
        """Set the item's review status."""
397
        self._data['reviewed'] = Stamp(value)
398
399
    @property  # type: ignore
400
    @auto_load
401
    def text(self):
402
        """Get the item's text."""
403
        return self._data['text']
404 1
405
    @text.setter  # type: ignore
406 1
    @auto_save
407 1
    def text(self, value):
408 1
        """Set the item's text."""
409
        self._data['text'] = Text(value)
410
411 1
    @property  # type: ignore
412
    @auto_load
413 1
    def header(self):
414 1
        """Get the item's header."""
415
        if settings.ENABLE_HEADERS:
416
            return self._data['header']
417 1
        return None
418
419 1
    @header.setter  # type: ignore
420 1
    @auto_save
421 1
    def header(self, value):
422
        """Set the item's header."""
423
        if settings.ENABLE_HEADERS:
424 1
            self._data['header'] = Text(value)
425
426 1
    @property  # type: ignore
427
    @auto_load
428
    def ref(self):
429 1
        """Get the item's external file reference.
430
431 1
        An external reference can be part of a line in a text file or
432
        the filename of any type of file.
433
434 1
        """
435
        return self._data['ref']
436 1
437 1
    @ref.setter  # type: ignore
438
    @auto_save
439
    def ref(self, value):
440 1
        """Set the item's external file reference."""
441 1
        self._data['ref'] = str(value) if value else ""
442 1
443 1
    @property  # type: ignore
444 1
    @auto_load
445 1
    def links(self):
446 1
        """Get a list of the item UIDs this item links to."""
447 1
        return sorted(self._data['links'])
448 1
449
    @links.setter  # type: ignore
450 1
    @auto_save
451 1
    def links(self, value):
452 1
        """Set the list of item UIDs this item links to."""
453
        self._data['links'] = set(UID(v) for v in value)
454
455
    @property
456
    def parent_links(self):
457
        """Get a list of the item UIDs this item links to."""
458
        return self.links  # alias
459
460
    @parent_links.setter
461 1
    def parent_links(self, value):
462 1
        """Set the list of item UIDs this item links to."""
463 1
        self.links = value  # alias
464 1
465 1
    @requires_tree
466
    def _get_parent_uid_and_item(self):
467
        """Yield UID and item of all links of this item."""
468
        for uid in self.links:
469 1
            try:
470 1
                item = self.tree.find_item(uid)
471
            except DoorstopError:
472
                item = UnknownItem(uid)
473
                log.warning(item.exception)
474
            yield uid, item
475
476
    @property
477 1
    def parent_items(self):
478 1
        """Get a list of items that this item links to."""
479
        return [item for uid, item in self._get_parent_uid_and_item()]
480 1
481
    @property  # type: ignore
482 1
    @requires_tree
483
    def parent_documents(self):
484 1
        """Get a list of documents that this item's document should link to.
485 1
486
        .. note::
487
488
           A document only has one parent.
489
490
        """
491
        try:
492 1
            return [self.tree.find_document(self.document.prefix)]
493 1
        except DoorstopError:
494 1
            log.warning(Prefix.UNKNOWN_MESSAGE.format(self.document.prefix))
495
            return []
496 1
497 1
    # actions ################################################################
498
499
    @auto_save
500
    def set_attributes(self, attributes):
501
        """Set the item's attributes and save them."""
502
        self._set_attributes(attributes)
503
504 1
    @auto_save
505 1
    def edit(self, tool=None, edit_all=True):
506 1
        """Open the item for editing.
507 1
508 1
        :param tool: path of alternate editor
509
        :param edit_all: True to edit the whole item,
510 1
            False to only edit the text.
511
512
        """
513
        # Lock the item
514
        if self.tree:
515
            self.tree.vcs.lock(self.path)
516
        # Edit the whole file in an editor
517
        if edit_all:
518
            editor.edit(self.path, tool=tool)
519
        # Edit only the text part in an editor
520 1
        else:
521 1
            # Edit the text in a temporary file
522 1
            edited_text = editor.edit_tmp_content(
523
                title=str(self.uid), original_content=str(self.text), tool=tool
524 1
            )
525
            # Save the text in the actual item file
526
            self.text = edited_text
527 1
            self.save()
528
529
        # Force reloaded
530 1
        self._loaded = False
531 1
532 1
    @auto_save
533
    def link(self, value):
534
        """Add a new link to another item UID.
535 1
536 1
        :param value: item or UID
537
538
        """
539 1
        uid = UID(value)
540 1
        log.info("linking to '{}'...".format(uid))
541
        self._data['links'].add(uid)
542
543 1
    @auto_save
544 1
    def unlink(self, value):
545 1
        """Remove an existing link by item UID.
546 1
547 1
        :param value: item or UID
548
549
        """
550 1
        uid = UID(value)
551 1
        try:
552
            self._data['links'].remove(uid)
553
        except KeyError:
554 1
            log.warning("link to {0} does not exist".format(uid))
555 1
556
    def get_issues(
557
        self, skip=None, document_hook=None, item_hook=None
558 1
    ):  # pylint: disable=unused-argument
559 1
        """Yield all the item's issues.
560
561
        :param skip: list of document prefixes to skip
562 1
563 1
        :return: generator of :class:`~doorstop.common.DoorstopError`,
564
                              :class:`~doorstop.common.DoorstopWarning`,
565
                              :class:`~doorstop.common.DoorstopInfo`
566
567 1
        """
568 1
        assert document_hook is None
569 1
        assert item_hook is None
570 1
        skip = [] if skip is None else skip
571 1
572
        log.info("checking item %s...", self)
573 1
574
        # Verify the file can be parsed
575 1
        self.load()
576
577
        # Skip inactive items
578 1
        if not self.active:
579 1
            log.info("skipped inactive item: %s", self)
580 1
            return
581
582 1
        # Delay item save if reformatting
583
        if settings.REFORMAT:
584 1
            self.auto = False
585
586 1
        # Check text
587
        if not self.text:
588
            yield DoorstopWarning("no text")
589
590
        # Check external references
591 1
        if settings.CHECK_REF:
592 1
            try:
593 1
                self.find_ref()
594
            except DoorstopError as exc:
595
                yield exc
596 1
597
        # Check links
598
        if not self.normative and self.links:
599 1
            yield DoorstopWarning("non-normative, but has links")
600 1
601
        # Check links against the document
602
        yield from self._get_issues_document(self.document, skip)
603 1
604 1
        if self.tree:
605 1
            # Check links against the tree
606 1
            yield from self._get_issues_tree(self.tree)
607 1
608 1
            # Check links against both document and tree
609
            yield from self._get_issues_both(self.document, self.tree, skip)
610 1
611
        # Check review status
612
        if not self.reviewed:
613
            if settings.CHECK_REVIEW_STATUS:
614 1
                if not self._data['reviewed']:
615
                    if settings.REVIEW_NEW_ITEMS:
616 1
                        self.review()
617
                    else:
618 1
                        yield DoorstopInfo("needs initial review")
619
                else:
620 1
                    yield DoorstopWarning("unreviewed changes")
621
622
        # Reformat the file
623 1
        if settings.REFORMAT:
624 1
            log.debug("reformatting item %s...", self)
625 1
            self.save()
626 1
627 1
    def _get_issues_document(self, document, skip):
628 1
        """Yield all the item's issues against its document."""
629 1
        log.debug("getting issues against document...")
630 1
631
        if document in skip:
632
            log.debug("skipping issues against document %s...", document)
633 1
            return
634 1
635 1
        # Verify an item's UID matches its document's prefix
636 1
        if self.prefix != document.prefix:
637 1
            msg = "prefix differs from document ({})".format(document.prefix)
638 1
            yield DoorstopInfo(msg)
639
640 1
        # Verify that normative, non-derived items in a child document have at
641 1
        # least one link.  It is recommended that these items have an upward
642 1
        # link to an item in the parent document, however, this is not
643 1
        # enforced.  An info message is generated if this is not the case.
644 1
        if all((document.parent, self.normative, not self.derived)) and not self.links:
645 1
            msg = "no links to parent document: {}".format(document.parent)
646 1
            yield DoorstopWarning(msg)
647 1
648
        # Verify an item's links are to the correct parent
649 1
        for uid in self.links:
650 1
            try:
651
                prefix = uid.prefix
652
            except DoorstopError:
653 1
                msg = "invalid UID in links: {}".format(uid)
654 1
                yield DoorstopError(msg)
655
            else:
656 1
                if document.parent and prefix != document.parent:
657
                    # this is only 'info' because a document is allowed
658 1
                    # to contain items with a different prefix, but
659
                    # Doorstop will not create items like this
660 1
                    msg = "parent is '{}', but linked to: {}".format(
661
                        document.parent, uid
662
                    )
663
                    yield DoorstopInfo(msg)
664
665 1
    def _get_issues_tree(self, tree):
666 1
        """Yield all the item's issues against its tree."""
667 1
        log.debug("getting issues against tree...")
668
669
        # Verify an item's links are valid
670
        identifiers = set()
671 1
        for uid in self.links:
672 1
            try:
673 1
                item = tree.find_item(uid)
674
            except DoorstopError:
675
                identifiers.add(uid)  # keep the invalid UID
676
                msg = "linked to unknown item: {}".format(uid)
677 1
                yield DoorstopError(msg)
678 1
            else:
679 1
                # check the linked item
680
                if not item.active:
681
                    msg = "linked to inactive item: {}".format(item)
682
                    yield DoorstopInfo(msg)
683
                if not item.normative:
684
                    msg = "linked to non-normative item: {}".format(item)
685
                    yield DoorstopWarning(msg)
686
                # check the link status
687
                if uid.stamp == Stamp(True):
688 1
                    uid.stamp = item.stamp()
689
                elif not str(uid.stamp) and settings.STAMP_NEW_LINKS:
690
                    uid.stamp = item.stamp()
691
                elif uid.stamp != item.stamp():
692
                    if settings.CHECK_SUSPECT_LINKS:
693
                        msg = "suspect link: {}".format(item)
694
                        yield DoorstopWarning(msg)
695
                # reformat the item's UID
696
                identifier2 = UID(item.uid, stamp=uid.stamp)
697
                identifiers.add(identifier2)
698
699
        # Apply the reformatted item UIDs
700
        if settings.REFORMAT:
701
            self._data['links'] = identifiers
702 1
703 1
    def _get_issues_both(self, document, tree, skip):
704 1
        """Yield all the item's issues against its document and tree."""
705
        log.debug("getting issues against document and tree...")
706 1
707 1
        if document.prefix in skip:
708
            log.debug("skipping issues against document %s...", document)
709 1
            return
710 1
711 1
        # Verify an item is being linked to (child links)
712 1
        if settings.CHECK_CHILD_LINKS and self.normative:
713 1
            find_all = settings.CHECK_CHILD_LINKS_STRICT or False
714
            items, documents = self._find_child_objects(
715 1
                document=document, tree=tree, find_all=find_all
716 1
            )
717
718 1
            if not items:
719 1
                for child_document in documents:
720
                    if document.prefix in skip:
721 1
                        msg = "skipping issues against document %s..."
722 1
                        log.debug(msg, child_document)
723
                        continue
724 1
                    msg = "no links from child document: {}".format(child_document)
725 1
                    yield DoorstopWarning(msg)
726 1
            elif settings.CHECK_CHILD_LINKS_STRICT:
727 1
                prefix = [item.prefix for item in items]
728 1
                for child in document.children:
729 1
                    if child in skip:
730 1
                        continue
731 1
                    if child not in prefix:
732
                        msg = 'no links from document: {}'.format(child)
733 1
                        yield DoorstopWarning(msg)
734 1
735
    @requires_tree
736 1
    def find_ref(self):
737
        """Get the external file reference and line number.
738
739
        :raises: :class:`~doorstop.common.DoorstopError` when no
740
            reference is found
741
742
        :return: relative path to file or None (when no reference
743
            set),
744 1
            line number (when found in file) or None (when found as
745 1
            filename) or None (when no reference set)
746 1
747
        """
748 1
        # Return immediately if no external reference
749
        if not self.ref:
750 1
            log.debug("no external reference to search for")
751
            return None, None
752
        # Update the cache
753
        if not settings.CACHE_PATHS:
754
            pyficache.clear_file_cache()
755
        # Search for the external reference
756
        log.debug("seraching for ref '{}'...".format(self.ref))
757
        pattern = r"(\b|\W){}(\b|\W)".format(re.escape(self.ref))
758 1
        log.trace("regex: {}".format(pattern))  # type: ignore
759 1
        regex = re.compile(pattern)
760
        for path, filename, relpath in self.tree.vcs.paths:
761 1
            # Skip the item's file while searching
762
            if path == self.path:
763 1
                continue
764
            # Check for a matching filename
765
            if filename == self.ref:
766
                return relpath, None
767
            # Skip extensions that should not be considered text
768
            if os.path.splitext(filename)[-1] in settings.SKIP_EXTS:
769 1
                continue
770 1
            # Search for the reference in the file
771
            lines = pyficache.getlines(path)
772 1
            if lines is None:
773
                log.trace("unable to read lines from: {}".format(path))  # type: ignore
774 1
                continue
775
            for lineno, line in enumerate(lines, start=1):
776
                if regex.search(line):
777
                    log.debug("found ref: {}".format(relpath))
778
                    return relpath, lineno
779
780
        msg = "external reference not found: {}".format(self.ref)
781
        raise DoorstopError(msg)
782
783
    def find_child_links(self, find_all=True):
784 1
        """Get a list of item UIDs that link to this item (reverse links).
785 1
786 1
        :param find_all: find all items (not just the first) before returning
787 1
788 1
        :return: list of found item UIDs
789 1
790
        """
791 1
        items, _ = self._find_child_objects(find_all=find_all)
792 1
        identifiers = [item.uid for item in items]
793 1
        return identifiers
794 1
795
    child_links = property(find_child_links)
796 1
797 1
    def find_child_items(self, find_all=True):
798 1
        """Get a list of items that link to this item.
799 1
800
        :param find_all: find all items (not just the first) before returning
801
802
        :return: list of found items
803
804 1
        """
805 1
        items, _ = self._find_child_objects(find_all=find_all)
806 1
        return items
807
808 1
    child_items = property(find_child_items)
809 1
810 1
    def find_child_documents(self):
811 1
        """Get a list of documents that should link to this item's document.
812
813 1
        :return: list of found documents
814 1
815 1
        """
816 1
        _, documents = self._find_child_objects(find_all=False)
817 1
        return documents
818
819 1
    child_documents = property(find_child_documents)
820 1
821
    def _find_child_objects(self, document=None, tree=None, find_all=True):
822 1
        """Get lists of child items and child documents.
823 1
824 1
        :param document: document containing the current item
825 1
        :param tree: tree containing the current item
826
        :param find_all: find all items (not just the first) before returning
827 1
828 1
        :return: list of found items, list of all child documents
829 1
830
        """
831 1
        child_items: List[Item] = []
832 1
        child_documents: List[Any] = []  # `List[Document]`` creats an import cycle
833 1
        document = document or self.document
834 1
        tree = tree or self.tree
835 1
        if not document or not tree:
836 1
            return child_items, child_documents
837 1
        # Find child objects
838
        log.debug("finding item {}'s child objects...".format(self))
839 1
        for document2 in tree:
840
            if document2.parent == document.prefix:
841 1
                child_documents.append(document2)
842 1
                # Search for child items unless we only need to find one
843
                if not child_items or find_all:
844
                    for item2 in document2:
845 1
                        if self.uid in item2.links:
846 1
                            if not item2.active:
847
                                item2 = UnknownItem(item2.uid)
848 1
                                log.warning(item2.exception)
849 1
                                child_items.append(item2)
850
                            else:
851 1
                                child_items.append(item2)
852
                                if not find_all and item2.active:
853
                                    break
854 1
        # Display found links
855
        if child_items:
856
            if find_all:
857 1
                joined = ', '.join(str(i) for i in child_items)
858
                msg = "child items: {}".format(joined)
859 1
            else:
860 1
                msg = "first child item: {}".format(child_items[0])
861
            log.debug(msg)
862 1
            joined = ', '.join(str(d) for d in child_documents)
863 1
            log.debug("child documents: {}".format(joined))
864 1
        return sorted(child_items), child_documents
865 1
866 1
    @auto_load
867
    def stamp(self, links=False):
868 1
        """Hash the item's key content for later comparison."""
869 1
        values = [self.uid, self.text, self.ref]
870
        if links:
871 1
            values.extend(self.links)
872 1
        for key in self.document.extended_reviewed:
873 1
            if key in self._data:
874 1
                values.append(self._dump(self._data[key]))
875
            else:
876 1
                log.warning(
877
                    "{}: missing extended reviewed attribute: {}".format(self.uid, key)
878
                )
879 1
        return Stamp(*values)
880
881
    @auto_save
882 1
    def clear(self, parents=None):
883
        """Clear suspect links."""
884 1
        log.info("clearing suspect links...")
885 1
        for uid, item in self._get_parent_uid_and_item():
886
            if not parents or uid in parents:
887 1
                uid.stamp = item.stamp()
888
889
    @auto_save
890 1
    def review(self):
891
        """Mark the item as reviewed."""
892 1
        log.info("marking item as reviewed...")
893
        self._data['reviewed'] = self.stamp(links=True)
894 1
895
    @delete_item
896
    def delete(self, path=None):
897
        """Delete the item."""
898
899
900
class UnknownItem:
901
    """Represents an unknown item, which doesn't have a path."""
902
903
    UNKNOWN_PATH = '???'  # string to represent an unknown path
904
905
    normative = False  # do not include unknown items in traceability
906
    level = Item.DEFAULT_LEVEL
907
908
    def __init__(self, value, spec=Item):
909
        self._uid = UID(value)
910
        self._spec = dir(spec)  # list of attribute names for warnings
911
        msg = UID.UNKNOWN_MESSAGE.format(k='', u=self.uid)
912
        self.exception = DoorstopError(msg)
913
914
    def __str__(self):
915
        return Item.__str__(self)
916
917
    def __getattr__(self, name):
918
        if name in self._spec:
919
            log.debug(self.exception)
920
        return self.__getattribute__(name)
921
922
    def __lt__(self, other):
923
        return self.uid < other.uid
924
925
    @property
926
    def uid(self):
927
        """Get the item's UID."""
928
        return self._uid
929
930
    prefix = Item.prefix
931
    number = Item.number
932
933
    @property
934
    def relpath(self):
935
        """Get the unknown item's relative path string."""
936
        return "@{}{}".format(os.sep, self.UNKNOWN_PATH)
937
938
    def stamp(self):  # pylint: disable=R0201
939
        """Return an empty stamp."""
940
        return Stamp(None)
941