Parser#

class Parser(**properties: Any)#

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#

class Parser
classmethod new() Parser#

Create a new Parser

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 authenticate signal.

Parameters:

password – a password to try

do_authenticate(self, count: int) bool#
Parameters:

count

do_parsed(self) None#
format_disable(format: DataFormat) None#

Disable parsing of the given format. Use ALL to disable all the formats.

Parameters:

format – The format identifier

format_enable(format: DataFormat) None#

Enable parsing of the given format. Use ALL to 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_filename() str#

Get the filename of the parser item.

get_parsed() Parsed#

Get the currently parsed item

get_parsed_attributes() Attributes | None#

Get the attributes which make up the currently parsed item. This is generally only valid during the parsed signal.

get_parsed_block() list[int] | None#

Get the raw data block that represents this parsed object. This is only valid during the parsed signal.

get_parsed_bytes() Bytes#

Get the raw data block that represents this parsed object. This is only valid during the parsed signal.

get_parsed_description() str | None#

Get a description for the type of the currently parsed item. This is generally only valid during the parsed signal.

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 parsed signal.

get_parsed_label() str | None#

Get the label of the currently parsed item. This is generally only valid during the parsed signal.

parse_bytes(data: Bytes) bool#

Parse the data. The parsed and authenticate signals may fire during the parsing.

Parameters:

data – the data to parse

parse_data(data: list[int]) bool#

Parse the data. The parsed and authenticate signals may fire during the parsing.

A copy of the data will be made. Use parse_bytes to 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. Use parse_stream_async for a non-blocking variant.

The parsed and authenticate signals 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 parsed and authenticate signals 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

set_filename(filename: str | None = None) None#

Sets the filename of the parser item.

Parameters:

filename – a string of the filename of the parser item

Properties#

class Parser
props.parsed_attributes: Attributes#

Get the attributes that make up the currently parsed item. This is generally only valid during a parsed signal.

props.parsed_description: str#

The description of the type of the currently parsed item. This is generally only valid during a parsed signal.

props.parsed_label: str#

The label of the currently parsed item. This is generally only valid during a parsed signal.

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 count argument 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 False is returned, then the authentication was not handled. If no handlers return True then the item is not parsed and an error with the code CANCELLED will 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() and get_parsed_description() functions.

Virtual Methods#

class Parser
do_authenticate(count: int) bool#

The default handler for the authenticate signal.

Parameters:

count

do_parsed() None#

The default handler for the parsed signal.

Fields#

class Parser
parent#
pv#