Skip to content

Commit

Permalink
docs: add docstrings to reindex and index
Browse files Browse the repository at this point in the history
  • Loading branch information
pwall2222 committed Nov 10, 2023
1 parent 97095af commit d206474
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions pykeepass/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
]

# FIXME python2


@python_2_unicode_compatible
class Entry(BaseElement):

Expand All @@ -48,9 +50,11 @@ def __init__(self, title=None, username=None, password=None, url=None,
icon=icon
)
self._element.append(E.String(E.Key('Title'), E.Value(title)))
self._element.append(E.String(E.Key('UserName'), E.Value(username)))
self._element.append(
E.String(E.Key('Password'), E.Value(password, Protected="True"))
E.String(E.Key('UserName'), E.Value(username)))
self._element.append(
E.String(E.Key('Password'), E.Value(
password, Protected="True"))
)
if url:
self._element.append(E.String(E.Key('URL'), E.Value(url)))
Expand All @@ -68,7 +72,8 @@ def __init__(self, title=None, username=None, password=None, url=None,
E.AutoType(
E.Enabled(str(autotype_enabled)),
E.DataTransferObfuscation('0'),
E.DefaultSequence(str(autotype_sequence) if autotype_sequence else '')
E.DefaultSequence(str(autotype_sequence)
if autotype_sequence else '')
)
)
# FIXME: include custom_properties in constructor
Expand All @@ -77,7 +82,7 @@ def __init__(self, title=None, username=None, password=None, url=None,
assert type(element) in [_Element, Element, ObjectifiedElement], \
'The provided element is not an LXML Element, but a {}'.format(
type(element)
)
)
assert element.tag == 'Entry', 'The provided element is not an Entry '\
'element, but a {}'.format(element.tag)
self._element = element
Expand All @@ -92,7 +97,8 @@ def _get_string_field(self, key):
(str or None): field value
"""

field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
field = self._xpath(
'String/Key[text()="{}"]/../Value'.format(key), first=True)
if field is not None:
return field.text

Expand All @@ -106,10 +112,12 @@ def _set_string_field(self, key, value, protected=True):
in other tools. This property is ignored in PyKeePass and all
fields are decrypted immediately upon opening the database.
"""
field = self._xpath('String/Key[text()="{}"]/..'.format(key), first=True)
field = self._xpath(
'String/Key[text()="{}"]/..'.format(key), first=True)
if field is not None:
self._element.remove(field)
self._element.append(E.String(E.Key(key), E.Value(value, Protected=str(protected))))
self._element.append(
E.String(E.Key(key), E.Value(value, Protected=str(protected))))

def _get_string_field_keys(self, exclude_reserved=False):
results = [x.find('Key').text for x in self._element.findall('String')]
Expand All @@ -120,13 +128,19 @@ def _get_string_field_keys(self, exclude_reserved=False):

@property
def index(self):
"""int: get index of a entry within a group"""
group = self.group._element
children = group.getchildren()
first_index = self.group._first_entry
index = children.index(self._element)
return index - first_index

def reindex(self, new_index):
"""Move entry to a new index within a group
Args:
new_index (int): new index for the entry starting at 0
"""
group = self.group._element
first_index = self.group._first_entry
group.remove(self._element)
Expand Down Expand Up @@ -305,7 +319,8 @@ def get_custom_property(self, key):
def delete_custom_property(self, key):
if key not in self._get_string_field_keys(exclude_reserved=True):
raise AttributeError('No such key: {}'.format(key))
prop = self._xpath('String/Key[text()="{}"]/..'.format(key), first=True)
prop = self._xpath(
'String/Key[text()="{}"]/..'.format(key), first=True)
if prop is None:
raise AttributeError('Could not find property element')
self._element.remove(prop)
Expand All @@ -324,7 +339,8 @@ def is_custom_property_protected(self, key):
"""
assert key not in reserved_keys, '{} is a reserved key'.format(key)
field = self._xpath('String/Key[text()="{}"]/../Value'.format(key), first=True)
field = self._xpath(
'String/Key[text()="{}"]/../Value'.format(key), first=True)
if field is not None:
return field.attrib.get("Protected", "False") == "True"
return False
Expand Down Expand Up @@ -389,7 +405,7 @@ def delete_history(self, history_entry=None, all=False):

def __str__(self):
# filter out NoneTypes and join into string
pathstr = '/'.join('' if p==None else p for p in self.path)
pathstr = '/'.join('' if p == None else p for p in self.path)
return 'Entry: "{} ({})"'.format(pathstr, self.username)


Expand Down

0 comments on commit d206474

Please sign in to comment.