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