salome.kernel – Package containing the KERNEL python utilities

deprecation – Indication of deprecated modules and functions

This module provides several functions to indicate the deprecation of a module, a method or a function.

salome.kernel.deprecation.deprecated(msg='See documentation for possible replacements.')

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used. The message in parameter will be displayed and should indicate how this function can be replaced. If the terminal can display colors, the warning messages will appear in blue.

salome.kernel.deprecation.deprecated_module(msg='See documentation for possible replacements.')

This function can be used to mark a module as deprecated. It must be called explicitly at the beginning of the deprecated module. It will result in a warning being emitted. The message in parameter will be displayed and should indicate how this module can be replaced. If the terminal can display colors, the warning messages will appear in blue.

salome.kernel.deprecation.is_called_by_sphinx()

Determine if the calling code is ultimately called by sphinx to generate documentation. The result can be used to conditionally inhibit the decorators or some Salome-related imports that fail when called outside Salome.

termcolor – Display colored text in terminal

This module provides utility functions to display colored text in the terminal. It is based on ISO 6429 standard that defines control codes to change characters representation in color-capable ASCII terminals.

In this module, colors are represented as lists of codes, so they can be added to obtain special effects (e.g. RED + GREEN_BG to display red text on green background). Several constants are defined for the most usual codes to facilitate the use of colors, but it is also possible to define colors directly from the corresponding code from ISO 6429 standard. In fact it is even necessary for less usual codes that don’t have an associated constant (e.g. PURPLE + [‘09’] can be used to display a crossed-out purple text).

Example:

import sys
from pal import termcolor
if termcolor.canDisplayColor(sys.stdout):
    print termcolor.makeColoredMessage("Hello world!", termcolor.BLUE)
else:
    print "Hello world!"
salome.kernel.termcolor.BLACK

Black text (equivalent to BLACK_FG)

salome.kernel.termcolor.BLACK_BG

Black background

salome.kernel.termcolor.BLACK_FG

Black foreground

salome.kernel.termcolor.BLUE

Blue text (equivalent to BOLD + BLUE_FG)

salome.kernel.termcolor.BLUE_BG

Blue background

salome.kernel.termcolor.BLUE_FG

Blue foreground

salome.kernel.termcolor.BOLD

Bold text and brighter colors

salome.kernel.termcolor.CYAN

Cyan text (equivalent to BOLD + CYAN_FG)

salome.kernel.termcolor.CYAN_BG

Cyan background

salome.kernel.termcolor.CYAN_FG

Cyan foreground

salome.kernel.termcolor.DEFAULT

Default color for the terminal

salome.kernel.termcolor.GREEN

Green text (equivalent to BOLD + GREEN_FG)

salome.kernel.termcolor.GREEN_BG

Green background

salome.kernel.termcolor.GREEN_FG

Green foreground

salome.kernel.termcolor.PURPLE

Purple text (equivalent to BOLD + PURPLE_FG)

salome.kernel.termcolor.PURPLE_BG

Purple background

salome.kernel.termcolor.PURPLE_FG

Purple foreground

salome.kernel.termcolor.RED

Red text (equivalent to BOLD + RED_FG)

salome.kernel.termcolor.RED_BG

Red background

salome.kernel.termcolor.RED_FG

Red foreground

salome.kernel.termcolor.UNDERLINED

Underlined text

salome.kernel.termcolor.WHITE

White text (equivalent to WHITE_FG)

salome.kernel.termcolor.WHITE_BG

White background

salome.kernel.termcolor.WHITE_FG

White foreground

salome.kernel.termcolor.YELLOW

Yellow text (equivalent to BOLD + YELLOW_FG)

salome.kernel.termcolor.YELLOW_BG

Yellow background

salome.kernel.termcolor.YELLOW_FG

Yellow foreground

salome.kernel.termcolor.canDisplayColor(stream)

Return True if the stream can display colored text, False otherwise.

salome.kernel.termcolor.getControlSequence(color)

Return the control sequence for the color in parameter, i.e. the string telling the terminal to display the following text in the given color.

salome.kernel.termcolor.makeColoredMessage(message, color)

Return a string that can be used to display the message in parameter with the given color.

logger – Logging utility

This module defines a class which provides logging facility in Salome:

class salome.kernel.logger.Logger(keyword='KEY', level=30, color=None, sourceFileName=None)

Bases: logging.Logger

This class formats and displays log messages in Salome environment. It inherits Logger class defined in logging module from Python library, so all methods from logging.Logger can be used here. The format of the traces is:

LEVEL [keyword] : Message

where LEVEL is the level of the message (DEBUG, INFO, etc.), keyword is the name of the logger, and Message is the message to log.

When creating a new Logger object, the parameter keyword defines the name of the logger, level defines the logging level (default is logging.DEBUG if KERNEL module is configured with –enable-debug option or logging.WARNING otherwise), and color defines the color of the log messages for this logger (log messages will appear in color only when displayed on color-capable ASCII terminals). See module salome.kernel.termcolor for the color constants.

By default, log messages will be displayed only on standard output. They can also be recorded in a file (see method setLogFile()). For now, the CORBA-based logging facility can not be used through this class.

A source filename sourceFileName can be defined. If this argument is specified, then the keyword is modified to the basename of the sourceFileName

Basic usage:

from salome.kernel.logger import Logger
log = Logger("Test")
log.debug("Debug message")
log.info("Information message")
log.warning("Warning message")
log.error("Error message")
log.critical("Fatal error message")
closeLogFile()

Close the log file.

fatal(message)

Log a message with CRITICAL level. This method only exists for backward compatibility and is equivalent to critical(message).

hideDebug()

Hide DEBUG level messages (equivalent to setLevel(logging.INFO)).

setColor(color)

Set the color of log messages on color-capable terminals. If color is None, the default color will be used.

setLogFile(logFilename)

Define a log file to record the log messages (in addition to the standard output).

showDebug()

Log all messages, including DEBUG level messages (equivalent to setLevel(logging.DEBUG)).

class salome.kernel.logger.ExtLogger(keyword='KEY', level=30, color=None, sourceFileName=None)

Bases: salome.kernel.logger.Logger

This class extends Logger class and adds exception information when DEBUG messages are recorded. It exists mainly for backward compatibility, as the same thing can be done by calling Logger.debug(message, exc_info = True).

debug(message)

Log a DEBUG message with exception information (equivalent to Logger.debug(message, exc_info = True)).

studyedit – Study editor

This module provides a new class StudyEditor to complement Study and StudyBuilder classes.

class salome.kernel.studyedit.StudyEditor(studyId=None)

This class provides utility methods to complement Study and StudyBuilder classes. Those methods may be moved in those classes in the future. The parameter studyId defines the ID of the study to edit. If it is None, the edited study will be the current study. The preferred way to get a StudyEditor object is through the method getStudyEditor() which allows to reuse existing instances.

studyId

This instance attribute contains the ID of the edited study. This attribute should not be modified.

study

This instance attribute contains the underlying Study object. It can be used to access the study but the attribute itself should not be modified.

builder

This instance attribute contains the underlying StudyBuilder object. It can be used to edit the study but the attribute itself should not be modified.

createItem(fatherItem, name, fileType=None, fileName=None, comment=None, icon=None, IOR=None, typeId=None)

Create a new object named name under fatherItem in the study, with the given attributes. If an object named name already exists under the father object, the new object is created with a new name name_X where X is the first available index.

Parameters:fatherItem (SObject) – item under which the new item will be added.
Returns:new SObject created in the study

See setItem() for the description of the other parameters.

findItem(fatherItem, name=None, fileType=None, fileName=None, comment=None, icon=None, IOR=None, typeId=None)

Find an item with given attributes under fatherItem in the study. If none is found, return None. If several items correspond to the parameters, only the first one is returned. The search is made only on given parameters (i.e. not None). To look explicitly for an empty attribute, use an empty string in the corresponding parameter.

See setItem() for the description of the parameters.

findOrCreateComponent(moduleName, componentName=None, icon=None, containerName='FactoryServer')

Find a component corresponding to the Salome module moduleName in the study. If none is found, create a new component and associate it with the corresponding engine (i.e. the engine named moduleName). Note that in Salome 5, the module name and engine name must be identical (every module must provide an engine with the same name). In Salome 6 it will be possible to define a different name for the engine.

Parameters:
  • moduleName (string) – name of the module corresponding to the component (the module name is the string value in the attribute “AttributeComment” of the component)
  • componentName (string) – name of the new component if created. If None, use moduleName instead.
  • icon (string) – icon for the new component (attribute “AttributePixMap”).
  • containerName (string) – name of the container in which the engine should be loaded.
Returns:

the SComponent found or created.

findOrCreateItem(fatherItem, name, fileType=None, fileName=None, comment=None, icon=None, IOR=None, typeId=None)

Find an object under fatherItem in the study with the given attributes. Return the first one found if at least one exists, otherwise create a new one with the given attributes and return it.

See setItem() for the description of the parameters.

getAttributeValue(sObject, attributeName, default=None)

Return the value of the attribute named attributeName on the object sObject, or default if the attribute doesn’t exist.

getFileName(sObject)

Return the value of the attribute “AttributeExternalFileDef” of the object sObject, or an empty string if it is not set.

getFileType(sObject)

Return the value of the attribute “AttributeFileType” of the object sObject, or an empty string if it is not set.

getIcon(sObject)

Return the value of the attribute “AttributePixMap” of the object sObject, or an empty string if it is not set.

getOrLoadObject(item)

Get the CORBA object associated with the SObject item, eventually by first loading it with the corresponding engine.

getTypeId(sObject)

Return the value of the attribute “AttributeLocalID” of the object sObject, or None if it is not set.

loadComponentEngine(sComponent, containerName='FactoryServer')

Load the engine corresponding to sComponent in the container containerName, associate the engine with the component and load the CORBA objects of this component in the study.

removeItem(item, withChildren=False)

Remove the given item from the study. Note that the items are never really deleted. They just don’t appear in the study anymore.

Parameters:
  • item (SObject) – the item to be removed
  • withChildren (boolean) – if True, also remove the children of item
Returns:

True if the item was removed successfully, or False if an error happened.

setAttributeValue(sObject, attributeName, attributeValue)

Set the value of the attribute named attributeName on the object sObject to the value attributeValue.

setFileName(sObject, value)

Set the attribute “AttributeExternalFileDef” of the object sObject to the value value.

setFileType(sObject, value)

Set the attribute “AttributeFileType” of the object sObject to the value value.

setIcon(sObject, value)

Set the attribute “AttributePixMap” of the object sObject to the value value.

setItem(item, name=None, fileType=None, fileName=None, comment=None, icon=None, IOR=None, typeId=None)

Modify the attributes of an item in the study. Unspecified attributes (i.e. those set to None) are left unchanged.

Parameters:
  • item (SObject) – item to modify.
  • name (string) – item name (attribute ‘AttributeName’).
  • fileType (string) – item file type (attribute ‘AttributeFileType’).
  • fileName (string) – item file name (attribute ‘AttributeExternalFileDef’).
  • comment (string) – item comment (attribute ‘AttributeComment’). Note that this attribute will appear in the ‘Value’ column in the object browser.
  • icon (string) – item icon name (attribute ‘AttributePixMap’).
  • IOR (string) – IOR of a CORBA object associated with the item (attribute ‘AttributeIOR’).
  • typeId (integer) – item type (attribute ‘AttributeLocalID’).
setItemAtTag(fatherItem, tag, name=None, fileType=None, fileName=None, comment=None, icon=None, IOR=None, typeId=None)

Find an item tagged tag under fatherItem in the study tree or create it if there is none, then set its attributes.

Parameters:
  • fatherItem (SObject) – item under which the tagged item will be looked for and eventually created.
  • tag (integer) – tag of the item to look for.
Returns:

the SObject at tag if found or created successfully, or None if an error happened.

See setItem() for the description of the other parameters.

setTypeId(sObject, value)

Set the attribute “AttributeLocalID” of the object sObject to the value value.

salome.kernel.studyedit.getActiveStudyId()

Return the ID of the active study. In GUI mode, this function is equivalent to salome.sg.getActiveStudyId(). Outside GUI, it returns salome.myStudyId variable.

salome.kernel.studyedit.getStudyEditor(studyId=None)

Return a StudyEditor instance to edit the study with ID studyId. If studyId is None, return an editor for the current study.