Class MACreator
Bases:
LoInstPropsPartial
,EventsPartial
,JsonEncoder
Class for creating context action menu.
This class can also be used to convert menu data to a format that can be used with JSON.
Example
# ... menus = get_action_menu() json_str = json.dumps(menus, cls=ContextCreator, indent=4) with open("action_menu.json", "w") as f: f.write(json_str)
- Parameters:
lo_inst (LoInst | None) –
Constructor
- Parameters:
lo_inst (LoInst | None, optional) – LibreOffice instance. Defaults to
None
.kwargs (Any, optional) – Additional keyword arguments. (Used by JsonEncoder)
- Return type:
None
Adds observers that gets their
trigger
method called when this classtrigger
method is called.- Parameters:
args (EventObserver) – One or more observers to add.
- Return type:
None
Note
Observers are removed automatically when they are out of scope.
Create popup menu.
- Parameters:
menus (List[Dict[str, Any]]) – Menu Data.
- Return type:
List
[Dict
[str
,Any
]]
JsonEncoder default method.
- Parameters:
o (Any) – Data to be encoded.
- Returns:
Encoded data.
- Return type:
Any
Note
This method or the
on_json_encode()
can be overridden by subclasses to encode objects to JSON. If this class is a subclass ofEventsPartial
, thejson_encoding
event is triggered before encoding. The event data is a dictionary with the keyobj
containing the object to be encoded. If the event dataresult
key is set then the value is returned as the result of the encoding.
Return a JSON string representation of a Python data structure.
>>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
Gets a dictionary that can be converted to JSON. This is an alternative to created json data. This is a more standard way that would not require this library to decode.
The dictionary created by this method can also be used with the
create
method to create an Action Item menu.The menu dictionaries can have data such as
{"command": ".uno:Cut", "module": ModuleNamesKind.SPREADSHEET_DOCUMENT}
. For json this sort of data does not work, this this method converts all menu data to a standard format that can be used with json.- Parameters:
menus (List[Dict[str, Any]]) – Action Item Menu Data. This is can be the same data used to create an Action Item menu.
- Return type:
List[Dict[str, Any]]
Note
Even though menu data such as
{"command": ".uno:Cut", "module": ModuleNamesKind.SPREADSHEET_DOCUMENT}
is not valid for json. It can still be encoded using thisContextCreator
class.- Example:
# ... menus = get_action_menu() json_str = json.dumps(menus, cls=ContextCreator, indent=4) with open("action_menu.json", "w") as f: f.write(json_str)
- Return type:
List
[Dict
[str
,Any
]]- Parameters:
menus (List[Dict[str, Any]]) –
Encode the given object and yield each string representation as available.
For example:
for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
Dump JSON data to file.
- Parameters:
file (Any) – File path.
menus (List[Dict[str, Any]]) – Menu Data.
dynamic (bool, optional) – Dynamic data. Defaults to
False
.
- Return type:
None
Get JSON data.
- Parameters:
menus (List[Dict[str, Any]]) – Menu Data.
dynamic (bool) –
- Returns:
JSON data.
- Return type:
str
Load JSON data from file.
- Parameters:
json_file (str) – JSON file path.
- Returns:
Menu Data.
- Return type:
List[Dict[str, Any]]
Load JSON data.
- Parameters:
json_str (str) – JSON data.
- Returns:
Menu Data.
- Return type:
List[Dict[str, Any]]
Protected method to encode object to JSON. Can be overridden by subclasses.
- Parameters:
obj (Any) – Object to encode. This is the object that json is currently encoding.
- Returns:
The result of the encoding. The default is
NULL_OBJ
which means that the encoding is not handled.- Return type:
Any
Removes an observer
- Parameters:
observer (EventObserver) – One or more observers to add.
- Returns:
True
if observer has been removed; Otherwise,False
.- Return type:
bool
Subscribe on Action Container created event.
The callback
event_data
is a dictionary with keys:container
: ActionTriggerContainer instance
- Return type:
None
- Parameters:
callback (Callable[[Any, EventArgs], None]) –
Subscribe on after process event.
The callback
event_data
is a dictionary with keys:container
: ActionTriggerContainer instanceaction_item
: Action Item instance
- Return type:
None
- Parameters:
callback (Callable[[Any, EventArgs], None]) –
Subscribe on before process event.
The callback
event_data
is a dictionary with keys:container
: ActionTriggerContainer instanceaction_item
: Action Item instance
- Return type:
None
- Parameters:
callback (Callable[[Any, CancelEventArgs], None]) –
Add an event listener to current instance.
- Parameters:
event_name (str) – Event Name.
callback (EventCallback) – Callback of the event listener.
- Return type:
None
Subscribe on no text found for module menu entry.
This event occurs when a module menu entry is created using the
module
key and the text for the menu entry is not found. This event will not be raised if themodule
entry also provides atext
key. Atext
key can be provided to the module entry to provide a valid menu text as a replacement if not found.The callback
event_data
is a dictionary with keys:module_kind
: ModuleNamesKindcmd
: Command as a string.index
: Index as an integer.menu
: Menu Data as a dictionary.
The caller can set
menu["text"]
to provide a valid menu text. If the caller cancels the event then the menu item is not created.- Return type:
None
- Parameters:
callback (Callable[[Any, CancelEventArgs], None]) –
Trigger an event on current instance.
- Parameters:
event_name (str) – Event Name.
event_args (EventArgsT) – Event Args.
- Return type:
None
Unsubscribe on popup created event.
- Return type:
None
- Parameters:
callback (Callable[[Any, EventArgs], None]) –
Unsubscribe on after process event.
- Return type:
None
- Parameters:
callback (Callable[[Any, EventArgs], None]) –
Unsubscribe on before process event.
- Return type:
None
- Parameters:
callback (Callable[[Any, CancelEventArgs], None]) –
Remove an event listener from current instance.
- Parameters:
event_name (str) – Event Name.
callback (EventCallback) – Callback of the event listener.
- Return type:
None
Unsubscribe on no text found for module menu entry.
- Return type:
None
- Parameters:
callback (Callable[[Any, CancelEventArgs], None]) –
Gets/Sets The Event Observer for this instance.
- Return type:
Get key lookups.
- Returns:
Key lookups.
- Return type:
Dict[str, Any]
Lo Instance
- Return type: