Esempio n. 1
0
def unpack_inventory_flat(elt,
                          format_num,
                          unpack_entry,
                          entry_cache=None,
                          return_from_cache=False):
    """Unpack a flat XML inventory.

    :param elt: XML element for the inventory
    :param format_num: Expected format number
    :param unpack_entry: Function for unpacking inventory entries
    :return: An inventory
    :raise UnexpectedInventoryFormat: When unexpected elements or data is
        encountered
    """
    if elt.tag != 'inventory':
        raise errors.UnexpectedInventoryFormat('Root tag is %r' % elt.tag)
    format = elt.get('format')
    if format != format_num:
        raise errors.UnexpectedInventoryFormat('Invalid format version %r' %
                                               format)
    revision_id = elt.get('revision_id')
    if revision_id is not None:
        revision_id = cache_utf8.encode(revision_id)
    inv = inventory.Inventory(root_id=None, revision_id=revision_id)
    for e in elt:
        ie = unpack_entry(e, entry_cache, return_from_cache)
        inv.add(ie)
    return inv
Esempio n. 2
0
 def _unpack_inventory(self, elt, revision_id=None):
     """Construct from XML Element"""
     if elt.tag != 'inventory':
         raise errors.UnexpectedInventoryFormat('Root tag is %r' % elt.tag)
     format = elt.get('format')
     if format != self.format_num:
         raise errors.UnexpectedInventoryFormat(
             'Invalid format version %r' % format)
     revision_id = elt.get('revision_id')
     if revision_id is not None:
         revision_id = cache_utf8.encode(revision_id)
     inv = inventory.Inventory(root_id=None, revision_id=revision_id)
     for e in elt:
         ie = self._unpack_entry(e)
         inv.add(ie)
     return inv
Esempio n. 3
0
    def read_inventory_from_string(self,
                                   xml_string,
                                   revision_id=None,
                                   entry_cache=None,
                                   return_from_cache=False):
        """Read xml_string into an inventory object.

        :param xml_string: The xml to read.
        :param revision_id: If not-None, the expected revision id of the
            inventory. Some serialisers use this to set the results' root
            revision. This should be supplied for deserialising all
            from-repository inventories so that xml5 inventories that were
            serialised without a revision identifier can be given the right
            revision id (but not for working tree inventories where users can
            edit the data without triggering checksum errors or anything).
        :param entry_cache: An optional cache of InventoryEntry objects. If
            supplied we will look up entries via (file_id, revision_id) which
            should map to a valid InventoryEntry (File/Directory/etc) object.
        :param return_from_cache: Return entries directly from the cache,
            rather than copying them first. This is only safe if the caller
            promises not to mutate the returned inventory entries, but it can
            make some operations significantly faster.
        """
        try:
            return self._unpack_inventory(fromstring(xml_string),
                                          revision_id,
                                          entry_cache=entry_cache,
                                          return_from_cache=return_from_cache)
        except ParseError, e:
            raise errors.UnexpectedInventoryFormat(e)
Esempio n. 4
0
 def read_inventory(self, f, revision_id=None):
     try:
         try:
             return self._unpack_inventory(self._read_element(f),
                                           revision_id=None)
         finally:
             f.close()
     except ParseError, e:
         raise errors.UnexpectedInventoryFormat(e)
Esempio n. 5
0
 def read_inventory(self, f, revision_id=None):
     """Read an inventory from a file-like object."""
     try:
         try:
             return self._unpack_inventory(self._read_element(f),
                 revision_id=None)
         finally:
             f.close()
     except xml_serializer.ParseError, e:
         raise errors.UnexpectedInventoryFormat(e)
Esempio n. 6
0
    def read_inventory_from_string(self, xml_string, revision_id=None):
        """Read xml_string into an inventory object.

        :param xml_string: The xml to read.
        :param revision_id: If not-None, the expected revision id of the
            inventory. Some serialisers use this to set the results' root
            revision. This should be supplied for deserialising all
            from-repository inventories so that xml5 inventories that were
            serialised without a revision identifier can be given the right
            revision id (but not for working tree inventories where users can
            edit the data without triggering checksum errors or anything).
        """
        try:
            return self._unpack_inventory(fromstring(xml_string), revision_id)
        except ParseError, e:
            raise errors.UnexpectedInventoryFormat(e)
Esempio n. 7
0
    def read_inventory_from_string(self, xml_string, revision_id=None,
                                   entry_cache=None, return_from_cache=False):
        """Read xml_string into an inventory object.

        :param xml_string: The xml to read.
        :param revision_id: If not-None, the expected revision id of the
            inventory.
        :param entry_cache: An optional cache of InventoryEntry objects. If
            supplied we will look up entries via (file_id, revision_id) which
            should map to a valid InventoryEntry (File/Directory/etc) object.
        :param return_from_cache: Return entries directly from the cache,
            rather than copying them first. This is only safe if the caller
            promises not to mutate the returned inventory entries, but it can
            make some operations significantly faster.
        """
        try:
            return self._unpack_inventory(
                xml_serializer.fromstring(xml_string), revision_id,
                entry_cache=entry_cache,
                return_from_cache=return_from_cache)
        except xml_serializer.ParseError, e:
            raise errors.UnexpectedInventoryFormat(e)