diff --git a/pykeepass/entry.py b/pykeepass/entry.py index e0c74f2d..30fdd594 100644 --- a/pykeepass/entry.py +++ b/pykeepass/entry.py @@ -29,6 +29,8 @@ ] # FIXME python2 + + @python_2_unicode_compatible class Entry(BaseElement): @@ -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))) @@ -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 @@ -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 @@ -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 @@ -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')] @@ -120,6 +128,7 @@ 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 @@ -127,6 +136,11 @@ def index(self): 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) @@ -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) @@ -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 @@ -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)