Skip to content

result

Attributes

Classes

MethodNotRegisteredError

Bases: Exception

Exception raised when trying to add an action with a method that is not registered as a plugin method.

Source code in pyflowlauncher/result.py
12
13
14
15
16
17
18
19
20
class MethodNotRegisteredError(Exception):
    """Exception raised when trying to add an action with a method that is not registered as a plugin method."""

    def __init__(self, method: Callable[..., Any]):
        self.method = method
        super().__init__(
            f"Method {method.__name__} is not registered as a plugin method. "
            "Please use the @plugin.on_method decorator to register it."
        )

Attributes

method instance-attribute
method = method

Functions

__init__
__init__(method: Callable[..., Any])
Source code in pyflowlauncher/result.py
15
16
17
18
19
20
def __init__(self, method: Callable[..., Any]):
    self.method = method
    super().__init__(
        f"Method {method.__name__} is not registered as a plugin method. "
        "Please use the @plugin.on_method decorator to register it."
    )

Result dataclass

Source code in pyflowlauncher/result.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@dataclass
class Result:
    title: str
    subtitle: Optional[str] = None
    icon: Optional[Union[str, Path]] = None
    score: int = 0
    json_rpc_action: Optional[JsonRPCRequest] = None
    context_data: Optional[Iterable] = None
    glyph: Optional[Glyph] = None
    copy_text: Optional[str] = None
    auto_complete_text: Optional[str] = None
    rounded_icon: bool = False
    preview: Optional[PreviewInfo] = None
    title_highlight_data: Optional[List[int]] = None

    def add_action(
        self, method: Method, parameters: Optional[Iterable[Any]] = None, dont_hide_after_action: bool = False
    ) -> None:
        """Adds a JsonRPC action to the result."""
        if not getattr(method, '_is_registered_method', False):
            raise MethodNotRegisteredError(method)
        self.json_rpc_action = {
            'Method': method.__name__,
            'Parameters': list(parameters) if parameters else [],
            'DontHideAfterAction': dont_hide_after_action,
        }

    def as_dict(self) -> Dict[str, Any]:
        return self.__dict__

    @staticmethod
    def from_json(json_result: JsonRPCResult) -> Result:
        """Creates a Result instance from a JsonRPCResult dictionary."""
        if 'Title' not in json_result:
            raise ValueError("JsonRPCResult must have a 'Title' field")
        return Result(
            title=json_result['Title'],
            subtitle=json_result.get('SubTitle'),
            icon=json_result.get('IcoPath'),
            score=json_result.get('Score', 0),
            json_rpc_action=json_result.get('JsonRPCAction'),
            context_data=json_result.get('ContextData'),
            glyph=json_result.get('Glyph'),
            copy_text=json_result.get('CopyText'),
            auto_complete_text=json_result.get('AutoCompleteText'),
            rounded_icon=json_result.get('RoundedIcon', False),
            preview=json_result.get('Preview'),
            title_highlight_data=list(json_result.get('TitleHighlightData', []))
        )

    def to_json(self) -> JsonRPCResult:
        """Converts the Result instance to a JsonRPCResult dictionary"""
        return cast(JsonRPCResult, {
            'Title': self.title,
            'SubTitle': self.subtitle,
            'IcoPath': str(self.icon) if self.icon else None,
            'Score': self.score,
            'JsonRPCAction': self.json_rpc_action,
            'ContextData': self.context_data,
            'Glyph': self.glyph,
            'CopyText': self.copy_text,
            'AutoCompleteText': self.auto_complete_text,
            'RoundedIcon': self.rounded_icon,
            'Preview': self.preview,
            'TitleHighlightData': self.title_highlight_data,
        })

Attributes

auto_complete_text class-attribute instance-attribute
auto_complete_text: Optional[str] = None
context_data class-attribute instance-attribute
context_data: Optional[Iterable] = None
copy_text class-attribute instance-attribute
copy_text: Optional[str] = None
glyph class-attribute instance-attribute
glyph: Optional[Glyph] = None
icon class-attribute instance-attribute
icon: Optional[Union[str, Path]] = None
json_rpc_action class-attribute instance-attribute
json_rpc_action: Optional[JsonRPCRequest] = None
preview class-attribute instance-attribute
preview: Optional[PreviewInfo] = None
rounded_icon class-attribute instance-attribute
rounded_icon: bool = False
score class-attribute instance-attribute
score: int = 0
subtitle class-attribute instance-attribute
subtitle: Optional[str] = None
title instance-attribute
title: str
title_highlight_data class-attribute instance-attribute
title_highlight_data: Optional[List[int]] = None

Functions

__init__
__init__(title: str, subtitle: Optional[str] = None, icon: Optional[Union[str, Path]] = None, score: int = 0, json_rpc_action: Optional[JsonRPCRequest] = None, context_data: Optional[Iterable] = None, glyph: Optional[Glyph] = None, copy_text: Optional[str] = None, auto_complete_text: Optional[str] = None, rounded_icon: bool = False, preview: Optional[PreviewInfo] = None, title_highlight_data: Optional[List[int]] = None) -> None
add_action
add_action(method: Method, parameters: Optional[Iterable[Any]] = None, dont_hide_after_action: bool = False) -> None

Adds a JsonRPC action to the result.

Source code in pyflowlauncher/result.py
38
39
40
41
42
43
44
45
46
47
48
def add_action(
    self, method: Method, parameters: Optional[Iterable[Any]] = None, dont_hide_after_action: bool = False
) -> None:
    """Adds a JsonRPC action to the result."""
    if not getattr(method, '_is_registered_method', False):
        raise MethodNotRegisteredError(method)
    self.json_rpc_action = {
        'Method': method.__name__,
        'Parameters': list(parameters) if parameters else [],
        'DontHideAfterAction': dont_hide_after_action,
    }
as_dict
as_dict() -> Dict[str, Any]
Source code in pyflowlauncher/result.py
50
51
def as_dict(self) -> Dict[str, Any]:
    return self.__dict__
from_json staticmethod
from_json(json_result: JsonRPCResult) -> Result

Creates a Result instance from a JsonRPCResult dictionary.

Source code in pyflowlauncher/result.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@staticmethod
def from_json(json_result: JsonRPCResult) -> Result:
    """Creates a Result instance from a JsonRPCResult dictionary."""
    if 'Title' not in json_result:
        raise ValueError("JsonRPCResult must have a 'Title' field")
    return Result(
        title=json_result['Title'],
        subtitle=json_result.get('SubTitle'),
        icon=json_result.get('IcoPath'),
        score=json_result.get('Score', 0),
        json_rpc_action=json_result.get('JsonRPCAction'),
        context_data=json_result.get('ContextData'),
        glyph=json_result.get('Glyph'),
        copy_text=json_result.get('CopyText'),
        auto_complete_text=json_result.get('AutoCompleteText'),
        rounded_icon=json_result.get('RoundedIcon', False),
        preview=json_result.get('Preview'),
        title_highlight_data=list(json_result.get('TitleHighlightData', []))
    )
to_json
to_json() -> JsonRPCResult

Converts the Result instance to a JsonRPCResult dictionary

Source code in pyflowlauncher/result.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def to_json(self) -> JsonRPCResult:
    """Converts the Result instance to a JsonRPCResult dictionary"""
    return cast(JsonRPCResult, {
        'Title': self.title,
        'SubTitle': self.subtitle,
        'IcoPath': str(self.icon) if self.icon else None,
        'Score': self.score,
        'JsonRPCAction': self.json_rpc_action,
        'ContextData': self.context_data,
        'Glyph': self.glyph,
        'CopyText': self.copy_text,
        'AutoCompleteText': self.auto_complete_text,
        'RoundedIcon': self.rounded_icon,
        'Preview': self.preview,
        'TitleHighlightData': self.title_highlight_data,
    })

Functions

send_results

send_results(results: Iterable[Result], settings: Optional[Dict[str, Any]] = None) -> JsonRPCResponse

Formats and returns results as a JsonRPCResponse

Source code in pyflowlauncher/result.py
91
92
93
def send_results(results: Iterable[Result], settings: Optional[Dict[str, Any]] = None) -> JsonRPCResponse:
    """Formats and returns results as a JsonRPCResponse"""
    return {'Result': [result.to_json() for result in results], 'SettingsChange': settings}