pyqode.core.managers

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
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 server side. 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 two arguments: the status (bool) and the results (object)
Raise:

backend.NotRunning if the backend process is not running.

start(script, interpreter='/home/colin/devenv/bin/python', args=None)

Starts the backend server process.

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

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

Parameters:
  • script – Path to the server main script.
  • interpreter – The python interpreter to use to run the server script. If None, sys.executable is used unless we are in a frozen application (frozen servers do not require an interpreter).
  • args – list of additional command line args to use to start the server process.
stop()

Stops the backend process.

connected

Checks if the client socket is connected to a backend server.

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
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'
close()
Close the file open in the editor:
  • clear editor content
  • reset file attributes to their default values
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
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

name

Gets the file base name

path

Gets the file path

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 – The name (or class) of the mode to remove.
Returns:The removed mode.
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)

Install a panel on the editor.

Parameters:
  • panel – Panel to install
  • position – Panel position
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

update(rect, delta_y, force_update_margins=False)

Updates panels