grp
Module¶
Source code: grp.py
The grp
module provides an GrpFile
class which
derives from ArchiveFile
and is used to read and write
Duke3D archive data.
- vgio.duke3d.grp.is_grpfile(filename)¶
Quickly see if a file is a grp file by checking the magic number.
The filename argument may be a file for file-like object.
- Parameters
filename – File to check as string or file-like object.
- Returns
True if given file’s magic number is correct.
GrpFile
Class¶
- class vgio.duke3d.grp.GrpFile¶
Class with methods to open, read, close, and list grp files.
Example
Basic usage:
from vgio.duke3d.grp import GrpFile grp_file = GrpFile(file, mode='r')
- 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 GrpFile.
- mode¶
The file mode for the file-like object.
- GrpFile.__init__(file, mode='r')¶
Open an Grp file, 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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.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.
- GrpFile.namelist()¶
Return a list of archive members by name.
- Returns
A sequence of filenames.
GrpInfo
Class¶
- class vgio.duke3d.grp.GrpInfo¶
Instances of the GrpInfo class are returned by the getinfo() and infolist() methods of GrpFile objects. Each object stores information about a single member of the GrpFile archive.
- filename¶
Name of file.
- file_offset¶
Offset of file in bytes.
- file_size¶
Size of the file in bytes.
- GrpInfo.__init__(filename, file_offset=0, file_size=0)¶
Constructs a GrpInfo object.