import pgpy from email_validator import validate_email def import_key_from_attachment(file_path: str) -> (str, str, str): """Import a PGP public key from an attachment. :param file_path: the path to the attachment :return: a tuple with the public key, the email address, and a displayname """ try: key = pgpy.PGPKey.from_file(file_path)[0] except ValueError: return "", "", "" except AttributeError: return "", "", "" except pgpy.errors.PGPError: return "", "", "" ascii_key = str(key) keyparts = [] for line in ascii_key.splitlines(): if line != "" and not ": " in line and not line.startswith("=") and not line.startswith("-----"): keyparts.append(line) uid = key.get_uid("").userid email = validate_email(uid, allow_display_name=True).ascii_email display_name = validate_email(uid, allow_display_name=True).display_name return "".join(keyparts), email, display_name