Parser#
Superclasses: Object
A parser for parsing various types of files or data.
A GcrParser can parse various certificate and key files such as OpenSSL
PEM files, DER encoded certifictes, PKCS``8`` keys and so on. Each various
format is identified by a value in the DataFormat enumeration.
In order to parse data, a new parser is created with new and
then the authenticate and parsed signals
should be connected to. Data is then fed to the parser via
parse_data or parse_stream.
During the parsed signal the attributes that make up the
currently parsed item can be retrieved using the
get_parsed_attributes function.
Constructors#
Methods#
- class Parser
- add_password(password: str | None = None) None#
Add a password to the set of passwords to try when parsing locked or encrypted items. This is usually called from the
authenticatesignal.- Parameters:
password – a password to try
- format_disable(format: DataFormat) None#
Disable parsing of the given format. Use
ALLto disable all the formats.- Parameters:
format – The format identifier
- format_enable(format: DataFormat) None#
Enable parsing of the given format. Use
ALLto enable all the formats.- Parameters:
format – The format identifier
- format_supported(format: DataFormat) bool#
Check whether the given format is supported by the parser.
- Parameters:
format – The format identifier
- get_parsed_attributes() Attributes | None#
Get the attributes which make up the currently parsed item. This is generally only valid during the
parsedsignal.
- get_parsed_block() list[int] | None#
Get the raw data block that represents this parsed object. This is only valid during the
parsedsignal.
- get_parsed_bytes() Bytes#
Get the raw data block that represents this parsed object. This is only valid during the
parsedsignal.
- get_parsed_description() str | None#
Get a description for the type of the currently parsed item. This is generally only valid during the
parsedsignal.
- get_parsed_format() DataFormat#
Get the format of the raw data block that represents this parsed object. This corresponds with the data returned from
get_parsed_block.This is only valid during the
parsedsignal.
- get_parsed_label() str | None#
Get the label of the currently parsed item. This is generally only valid during the
parsedsignal.
- parse_bytes(data: Bytes) bool#
Parse the data. The
parsedandauthenticatesignals may fire during the parsing.- Parameters:
data – the data to parse
- parse_data(data: list[int]) bool#
Parse the data. The
parsedandauthenticatesignals may fire during the parsing.A copy of the data will be made. Use
parse_bytesto avoid this.- Parameters:
data – the data to parse
- parse_stream(input: InputStream, cancellable: Cancellable | None = None) bool#
Parse items from the data in a
InputStream. This function may block while reading from the input stream. Useparse_stream_asyncfor a non-blocking variant.The
parsedandauthenticatesignals may fire during the parsing.- Parameters:
input – The input stream
cancellable – An optional cancellation object
- async parse_stream_async(self, input: InputStream) bool#
This is the awaitable version of
parse_stream_async().- Parameters:
input – The input stream
- parse_stream_async(input: InputStream, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Parse items from the data in a
InputStream. This function completes asyncronously and doesn’t block.The
parsedandauthenticatesignals may fire during the parsing.- Parameters:
input – The input stream
cancellable – An optional cancellation object
callback – Called when the operation result is ready.
user_data – Data to pass to callback
- parse_stream_finish(result: AsyncResult) bool#
Complete an operation to parse a stream.
- Parameters:
result – The operation result
Properties#
- class Parser
- props.parsed_attributes: Attributes#
Get the attributes that make up the currently parsed item. This is generally only valid during a
parsedsignal.
Signals#
- class Parser.signals
- authenticate(count: int) bool#
This signal is emitted when an item needs to be unlocked or decrypted before it can be parsed. The
countargument specifies the number of times the signal has been emitted for a given item. This can be used to display a message saying the previous password was incorrect.Typically the
add_password()function is called in response to this signal.If
Falseis returned, then the authentication was not handled. If no handlers returnTruethen the item is not parsed and an error with the codeCANCELLEDwill be raised.- Parameters:
count – the number of times this item has been authenticated
- parsed() None#
This signal is emitted when an item is sucessfully parsed. To access the information about the item use the
get_parsed_label(),get_parsed_attributes()andget_parsed_description()functions.