Passed
Pull Request — develop (#399)
by
unknown
01:51
created

doorstop.core.item.Item.references()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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