Ejemplo n.º 1
0
    def create_group(self, title, parent=None, icon=1, expires=None):
        """
        This method creates a new group.

        A group title is needed or no group will be created.

        If a parent is given, the group will be created as a sub-group.

        title must be a string, image an unsigned int >0 and parent a Group.
        
        :return: The newly created group.
        :rtype: :class:`keepassdb.model.Group`
        """
        if parent and not isinstance(parent, Group):
            raise TypeError("Parent must be of type Group")

        if expires is None:
            expires = const.NEVER

        if self.groups:
            group_id = max([g.id for g in self.groups]) + 1
        else:
            group_id = 1

        group = Group(id=group_id,
                      title=title,
                      icon=icon,
                      db=self,
                      created=util.now(),
                      modified=util.now(),
                      accessed=util.now(),
                      expires=expires)

        # If no parent is given, just append the new group at the end
        if parent is None:
            group.parent = self.root
            self.root.children.append(group)
            group.level = 0
            self.groups.append(group)

        # Else insert the group behind the parent
        else:
            if parent not in self.groups:
                raise ValueError(
                    "Group doesn't exist / is not bound to this database.")
            parent.children.append(group)
            group.parent = parent
            group.level = parent.level + 1
            self.groups.insert(self.groups.index(parent) + 1, group)

        return group
Ejemplo n.º 2
0
    def __init__(self,
                 id=None,
                 title=None,
                 icon=None,
                 level=None,
                 created=None,
                 modified=None,
                 accessed=None,
                 expires=None,
                 flags=None,
                 parent=None,
                 db=None):
        """
        Initialize a new Group object with optional attributes.
        
        Use the :method:`Group.parse` class method if you would like to initialize a group
        from the data structure.
        """
        super(Group, self).__init__()
        if icon is None:
            icon = 1
        if created is None:
            created = util.now()
        if modified is None:
            modified = util.now()
        if accessed is None:
            accessed = util.now()
        if expires is None:
            expires = const.NEVER
        if flags is None:
            flags = 0  # XXX: Need to figure out what this is, but 0 seems to be the correct default

        self.id = id
        self._title = title
        self._icon = icon
        self.level = level
        self.created = created
        self.modified = modified
        self.accessed = accessed
        self._expires = expires
        self.flags = flags

        # TODO: Determine how we want to handle these other attributes.
        self.parent = parent
        self.db = db
        self.children = []
        self.entries = []
Ejemplo n.º 3
0
    def create_group(self, title, parent=None, icon=1, expires=None):
        """
        This method creates a new group.

        A group title is needed or no group will be created.

        If a parent is given, the group will be created as a sub-group.

        title must be a string, image an unsigned int >0 and parent a Group.
        
        :return: The newly created group.
        :rtype: :class:`keepassdb.model.Group`
        """
        if parent and not isinstance(parent, Group):
            raise TypeError("Parent must be of type Group")
        
        if expires is None:
            expires = const.NEVER
        
        if self.groups:
            group_id = max([g.id for g in self.groups]) + 1
        else:
            group_id = 1
        
        group = Group(id=group_id, title=title, icon=icon, db=self, 
                      created=util.now(), modified=util.now(), accessed=util.now(),
                      expires=expires)
        
        # If no parent is given, just append the new group at the end
        if parent is None:
            group.parent = self.root
            self.root.children.append(group)
            group.level = 0
            self.groups.append(group)
            
        # Else insert the group behind the parent
        else:
            if parent not in self.groups:
                raise ValueError("Group doesn't exist / is not bound to this database.")
            parent.children.append(group)
            group.parent = parent
            group.level = parent.level + 1
            self.groups.insert(self.groups.index(parent) + 1, group)
                
        return group
Ejemplo n.º 4
0
    def create_entry(self, group, **kwargs):
        """
        Create a new Entry object.
        
        The group which should hold the entry is needed.

        image must be an unsigned int >0, group a Group.
        
        :param group: The associated group.
        :keyword title: 
        :keyword icon:
        :keyword url:
        :keyword username:
        :keyword password:
        :keyword notes:
        :keyword expires: Expiration date (if None, entry will never expire). 
        :type expires: datetime
         
        :return: The new entry.
        :rtype: :class:`keepassdb.model.Entry`
        """
        if group not in self.groups:
            raise ValueError(
                "Group doesn't exist / is not bound to this database.")

        uuid = binascii.hexlify(get_random_bytes(16))

        entry = Entry(uuid=uuid,
                      group_id=group.id,
                      created=util.now(),
                      modified=util.now(),
                      accessed=util.now(),
                      **kwargs)

        self.entries.append(entry)
        group.entries.append(entry)

        return entry
Ejemplo n.º 5
0
    def move_group(self, group, parent, index=None):
        """
        Move group to be a child of new parent.
        
        :param group: The group to move.
        :type group: :class:`keepassdb.model.Group`
        :param parent: The new parent for the group.
        :type parent: :class:`keepassdb.model.Group`
        :param index: The 0-based index within the parent (defaults to appending
                      group to end of parent's children).
        :type index: int
        """
        if not isinstance(group, Group):
            raise TypeError("group param must be of type Group")

        if parent is not None and not isinstance(parent, Group):
            raise TypeError("parent param must be of type Group")

        if group is parent:
            raise ValueError("group and parent are the same")

        if parent is None:
            parent = self.root

        if group not in self.groups:
            raise exc.UnboundModelError(
                "Group doesn't exist / is not bound to this database.")

        if parent not in self.groups:
            raise exc.UnboundModelError(
                "Parent group doesn't exist / is not bound to this database.")

        curr_parent = group.parent
        curr_parent.children.remove(group)

        if index is None:
            parent.children.append(group)
            self.log.debug(
                "Moving {0!r} to child of {1!r}, (appending)".format(
                    group, parent))
        else:
            parent.children.insert(index, group)
            self.log.debug(
                "Moving {0!r} to child of {1!r}, (at position {2!r})".format(
                    group, parent, index))

        group.parent = parent
        group.modified = util.now()

        self._rebuild_groups()
Ejemplo n.º 6
0
    def move_group(self, group, parent, index=None):
        """
        Move group to be a child of new parent.
        
        :param group: The group to move.
        :type group: :class:`keepassdb.model.Group`
        :param parent: The new parent for the group.
        :type parent: :class:`keepassdb.model.Group`
        :param index: The 0-based index within the parent (defaults to appending
                      group to end of parent's children).
        :type index: int
        """
        if not isinstance(group, Group):
            raise TypeError("group param must be of type Group")
        
        if parent is not None and not isinstance(parent, Group):
            raise TypeError("parent param must be of type Group")
            
        if group is parent:
            raise ValueError("group and parent are the same")
            
        if parent is None:
            parent = self.root
        elif parent not in self.groups:
            raise exc.UnboundModelError("Parent group doesn't exist / is not bound to this database.")
            
        if group not in self.groups:
            raise exc.UnboundModelError("Group doesn't exist / is not bound to this database.")
        
        curr_parent = group.parent
        curr_parent.children.remove(group)
        
        if index is None:
            parent.children.append(group)
            self.log.debug("Moving {0!r} to child of {1!r}, (appending)".format(group, parent))
        else:
            parent.children.insert(index, group)
            self.log.debug("Moving {0!r} to child of {1!r}, (at position {2!r})".format(group, parent, index))
        
        #Recurse down and reset level of all moved nodes
        def set_level(g):
            g.level = g.parent.level + 1
            for child in g.children:
                set_level(child)

        group.parent = parent
        set_level(group)
        group.modified = util.now()
        
        self._rebuild_groups()
Ejemplo n.º 7
0
    def create_entry(self, group, **kwargs):
        """
        Create a new Entry object.
        
        The group which should hold the entry is needed.

        image must be an unsigned int >0, group a Group.
        
        :param group: The associated group.
        :keyword title: 
        :keyword icon:
        :keyword url:
        :keyword username:
        :keyword password:
        :keyword notes:
        :keyword expires: Expiration date (if None, entry will never expire). 
        :type expires: datetime
         
        :return: The new entry.
        :rtype: :class:`keepassdb.model.Entry`
        """
        if group not in self.groups:
            raise ValueError("Group doesn't exist / is not bound to this database.")
                 
        uuid = binascii.hexlify(get_random_bytes(16))
        
        entry = Entry(uuid=uuid,
                      group_id=group.id,
                      created=util.now(),
                      modified=util.now(),
                      accessed=util.now(),
                      **kwargs)
        
        self.entries.append(entry)
        group.entries.append(entry)
        
        return entry
Ejemplo n.º 8
0
 def __init__(self, id=None, title=None, icon=None, level=None, created=None, modified=None,
              accessed=None, expires=None, flags=None, parent=None, db=None):
     """
     Initialize a new Group object with optional attributes.
     
     Use the :method:`Group.parse` class method if you would like to initialize a group
     from the data structure.
     """
     super(Group, self).__init__()
     if icon is None:
         icon = 1            
     if created is None:
         created = util.now()
     if modified is None:
         modified = util.now()
     if accessed is None:
         accessed = util.now()
     if expires is None:
         expires = const.NEVER
     if flags is None:
         flags = 0  # XXX: Need to figure out what this is, but 0 seems to be the correct default
     
     self.id = id
     self._title = title
     self._icon = icon
     self.level = level
     self.created = created
     self.modified = modified
     self.accessed = accessed
     self._expires = expires
     self.flags = flags
     
     # TODO: Determine how we want to handle these other attributes.
     self.parent = parent
     self.db = db
     self.children = []
     self.entries = []
Ejemplo n.º 9
0
    def move_entry(self, entry, group, index=None):
        """
        Move an entry to another group.
        
        :param entry: The Entry object to move.
        :type entry: :class:`keepassdb.model.Entry`
        
        :param group: The new parent Group object for the entry.
        :type group: :class:`keepassdb.model.Group`
        
        :param index: The 0-based index within the parent (defaults to appending
                      group to end of parent's children).
        :type index: int
        """
        if not isinstance(entry, Entry):
            raise TypeError("entry param must be of type Entry")
        if not isinstance(group, Group):
            raise TypeError("group param must be of type Group")

        if entry not in self.entries:
            raise exc.UnboundModelError(
                "Invalid entry (or not bound to this database): {0!r}".format(
                    entry))
        if group not in self.groups:
            raise exc.UnboundModelError(
                "Invalid group (or not bound to this database): {0!r}".format(
                    group))

        curr_group = entry.group

        curr_group.entries.remove(entry)
        if index is None:
            group.entries.append(entry)
            self.log.debug(
                "Moving {0!r} to child of {1!r}, (appending)".format(
                    entry, group))
        else:
            group.entries.insert(index, entry)
            self.log.debug(
                "Moving {0!r} to child of {1!r}, (at position {2})".format(
                    entry, group, index))

        entry.group = group

        entry.modified = util.now()

        self._rebuild_entries()
Ejemplo n.º 10
0
 def move_entry(self, entry, group, index=None):
     """
     Move an entry to another group.
     
     :param entry: The Entry object to move.
     :type entry: :class:`keepassdb.model.Entry`
     
     :param group: The new parent Group object for the entry.
     :type group: :class:`keepassdb.model.Group`
     
     :param index: The 0-based index within the parent (defaults to appending
                   group to end of parent's children).
     :type index: int
     """
     if not isinstance(entry, Entry):
         raise TypeError("entry param must be of type Entry")
     if not isinstance(group, Group):
         raise TypeError("group param must be of type Group")
     
     if entry not in self.entries:
         raise exc.UnboundModelError("Invalid entry (or not bound to this database): {0!r}".format(entry))
     if group not in self.groups:
         raise exc.UnboundModelError("Invalid group (or not bound to this database): {0!r}".format(group))
     
     curr_group = entry.group
     
     curr_group.entries.remove(entry)
     if index is None:
         group.entries.append(entry)
         self.log.debug("Moving {0!r} to child of {1!r}, (appending)".format(entry, group))
     else:
         group.entries.insert(index, entry)
         self.log.debug("Moving {0!r} to child of {1!r}, (at position {2})".format(entry, group, index))
         
     entry.group = group
     
     entry.modified = util.now()
     
     self._rebuild_entries()
Ejemplo n.º 11
0
 def url(self, value):
     self._url = value
     self.modified = util.now()
Ejemplo n.º 12
0
 def icon(self, value):
     self._icon = value
     self.modified = util.now()
Ejemplo n.º 13
0
 def title(self, value):
     self._title = value
     self.modified = util.now()
Ejemplo n.º 14
0
 def username(self, value):
     self._username = value
     self.modified = util.now()
Ejemplo n.º 15
0
 def url(self, value):
     self._url = value
     self.modified = util.now()
Ejemplo n.º 16
0
 def notes(self, value):
     self._notes = value
     self.modified = util.now()
Ejemplo n.º 17
0
 def password(self, value):
     self._password = value
     self.modified = util.now()
Ejemplo n.º 18
0
 def expires(self, value):
     self._expires = value
     self.modified = util.now()
Ejemplo n.º 19
0
 def expires(self, value):
     self._expires = value
     self.modified = util.now()
Ejemplo n.º 20
0
 def password(self, value):
     self._password = value
     self.modified = util.now()
Ejemplo n.º 21
0
 def notes(self, value):
     self._notes = value
     self.modified = util.now()
Ejemplo n.º 22
0
 def username(self, value):
     self._username = value
     self.modified = util.now()
Ejemplo n.º 23
0
    def __init__(self, uuid = None, group_id = None, group = None,
                 icon = None, title = None, url = None, username = None,
                 password = None, notes = None, 
                 created = None, modified = None, accessed = None, 
                 expires = None, binary_desc = None, binary = None):
        """
        Initialize a Entry-instance with provided attributes.
        
        Typically Entry objects should be created using the :meth:`Group.create_entry` method 
        which will additionally bind the entry to the group.
        
        :keyword uuid: The ID for the entry.
        :type uuid: str (16 bytes)
        
        :keyword group_id: The numeric ID for the group.
        :type group_id: int
        
        :keyword group: The group object that this entity is related to.
        :type group: :class:`Group`
        
        :keyword icon: The icon identifier.
        :type icon: int

        :keyword title: The title for the entry.
        :type username: unicode

        :keyword username: The username.
        :type username: unicode
        
        :keyword password: The password
        :type password: unicode
        
        :keyword url: The entry URL.
        :type url: unicode
        
        :keyword notes: Notes/comment for the entry.
        :type notes: unicode
        
        :keyword created: When entry was created (default: now)
        :type created: :class:`datetime.datetime`
        
        :keyword modified: When entry was last modified (default: now)
        :type modified: :class:`datetime.datetime`
        
        :keyword accessed: When the entry was last accessed (default: now)
        :type accessed: :class:`datetime.datetime`
        
        :keyword expires: When the entry (password) expires.  Default will be :ref:`keepassdb.const.NEVER`.
        :type expires :class:`datetime.datetime`
        
        :keyword binary_desc: Description/metadata for the binary column.
        :type binary_desc: unicode
        
        :keyword binary: Binary contents.
        :type binary: str
        """
        super(Entry, self).__init__()
        if icon is None:
            icon = 1    
        if created is None:
            created = util.now()
        if modified is None:
            modified = util.now()
        if accessed is None:
            accessed = util.now()
        if expires is None:
            expires = const.NEVER
        
        # Some casting to strings here, since this is what we'll get when we read
        # these entries from the database.  (Probably needs to be more comprehensive.)
        if title is None: title = u''
        if notes is None: notes = u''
        if url is None: url = u''
        if binary is None: binary = b''
        if binary_desc is None: binary_desc = u''
        
        self.uuid = uuid
        self.group_id = group_id
        self.group = group
        
        # Property attributes
        self._icon = icon
        self._title = title
        self._url = url
        self._username = username
        self._password = password
        self._notes = notes
        
        self.created = created
        self.modified = modified
        self.accessed = accessed
        self._expires = expires
        self.binary_desc = binary_desc
        self.binary = binary
Ejemplo n.º 24
0
 def icon(self, value):
     self._icon = value
     self.modified = util.now()
Ejemplo n.º 25
0
 def title(self, value):
     self._title = value
     self.modified = util.now()
Ejemplo n.º 26
0
    def __init__(self,
                 uuid=None,
                 group_id=None,
                 group=None,
                 icon=None,
                 title=None,
                 url=None,
                 username=None,
                 password=None,
                 notes=None,
                 created=None,
                 modified=None,
                 accessed=None,
                 expires=None,
                 binary_desc=None,
                 binary=None):
        """
        Initialize a Entry-instance with provided attributes.
        
        Typically Entry objects should be created using the :meth:`Group.create_entry` method 
        which will additionally bind the entry to the group.
        
        :keyword uuid: The ID for the entry.
        :type uuid: str (16 bytes)
        
        :keyword group_id: The numeric ID for the group.
        :type group_id: int
        
        :keyword group: The group object that this entity is related to.
        :type group: :class:`Group`
        
        :keyword icon: The icon identifier.
        :type icon: int

        :keyword title: The title for the entry.
        :type username: unicode

        :keyword username: The username.
        :type username: unicode
        
        :keyword password: The password
        :type password: unicode
        
        :keyword url: The entry URL.
        :type url: unicode
        
        :keyword notes: Notes/comment for the entry.
        :type notes: unicode
        
        :keyword created: When entry was created (default: now)
        :type created: :class:`datetime.datetime`
        
        :keyword modified: When entry was last modified (default: now)
        :type modified: :class:`datetime.datetime`
        
        :keyword accessed: When the entry was last accessed (default: now)
        :type accessed: :class:`datetime.datetime`
        
        :keyword expires: When the entry (password) expires.  Default will be :ref:`keepassdb.const.NEVER`.
        :type expires :class:`datetime.datetime`
        
        :keyword binary_desc: Description/metadata for the binary column.
        :type binary_desc: unicode
        
        :keyword binary: Binary contents.
        :type binary: str
        """
        super(Entry, self).__init__()
        if icon is None:
            icon = 1
        if created is None:
            created = util.now()
        if modified is None:
            modified = util.now()
        if accessed is None:
            accessed = util.now()
        if expires is None:
            expires = const.NEVER

        # Some casting to strings here, since this is what we'll get when we read
        # these entries from the database.  (Probably needs to be more comprehensive.)
        if title is None: title = u''
        if notes is None: notes = u''
        if url is None: url = u''
        if binary is None: binary = b''
        if binary_desc is None: binary_desc = u''

        self.uuid = uuid
        self.group_id = group_id
        self.group = group

        # Property attributes
        self._icon = icon
        self._title = title
        self._url = url
        self._username = username
        self._password = password
        self._notes = notes

        self.created = created
        self.modified = modified
        self.accessed = accessed
        self._expires = expires
        self.binary_desc = binary_desc
        self.binary = binary