_core Subpackage

Source code: _core

The _core module provides several base classes for creating binary serialization classes.

Note

These classes must be subclassed as the don’t do anything useful on their own.

ReadWriteFile Class

class vgio._core.ReadWriteFile

ReadWriteFile serves as base class for serializing/deserialing binary data.

fp

The handle to the open file. Will be None if file closed.

mode

File mode. Is one of ‘r’, ‘w’, or ‘a’.

classmethod ReadWriteFile.open(file, mode='r')

Open a ReadWriteFile object where file can be a path to a file (a string), or a file-like object.

The mode parameter should be ‘r’ to read an existing file, ‘w’ to truncate and write a new file, or ‘a’ to append to an existing file.

open() is also a context manager and supports the with statement:

with ReadWriteFile.open('file.ext') as file:
    file.save('file2.ext')
Parameters
  • file – Either the path to the file, a file-like object, or bytes.

  • mode – An optional string that indicates which mode to open the file

Returns

An ReadWriteFile object constructed from the information read from the file-like object.

Raises
  • ValueError – If an invalid file mode is given.

  • TypeError – If attempting to write to a bytes object.

  • OSError – If the file argument is not a file-like object.

ReadWriteFile.close()

Closes the file pointer if possible. If mode is ‘w’ or ‘a’, the file will be written to.

ReadWriteFile.save(file)

Writes data to file.

Parameters

file – Either the path to the file, or a file-like object.

Raises

OSError – If file argument is not a file-like object.

ArchiveFile Class

class vgio._core.ArchiveFile(file, mode='r')

ArchiveFile serves as base class for working with binary archive data.

file

Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by ArchiveFile.

mode

File mode. Is one of ‘r’, ‘w’, or ‘a’.

ArchiveFile.open(name, mode='r')

Access a member of the archive as a binary file-like object. name can be either the name of a file within the archive or an ArchiveInfo object. The mode parameter, if included, must be ‘r’ (the default) or ‘w’.

open() is also a context manager and supports the with statement:

with ArchiveFile('archive.file') as archive_file:
    with archive_file.open('entry') as entry_file:
        print(entry_file.read())
Parameters
  • name – Name or ArchiveInfo object.

  • mode – File mode to open object.

Returns

A binary file-like object.

Raises
  • ValueError – If mode isn’t ‘r’ or ‘w’.

  • RuntimeError – If file was already closed.

ArchiveFile.close()

Close the archive file. You must call close() before exiting your program or essential records will not be written.

Raises

ValueError – If open writing handles exist.

ArchiveFile.read(name)

Return the bytes of the file name in the archive. name is the name of the file in the archive, or a ArchiveInfo object. The archive must be open for read or append.

Parameters

name – ArchiveInfo name.

Returns

File as bytes.

ArchiveFile.write(filename, arcname=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). The archive must be open with mode ‘w’, or ‘a’.

Parameters
  • filename

  • arcname – Optional. Name of the info object. If omitted filename will be used.

ArchiveFile.writestr(info_or_arcname, data)

Write a file into the archive. The contents is data, which may be either a string or a bytes instance; if it is a string, it is encoded as UTF-8 first. info_or_arcname is either the file name it will be given in the archive, or a ArchiveInfo instance. If it’s an instance, at least the filename must be given. The archive must be opened with mode ‘w’ or ‘a’.

Parameters
  • info_or_arcname

  • data – Data to be written. Either a string or bytes.

ArchiveFile.extract(member, path=None)

Extract a member from the archive to the current working directory; member must be its full name or a ArchiveInfo object. Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or an ArchiveInfo object.

Parameters
  • member – Either the name of the member to extract or a ArchiveInfo instance.

  • path – The directory to extract to. The current working directory will be used if None.

Returns

Path to extracted file.

ArchiveFile.extractall(path=None, members=None)

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist().

Parameters
  • path – The directory to extract to. The current working directory will be used if None.

  • members – The names of the members to extract. This must be a subset of the list returned by namelist(). All members will be extracted if None.

ArchiveFile.getinfo(name)

Return a ArchiveInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.

Parameters

name – AchiveInfo name.

Returns

An ArchiveInfo object.

Raises

KeyError – If no archive item exists for the given name.

ArchiveFile.infolist()

Return a list containing an ArchiveInfo object for each member of the archive. The objects are in the same order as their entries in the actual archive file on disk if an existing archive was opened.

Returns

A sequence of ArchiveInfo objects.

ArchiveFile.namelist()

Return a list of archive members by name.

Returns

A sequence of filenames.

ArchiveInfo Class

class vgio._core.ArchiveInfo(filename, file_offset=0, file_size=0)

ArchiveInfo objects store information about a single entry in the ArchiveFile archive. Instances of the ArchiveInfo class are returned by the getinfo() and infolist() methods of ArchiveFile objects.

filename

Name of file.

file_offset

Offset of file in bytes.

file_size

Size of the file in bytes.

classmethod ArchiveInfo.from_file(filename)

Construct an ArchiveInfo instance for a file on the filesystem, in preparation for adding it to an archive file. filename should be the path to a file or directory on the filesystem.

Parameters

filename – A path to a file or directory.

Returns

An ArchiveInfo object.