SourceFile objects are the interface between the OS storage and the repolib source_object
items. Files contain sources and comments and allow for loading sources from and saving them to disk. They also assign idents to sources in a way that ensures each source has a unique ident.
File objects contain the following attributes:
- SourceFile.name
The name of the file on disk. This does not include the file extension, as that is stored in
file-format
.
- SourceFile.path
A
Pathlib.Path
object representing this file's actual path on disk. Note that the file may not actually exist on disk yet.
- SourceFile.format
The format this file should be saved in. Saved as a
enum_sourceformat
.
- SourceFile.contents
A
list
containing, in order, every comment line and source in this file.
- SourceFile.sources
A
list
containing, in order, only the sources in this file.
- SourceFile.add_source(source) -> None
Adds a given source to the file. This correctly appends the source to both the
file-contents
andfile-sources
lists, and sets thesource-file
attribute of the source to this file.
The source to add to the file.
- SourceFile.remove_source(ident: str) -> None
Removes the source with a specified ident from this file.
The ident of the source to remove from the file.
- SourceFile.get_source_by_ident(ident: str) ->
Source
Finds a source within this file given a specified ident and returns the
source_object
matching that ident. If the file does not contain a Source matching the given ident, raises aexc_sourcefileerror
.
The ident to look up.
- SourceFile.reset_path() -> None
Attempts to detect the full path to the file given the
file-name
attribute for this file. If thename.sources
exists on disk, the path will be set to that, otherwise if thename.list
exists, it will be set to that instead. Failing both, the path will fallback toname.sources
as a default.
- SourceFile.load() -> None
Loads the file specified by
path
from disk, creating sources and comments and appending them in order to thefile-contents
andfile-sources
lists as appropriate.
- SourceFile.save() -> None
Saves the file and any sources currently configured to disk. This method must be called to commit changes to disk. If the file currently contains no sources, then the file will instead be deleted.
There are four attributes which contain the output of the files stored as strings and which are ready for full output in the specified format.
- SourceFile.deb822
Outputs the entire file as DEB822-formatted sources
- SourceFile.legacy
Outputs the entire file as one-line legacy-formatted deb lines
- SourceFile.ui
Outputs the file in a format for output through a UI (e.g. for preview or external parsing.)
- SourceFile.output
Outputs the entire file in the format matching that configured in
file-format
.