Skip to content

Commit

Permalink
Merge bdfa9e6 into c0484b6
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhub committed Jul 17, 2019
2 parents c0484b6 + bdfa9e6 commit f66d370
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
4 changes: 2 additions & 2 deletions docs/reference/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ e.g. `level: '1.10'`.
Each item has a fingerprint. By default, the UID of the item, the values of the
[text](items.md#text) and [ref](items.md#ref) attributes, and the UIDs of the
[links](items.md#links) attribute contribute to the fingerprint. Optionally,
values of extended attributes can be added to the fingerprint through the
[extended-reviewed](items.md#extended-reviewed-attributes) document setting.
values of extended attributes can be added to the fingerprint through a
[document configuration option](items.md#extended-reviewed-attributes).

The value of the *reviewed* attribute indicates the fingerprint of the item
when it was last reviewed. "null" if the item has not yet been reviewed.
Expand Down
60 changes: 26 additions & 34 deletions doorstop/core/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,9 @@ def new(
# Return the item
return item

def load(self, reload=False):
"""Load the item's properties from its file."""
if self._loaded and not reload:
return
log.debug("loading {}...".format(repr(self)))
# Read text from file
text = self._read(self.path)
# Parse YAML data from text
data = self._load(text, self.path)
# Store parsed data
for key, value in data.items():
def _set_attributes(self, attributes):
"""Set the item's attributes."""
for key, value in attributes.items():
if key == 'level':
value = Level(value)
elif key == 'active':
Expand All @@ -179,6 +171,18 @@ def load(self, reload=False):
if isinstance(value, str):
value = Text(value)
self._data[key] = value

def load(self, reload=False):
"""Load the item's properties from its file."""
if self._loaded and not reload:
return
log.debug("loading {}...".format(repr(self)))
# Read text from file
text = self._read(self.path)
# Parse YAML data from text
data = self._load(text, self.path)
# Store parsed data
self._set_attributes(data)
# Set meta attributes
self._loaded = True

Expand All @@ -187,20 +191,18 @@ def save(self):
"""Format and save the item's properties to its file."""
log.debug("saving {}...".format(repr(self)))
# Format the data items
data = self.data
data = self._yaml_data()
# Dump the data to YAML
text = self._dump(data)
# Save the YAML to file
self._write(text, self.path)
# Set meta attributes
self._loaded = False
self._loaded = True
self.auto = True

# properties #############################################################

@property
@auto_load
def data(self):
def _yaml_data(self):
"""Get all the item's data formatted for YAML dumping."""
data = {}
for key, value in self._data.items():
Expand Down Expand Up @@ -231,6 +233,12 @@ def data(self):
data[key] = value
return data

@property
@auto_load
def data(self):
"""Load and get all the item's data formatted for YAML dumping."""
return self._yaml_data()

@property
def uid(self):
"""Get the item's UID."""
Expand All @@ -255,7 +263,6 @@ def level(self):

@level.setter
@auto_save
@auto_load
def level(self, value):
"""Set the item's level."""
self._data['level'] = Level(value)
Expand Down Expand Up @@ -283,7 +290,6 @@ def active(self):

@active.setter
@auto_save
@auto_load
def active(self, value):
"""Set the item's active status."""
self._data['active'] = to_bool(value)
Expand All @@ -302,7 +308,6 @@ def derived(self):

@derived.setter
@auto_save
@auto_load
def derived(self, value):
"""Set the item's derived status."""
self._data['derived'] = to_bool(value)
Expand All @@ -324,7 +329,6 @@ def normative(self):

@normative.setter
@auto_save
@auto_load
def normative(self, value):
"""Set the item's normative status."""
self._data['normative'] = to_bool(value)
Expand All @@ -340,7 +344,6 @@ def heading(self):

@heading.setter
@auto_save
@auto_load
def heading(self, value):
"""Set the item's heading status."""
heading = to_bool(value)
Expand All @@ -365,7 +368,6 @@ def cleared(self):

@cleared.setter
@auto_save
@auto_load
def cleared(self, value):
"""Set the item's suspect link status."""
self.clear(_inverse=not to_bool(value))
Expand All @@ -381,7 +383,6 @@ def reviewed(self):

@reviewed.setter
@auto_save
@auto_load
def reviewed(self, value):
"""Set the item's review status."""
self._data['reviewed'] = Stamp(value)
Expand All @@ -394,7 +395,6 @@ def text(self):

@text.setter
@auto_save
@auto_load
def text(self, value):
"""Set the item's text."""
self._data['text'] = Text(value)
Expand All @@ -409,7 +409,6 @@ def header(self):

@header.setter
@auto_save
@auto_load
def header(self, value):
"""Set the item's header."""
if settings.ENABLE_HEADERS:
Expand All @@ -428,7 +427,6 @@ def ref(self):

@ref.setter
@auto_save
@auto_load
def ref(self, value):
"""Set the item's external file reference."""
self._data['ref'] = str(value) if value else ""
Expand All @@ -441,7 +439,6 @@ def links(self):

@links.setter
@auto_save
@auto_load
def links(self, value):
"""Set the list of item UIDs this item links to."""
self._data['links'] = set(UID(v) for v in value)
Expand Down Expand Up @@ -490,9 +487,8 @@ def parent_documents(self):

@auto_save
def set_attributes(self, attributes):
"""Set the specified item's attributes."""
for key, value in attributes.items():
self._data[key] = value
"""Set the item's attributes and save them."""
self._set_attributes(attributes)

@auto_save
def edit(self, tool=None, edit_all=True):
Expand Down Expand Up @@ -523,7 +519,6 @@ def edit(self, tool=None, edit_all=True):
self._loaded = False

@auto_save
@auto_load
def link(self, value):
"""Add a new link to another item UID.
Expand All @@ -535,7 +530,6 @@ def link(self, value):
self._data['links'].add(uid)

@auto_save
@auto_load
def unlink(self, value):
"""Remove an existing link by item UID.
Expand Down Expand Up @@ -874,7 +868,6 @@ def stamp(self, links=False):
return Stamp(*values)

@auto_save
@auto_load
def clear(self, _inverse=False):
"""Clear suspect links."""
log.info("clearing suspect links...")
Expand All @@ -888,7 +881,6 @@ def clear(self, _inverse=False):
uid.stamp = item.stamp()

@auto_save
@auto_load
def review(self):
"""Mark the item as reviewed."""
log.info("marking item as reviewed...")
Expand Down
11 changes: 9 additions & 2 deletions doorstop/core/tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
normative: true
ref: ''
reviewed: null
text: ''
text: |
something
""".lstrip()


Expand Down Expand Up @@ -103,7 +104,13 @@ def test_save_empty(self):
def test_set_attributes(self):
"""Verify setting attributes calls write with the attributes."""
self.item.set_attributes(
{'a': ['b', 'c'], 'd': {'e': 'f', 'g': 'h'}, 'i': 'j', 'k': None}
{
'a': ['b', 'c'],
'd': {'e': 'f', 'g': 'h'},
'i': 'j',
'k': None,
'text': 'something',
}
)
self.item._write.assert_called_once_with(
YAML_EXTENDED_ATTRIBUTES, self.item.path
Expand Down

0 comments on commit f66d370

Please sign in to comment.