Skip to content

Module gnupg.handlers.search_keys_handler

View Source
from __future__ import annotations

import re

from typing import TYPE_CHECKING

from .helper import _set_fields

from .status_handler import StatusHandler

if TYPE_CHECKING:

    from gnupg.gnupg import GPG

ESCAPE_PATTERN = re.compile(r"\\x([0-9a-f][0-9a-f])", re.IGNORECASE)

BASIC_ESCAPES = {

    r"\n": "\n",

    r"\r": "\r",

    r"\f": "\f",

    r"\v": "\v",

    r"\b": "\b",

    r"\0": "\0",

}

class SearchKeysHandler(StatusHandler, list):

    """

    This class handles status messages during key search.

    """

    # Handle pub and uid (relating the latter to the former).

    # Don't care about the rest

    UID_INDEX = 1

    FIELDS = "type keyid algo length date expires".split()

    returncode = None

    def __init__(self, gpg: GPG) -> None:

        StatusHandler.__init__(self, gpg)

        self.curkey = None

        self.fingerprints = []

        self.uids = []

    def get_fields(self, args: list[str]) -> dict[str, str]:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        result = {}

        _set_fields(result, self.FIELDS, args)

        result["uids"] = []

        result["sigs"] = []

        return result

    def pub(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        self.curkey = curkey = self.get_fields(args)

        self.append(curkey)

    def uid(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        uid = args[self.UID_INDEX]

        uid = ESCAPE_PATTERN.sub(lambda m: chr(int(m.group(1), 16)), uid)

        for k, v in BASIC_ESCAPES.items():

            uid = uid.replace(k, v)

        self.curkey["uids"].append(uid)

        self.uids.append(uid)

    def handle_status(self, key: str, value: str) -> None:  # pragma: no cover

        pass

Variables

BASIC_ESCAPES
ESCAPE_PATTERN
TYPE_CHECKING

Classes

SearchKeysHandler

class SearchKeysHandler(
    gpg: 'GPG'
)

This class handles status messages during key search.

View Source
class SearchKeysHandler(StatusHandler, list):

    """

    This class handles status messages during key search.

    """

    # Handle pub and uid (relating the latter to the former).

    # Don't care about the rest

    UID_INDEX = 1

    FIELDS = "type keyid algo length date expires".split()

    returncode = None

    def __init__(self, gpg: GPG) -> None:

        StatusHandler.__init__(self, gpg)

        self.curkey = None

        self.fingerprints = []

        self.uids = []

    def get_fields(self, args: list[str]) -> dict[str, str]:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        result = {}

        _set_fields(result, self.FIELDS, args)

        result["uids"] = []

        result["sigs"] = []

        return result

    def pub(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        self.curkey = curkey = self.get_fields(args)

        self.append(curkey)

    def uid(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        uid = args[self.UID_INDEX]

        uid = ESCAPE_PATTERN.sub(lambda m: chr(int(m.group(1), 16)), uid)

        for k, v in BASIC_ESCAPES.items():

            uid = uid.replace(k, v)

        self.curkey["uids"].append(uid)

        self.uids.append(uid)

    def handle_status(self, key: str, value: str) -> None:  # pragma: no cover

        pass

Ancestors (in MRO)

  • gnupg.handlers.status_handler.StatusHandler
  • builtins.list

Descendants

  • gnupg.handlers.list_keys_handler.ListKeysHandler

Class variables

FIELDS
UID_INDEX
returncode

Methods

append

def append(
    self,
    object,
    /
)

Append object to the end of the list.

clear

def clear(
    self,
    /
)

Remove all items from list.

copy

def copy(
    self,
    /
)

Return a shallow copy of the list.

count

def count(
    self,
    value,
    /
)

Return number of occurrences of value.

extend

def extend(
    self,
    iterable,
    /
)

Extend list by appending elements from the iterable.

get_fields

def get_fields(
    self,
    args: 'list[str]'
) -> 'dict[str, str]'

Internal method used to update the instance from a gpg status message.

View Source
    def get_fields(self, args: list[str]) -> dict[str, str]:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        result = {}

        _set_fields(result, self.FIELDS, args)

        result["uids"] = []

        result["sigs"] = []

        return result

handle_status

def handle_status(
    self,
    key: 'str',
    value: 'str'
) -> 'None'

Handle status messages from the gpg child process. These are lines of the format

[GNUPG:]

Parameters:

Name Type Description Default
key str Identifies what the status message is. None
value str Identifies additional data, which differs depending on the key. None
View Source
    def handle_status(self, key: str, value: str) -> None:  # pragma: no cover

        pass

index

def index(
    self,
    value,
    start=0,
    stop=9223372036854775807,
    /
)

Return first index of value.

Raises ValueError if the value is not present.

insert

def insert(
    self,
    index,
    object,
    /
)

Insert object before index.

pop

def pop(
    self,
    index=-1,
    /
)

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

pub

def pub(
    self,
    args: 'list[str]'
) -> 'None'

Internal method used to update the instance from a gpg status message.

View Source
    def pub(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        self.curkey = curkey = self.get_fields(args)

        self.append(curkey)

remove

def remove(
    self,
    value,
    /
)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse

def reverse(
    self,
    /
)

Reverse IN PLACE.

sort

def sort(
    self,
    /,
    *,
    key=None,
    reverse=False
)

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

uid

def uid(
    self,
    args: 'list[str]'
) -> 'None'

Internal method used to update the instance from a gpg status message.

View Source
    def uid(self, args: list[str]) -> None:

        """

        Internal method used to update the instance from a `gpg` status message.

        """

        uid = args[self.UID_INDEX]

        uid = ESCAPE_PATTERN.sub(lambda m: chr(int(m.group(1), 16)), uid)

        for k, v in BASIC_ESCAPES.items():

            uid = uid.replace(k, v)

        self.curkey["uids"].append(uid)

        self.uids.append(uid)