context_menu package

Submodules

context_menu.example module

context_menu.linux_menus module

class context_menu.linux_menus.CodeBuilder(name: str, body_commands: list[str], script_dirs: list[str], funcs: list[str], imports: list[str], type: ActivationType | str)

Bases: object

The CodeBuilder class is used for generating the final python file for the Linux menus.

build_imports() → str

Creates the header of necessary imports.

Handled automatically by compile.

build_script_dirs() → str

Creates the header of necessary path configurations.

Adds all the ‘sys.path.appends’ in order to immport the classes and functions.

Handled automatically by compile.

compile() → str

Creates the code file.

class context_menu.linux_menus.ExistingCode

Bases: enum.Enum

Preset values important for metaprogramming.

BACKGROUND_ITEMS = '\tdef get_background_items(self, *args):\n\t\tfiles = args[-1]'
CLASS_TEMPLATE = '\nclass {}MenuProvider(GObject.GObject, Nautilus.MenuProvider):\n\tdef __init__(self):\n\t\tpass\n'
CODE_HEAD = "\nimport gi\nimport sys\nimport os\n\ntry:\n\tgi.require_version('Nautilus', '3.0')\nexcept:\n\tgi.require_version('Nautilus', '4.0')\n\nfrom gi.repository import Nautilus, GObject\n\n# -------------------------------------------- #\n\ntry:\n\tfrom urllib import unquote\nexcept ImportError:\n\tfrom urllib.parse import unquote\n "
COMMAND_HANDLER_TEMPLATE = "\n\tdef {}(self, menu, files):\n\t\tfilepath = [unquote(subFile.get_uri()[7:]) for subFile in files][0]\n\t\tos.system('{}'{})\n\n"
FILE_ITEMS = '\tdef get_file_items(self, *args):\n\t\tfiles = args[-1]'
MENU_ITEM = 'menuitem{} = Nautilus.MenuItem(name = "ExampleMenuProvider::{}", label="{}", tip = "{}", icon = "{}")'
METHOD_HANDLER_TEMPLATE = '\n\tdef {}(self, menu, files):\n\t\tfilenames = [unquote(subFile.get_uri()[7:]) for subFile in files]\n\t\t{}.{}({}, "{}")\n\n'
SUB_MENU = 'submenu{} = Nautilus.Menu()'
class context_menu.linux_menus.NautilusMenu(name: str, sub_items: list[ItemType], type: ActivationType | str)

Bases: object

append_item(menu: str, item: str) → str

Creates a necessary body_command.

build_script() → str

Finishes and returns the full code.

build_script_body(name: str, items: list[ItemType]) → None

Builds the body commands of the script.

compile() → None

Creates the code, creates a file, and moves it to the correct location.

connect(item: str, func: str) → str

Creates a necessary body_command.

create_path(path: str, dir: str) → str

Creates a path to directory. Creates all sub-directories

generate_command_func(command: str) → context_menu.linux_menus.Variable

Generates a command attached to a python function

generate_item(name: str) → context_menu.linux_menus.Variable

Generates a nautilus command variable.

generate_menu() → context_menu.linux_menus.Variable

Generates a nautilus menu variable.

generate_mod_command_func(command: str, command_vars: list[CommandVar]) → Variable

Generates a command attached to a python function that allows special variables.

generate_python_func(class_origin: str, class_func: str, params: str) → context_menu.linux_menus.Variable

Generates a command attached to a python function

get_next_item() → str

Very niche, required in other methods.

set_submenu(item: str, menu: str) → str

Creates a necessary body_command.

class context_menu.linux_menus.Variable(name: str, code: str)

Bases: object

Very simple class with no methods to help simplify the code.

context_menu.linux_menus.command_var_format(item: str) → str

Converts a python string to a value for a command

context_menu.linux_menus.remove_linux_menu(name) → None

context_menu.menus module

class context_menu.menus.ContextCommand(name: str, command: str | None = None, python: FunctionType | None = None, params: str = '', command_vars: list[CommandVar] | None = None)

Bases: object

The general command class.

A command is an executable entry in a context-menu, where menus hold other commands.

Name = Name of the command Command = command to be ran from the shell python = function to be ran params = any other parameters to be passed command_vars = to help with the command

get_method_info() → MethodInfo

Extremely important for making shell commands to run python code.

Returns a tuple (function name, function file name, path to function directory)

get_platform_command()

Will be used in future changes.

class context_menu.menus.ContextMenu(name: str, type: ActivationType | str | None = None)

Bases: object

The general menu class. This class generalizes the menus and eventually passes the correct values to the platform-specifically menus.

add_items(items: list[ItemType]) → None

Adds a list of items to the current menu.

compile() → None

Recognizes the current platform and passes information to the respective menu. Creates the actual menu.

class context_menu.menus.FastCommand(name: str, type: ActivationType | str, command: str | None = None, python: FunctionType | None = None, params: str = '', command_vars: list[CommandVar] | None = None)

Bases: object

Used for fast creation of a command. Good if you don’t want to get too involved and just jump start a program.

Extremely similar methods to other classes, only slightly modified. View the documentation of the above classes for info on these methods.

compile() → None
get_method_info() → MethodInfo
context_menu.menus.removeMenu(name: str, type: ActivationType | str) → None

Removes a menu/command entry from a context menu.

Requires the name of the menu and type of the menu

context_menu.windows_menus module

class context_menu.windows_menus.FastRegistryCommand(name: str, type: ActivationType | str, command: str, python: FunctionType, params: str, command_vars: list[CommandVar])

Bases: object

Fast command class.

Everything is identical to either the RegistryMenu class or code in the menus file

compile() → None
get_method_info() → MethodInfo
class context_menu.windows_menus.RegistryMenu(name: str, sub_items: list[ItemType], type: str)

Bases: object

Class to convert the general menu from menus.py to a Windows-specific menu.

compile(items: list[ItemType] | None = None, path: str | None = None) → None

Used to create the menu. Recursively iterates through each element in the top level menu.

create_command(name: str, path: str, command: str) → None

Creates a key with a command subkey with the ‘name’ and ‘command’, at path ‘path’.

create_menu(name: str, path: str) → str

Creates a menu with the given name and path.

Used in the compile method.

context_menu.windows_menus.command_preset_format(item: str) → str

Converts a python string to an executable location.

For example, ‘python’ -> sys.executable

context_menu.windows_menus.command_var_format(item: str) → str

Converts a python string to a value for a command

context_menu.windows_menus.context_registry_format(item: str) → str

Converts a verbose type into a registry path.

For example, ‘FILES’ -> ‘SoftwareClasses*shell’

context_menu.windows_menus.create_directory_background_command(func_name: str, func_file_name: str, func_dir_path: str, params: str) → str

Creates a registry valid command to link a context menu entry to a funtion, specifically for backgrounds(DIRECTORY_BACKGROUND, DESKTOP_BACKGROUND).

Requires the name of the function, the name of the file, and the path to the directory of the file.

context_menu.windows_menus.create_file_select_command(func_name: str, func_file_name: str, func_dir_path: str, params: str) → str

Creates a registry valid command to link a context menu entry to a funtion, specifically for file selection(FILES, DIRECTORY, DRIVE).

Requires the name of the function, the name of the file, and the path to the directory of the file.

context_menu.windows_menus.create_key(path: str, hive: int = 0) → None

Creates a key at the desired path.

context_menu.windows_menus.create_shell_command(command: str, command_vars: list[CommandVar]) → str

Creates a shell command and replaces ‘?’ with the command_vars list

context_menu.windows_menus.delete_key(path: str, hive: int = 0) → None

Deletes the desired key and all other subkeys at the given path.

context_menu.windows_menus.get_key_value(key_path: str, subkey_name: str, hive: int = 0) → Any

Gets the value of a subkey.

context_menu.windows_menus.join_keys(*keys) → str

Joins parts of a registry path.

This joins the parts with unlike os.path.join that would use / on Linux and break tests.

Parameters:keys – parts of the registry path
Returns:complete registry path
context_menu.windows_menus.list_keys(path: str, hive: int = 0) → list[str]

Returns a list of all the keys at a given registry path.

context_menu.windows_menus.remove_windows_menu(name: str, type: ActivationType | str) → None

Removes a context menu from the windows registry.

context_menu.windows_menus.set_key_value(key_path: str, subkey_name: str, value: str | int, hive: int = 0) → None

Changes the value of a subkey. Creates the subkey if it doesn’t exist.

Module contents