pyqode.core.managers package

Module content

The managers package contains a series of managers classes for CodeEdit.

A manager is class that takes care of a specific aspect of CodeEdit:

  • FileManager: open, save, encoding detection
  • BackendManager: manage the backend process (start the process and handle communication through sockets).
  • ModesManager: manage the list of modes of an editor
  • PanelsManager: manage the list of panels and draw them into the editor margins.
  • DecorationManager: manage text decorations

Classes

BackendManager

class pyqode.core.managers.BackendManager(editor)

Bases: pyqode.core.api.manager.Manager

The backend controller takes care of controlling the client-server architecture.

It is responsible of starting the backend process and the client socket and exposes an API to easily control the backend:

  • start
  • stop
  • send_request
static pick_free_port()

Picks a free port

send_request(worker_class_or_function, args, on_receive=None)

Requests some work to be done by the backend. You can get notified of the work results by passing a callback (on_receive).

Parameters:
  • worker_class_or_function – Worker class or function
  • args – worker args, any Json serializable objects
  • on_receive – an optional callback executed when we receive the worker’s results. The callback will be called with one arguments: the results of the worker (object)
Raise:

backend.NotRunning if the backend process is not running.

start(script, interpreter='/usr/bin/python3', args=None, error_callback=None, reuse=False)

Starts the backend process.

The backend is a python script that starts a pyqode.core.backend.JsonServer. You must write the backend script so that you can apply your own backend configuration.

The script can be run with a custom interpreter. The default is to use sys.executable.

Note

This restart the backend process if it was previously running.

Parameters:
  • script – Path to the backend script.
  • interpreter – The python interpreter to use to run the backend script. If None, sys.executable is used unless we are in a frozen application (frozen backends do not require an interpreter).
  • args – list of additional command line args to use to start the backend process.
  • reuse – True to reuse an existing backend process. WARNING: to use this, your application must have one single server script. If you’re creating an app which supports multiple programming languages you will need to merge all backend scripts into one single script, otherwise the wrong script might be picked up).
stop()

Stops the backend process.

LAST_PORT = None
LAST_PROCESS = None
SHARE_COUNT = 0
connected

Checks if the client socket is connected to the backend.

exit_code

Returns the backend process exit status or None if the process is till running.

running

Tells whether the backend process is running.

Returns:True if the process is running, otherwise False

FileManager

class pyqode.core.managers.FileManager(editor, replace_tabs_by_spaces=True)

Bases: pyqode.core.api.manager.Manager

Helps manage file operations:
  • opening and saving files
  • providing file icon
  • detecting mimetype

Example of usage:

editor = CodeEdit()
assert editor.file.path == ''
# open a file with default locale encoding or using the cached one.
editor.open(__file__)
assert editor.file.path == __file__
print(editor.file.encoding)

# reload with another encoding
editor.open(__file__, encoding='cp1252', use_cached_encoding=False)
assert editor.file.path == __file__
editor.file.encoding == 'cp1252'
clone_settings(original)
close(clear=True)
Close the file open in the editor:
  • clear editor content
  • reset file attributes to their default values
Parameters:clear – True to clear the editor content. Default is True.
static get_mimetype(path)

Guesses the mime type of a file. If mime type cannot be detected, plain text is assumed.

Parameters:path – path of the file
Returns:the corresponding mime type.
open(path, encoding=None, use_cached_encoding=True)

Open a file and set its content on the editor widget.

pyqode does not try to guess encoding. It’s up to the client code to handle encodings. You can either use a charset detector to detect encoding or rely on a settings in your application. It is also up to you to handle UnicodeDecodeError, unless you’ve added class:pyqode.core.panels.EncodingPanel on the editor.

pyqode automatically caches file encoding that you can later reuse it automatically.

Parameters:
  • path – Path of the file to open.
  • encoding – Default file encoding. Default is to use the locale encoding.
  • use_cached_encoding – True to use the cached encoding instead of encoding. Set it to True if you want to force reload with a new encoding.
Raises:

UnicodeDecodeError in case of error if no EncodingPanel were set on the editor.

reload(encoding)

Reload the file with another encoding.

Parameters:encoding – the new encoding to use to reload the file.
save(path=None, encoding=None, fallback_encoding=None)

Save the editor content to a file.

Parameters:
  • path – optional file path. Set it to None to save using the current path (save), set a new path to save as.
  • encoding – optional encoding, will use the current file encoding if None.
  • fallback_encoding – Fallback encoding to use in case of encoding error. None to use the locale preferred encoding
clean_trailing_whitespaces = None

True to clean trailing whitespaces of changed lines. Default is True

dirname

Gets the file directory name

encoding

Gets the file encoding

extension

Gets the file path

icon

Gets the file icon, provided by _get_icon

mimetype = None

File mimetype

name

Gets the file base name

opening = None

Opening flag. Set to true during the opening of a file.

path

Gets the file path

replace_tabs_by_spaces = None

True to replace tabs by spaces

restore_cursor = None

True to restore cursor position (if the document has already been

safe_save = None

If True, the file is saved to a temporary file first. If the save went fine, the temporary file is renamed to the final filename.

saving = None

Saving flag. Set to while saving the editor content to a file.

ModesManager

class pyqode.core.managers.ModesManager(editor)

Bases: pyqode.core.api.manager.Manager

Manages the list of modes of the code edit widget.

append(mode)

Adds a mode to the editor.

Parameters:mode – The mode instance to append.
clear()

Removes all modes from the editor. All modes are removed from list and deleted.

get(name_or_klass)

Gets a mode by name (or class)

Parameters:name_or_klass (str or type) – The name or the class of the mode to get
Return type:pyqode.core.api.Mode
remove(name_or_klass)

Removes a mode from the editor.

Parameters:name_or_klass – The name (or class) of the mode to remove.
Returns:The removed mode.

PanelsManager

class pyqode.core.managers.PanelsManager(editor)

Bases: pyqode.core.api.manager.Manager

Manages the list of panels and draws them inised the margin of the code edit widget.

append(panel, position=1)

Installs a panel on the editor.

Parameters:
  • panel – Panel to install
  • position – Position where the panel must be installed.
Returns:

The installed panel

clear()

Removes all panel from the editor.

get(name_or_klass)

Gets a specific panel instance.

Parameters:name_or_klass – Name or class of the panel to retrieve.
Returns:The specified panel instance.
margin_size(position=1)

Gets the size of a specific margin.

Parameters:position – Margin position. See pyqode.core.api.Panel.Position
Returns:The size of the specified margin
Return type:float
panels_for_zone(zone)

Gets the list of panels attached to the specified zone.

Parameters:zone – Panel position.
Returns:List of panels instances.
refresh()

Refreshes the editor panels (resize and update margins)

remove(name_or_klass)

Removes the specified panel.

Parameters:name_or_klass – Name or class of the panel to remove.
Returns:The removed panel
resize()

Resizes panels

TextDecorationsManager

class pyqode.core.managers.TextDecorationsManager(editor)

Bases: pyqode.core.api.manager.Manager

Manages the collection of TextDecoration that have been set on the editor widget.

append(decoration)

Adds a text decoration on a CodeEdit instance

Parameters:decoration (pyqode.core.api.TextDecoration) – Text decoration to add
clear()

Removes all text decoration from the editor.

remove(decoration)

Removes a text decoration from the editor.

Parameters:decoration (pyqode.core.api.TextDecoration) – Text decoration to remove

Table Of Contents

Previous topic

pyqode.core.dialogs package

Next topic

pyqode.core.modes package