Soma-base documentation

Many miscelaneous all-purpose classes and functions...

SubModule: bufferandfile

BufferAndFile instances are used to read data from a file (for instance for identification) and “put back” the data on the file.

class soma.bufferandfile.BufferAndFile(fileObject)[source]

This class is a read only file-like object that allows to read ahead and push data back into the stream. All pushed back data are stored in a buffer that is “read” by all subsequent read acces until it is empty. When the buffer is empty, reading is done directly on the attached file object.

Example:

from soma.bufferandfile import BufferAndFile

# Open a file with a buffer
f = BufferAndFile.open( fileName )
# Check that the file content is XML
start = f.read( 5 )
# Put back the read characters
f.unread( start )
if start == '<?xml':
  # Use the file in an XML parser
  ...
elif start == '&HDF':
  # Use the file in an HDF5 parser
  ...

Create a file-like object that adds an unread() method to an opened fileObject.

changeFile(fileObject)[source]

Change the internal file object (keeps the internal buffer untouched).

clone()[source]

Return a new L{BufferAndFile} instance with the same internale buffer and the same internal file object as C{self}.

next()[source]

Iteration protocol

static open(*args, **kwargs)[source]

Open a file with built-in open() and create a BufferAndFile instance.

read(size=None)[source]

Read the file

readline(size=None)[source]

Read one text line

seek(offset, whence=0)[source]

If whence is 0 or 2 (absolute seek positioning) or if offset is negative, internal buffer is cleared and seek is done directly on the internal file object. Otherwise (relative seek with a positive offset), internal buffer is taken into account.

tell()[source]

POsition in file

unread(stringValue)[source]

Adds data at the begining of the internal buffer. Data in the internal buffer will be returned by all subsequent read acces until the buffer is empty.

SubModule: databases

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: debug

Utility classes and functions for debugging.

soma.debug.functionCallInfo(frame=None)[source]

Return a dictionary that gives information about a frame corresponding to a function call. The directory contains the following items:

  • ‘function’: name of the function called
  • ‘filename’: name of the python file containing the function
  • ‘lineno’: line number executed in ‘filename’
  • ‘arguments’: arguments passed to the function. It is a list containing pairs of (argument name, argument value).
soma.debug.print_stack(file=<open file '<stdout>', mode 'w' at 0x2ad7782ae198>, frame=None)[source]

Print information about the stack, including argument passed to functions called.

soma.debug.stackCallsInfo(frame=None)[source]

Return a list containing functionCallInfo(frame) for all frame in the stack.

SubModule: decorators

This module contains decorators.

soma.decorators.synchronized(function)[source]

SubModule: enum

Robust enumerated type support in Python

This package provides a module for robust enumerations in Python.

An enumeration object is created with a sequence of string arguments to the Enum() constructor:

>>> from enum import Enum
>>> Colours = Enum('red', 'blue', 'green')
>>> Weekdays = Enum('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun')

The return value is an immutable sequence object with a value for each of the string arguments. Each value is also available as an attribute named from the corresponding string argument:

>>> pizza_night = Weekdays[4]
>>> shirt_colour = Colours.green

The values are constants that can be compared only with values from the same enumeration; comparison with other values will invoke Python’s fallback comparisons:

>>> pizza_night == Weekdays.fri
True
>>> shirt_colour > Colours.red
True
>>> shirt_colour == "green"
False

Each value from an enumeration exports its sequence index as an integer, and can be coerced to a simple string matching the original arguments used to create the enumeration:

>>> str(pizza_night)
'fri'
>>> shirt_colour.index
2
class soma.enum.Enum(*keys, **kwargs)[source]

Enumerated type

Create an enumeration instance

exception soma.enum.EnumBadKeyError(key)[source]

Raised when creating an Enum with non-string keys

exception soma.enum.EnumEmptyError[source]

Raised when attempting to create an empty enumeration

exception soma.enum.EnumException[source]

Base class for all exceptions in this module

exception soma.enum.EnumImmutableError(*args)[source]

Raised when attempting to modify an Enum

exception soma.enum.EnumMissingKeyError(enumtype, key)[source]

Raised when creating an Enum with non-string keys

class soma.enum.EnumValue(enumtype, index, key)[source]

A specific value of an enumerated type

Set up a new instance

class soma.enum.EnumValues(enumtype, values=0)[source]

Values of an enumerated type

Set up a new instance

SubModule: env

class soma.env.BrainvisaSystemEnv(*args, **kwargs)[source]

This class gets the value of the variables BRAINVISA_UNENV_... if they are defined. These variables store the value of system environment variables that have been modified in brainvisa context. The method getVariables returns a map of variable -> value to restore the system value of these environment variables.

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

getVariables()[source]

Returns the dictionary of BRAINVISA_UNENV_* variables

SubModule: functiontools

Utility classes and functions for Python callable.

soma.functiontools.checkParameterCount(callable, paramCount)[source]

Check that a callable can be called with paramCount arguments. If not, a RuntimeError is raised.

  • callable: function, method, class or instance

    callable to inspect

  • paramCount: integer

    number of parameters

see: getArgumentsSpecification()

soma.functiontools.getArgumentsSpecification(callable)[source]

This is an extension of Python module inspect.getargspec that accepts classes and returns only information about the parameters that can be used in a call to callable (e.g. the first self parameter of bound methods is ignored). If callable has not an appropriate type, a TypeError exception is raised.

  • callable: function, method, class or instance

    callable to inspect

  • returns: tuple of four elements

    As inspect.getargspec(), returns (args, varargs, varkw, defaults) where args is a list of the argument names (it may contain nested lists). varargs and varkw are the names of the * and ** arguments or None. defaults is an n-tuple of the default values of the last n arguments.

soma.functiontools.getCallableString(callable)[source]

Returns a translated human readable string representing a callable.

  • callable: function, method, class or instance

    callable to inspect

  • returns: string

    type and name of the callable

soma.functiontools.hasParameter(callable, parameterName)[source]

Return True if callable can be called with a parameter named parameterName. Otherwise, returns False.

  • callable: function, method, class or instance

    callable to inspect

  • parameterName: string

    name of the parameter

  • returns: bool

see: getArgumentsSpecification()

soma.functiontools.numberOfParameterRange(callable)[source]

Return the minimum and maximum number of parameter that can be used to call a function. If the maximum number of argument is not defined, it is set to None.

  • callable: function, method, class or instance

    callable to inspect

  • returns: tuple of two elements

    (minimum, maximum)

see: getArgumentsSpecification()

SubModule: gui

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: html

Utility functions for HTML format.

soma.html.htmlEscape(msg)[source]

Replace special characters in the message by their correponding html entity.

  • returns: unicode
soma.html.lesserHtmlEscape(msg)[source]

Replace special characters in the message by their correponding html entity.

  • returns: unicode

SubModule: httpupload

This module contains classes used to upload files using http protocol. Http upload is processed as follow :

  • xml fragment header is uploaded. It is a file field that contains xml document formatted as follow :
<fragment>
  <filename>filename</filename>
  <filelength>4096</filelength>
  <offset>2048</offset>
  <length>1024</length>
  <sha1>f3862f40fc63c739618dace578da3607c6ac1847</sha1>
</fragment>
  • if the fragment containing data already exists on the server, it is not necessary to upload it again, otherwise the data for the fragment must be uploaded. To check that data exists on the server and that data integrity is correct, the sha1 key is used.
  • after each fragment upload (header or data), a check is made to ensure that files are not complete.
  • if file is complete (i.e. : all the file fragments have been receive), it is added to a queue for being rebuilt.
class soma.httpupload.FileBuilder[source]

Builder threading.Thread for files that were uploaded by fragments.

run()[source]

run

setInterval(interval)[source]

Set the number of seconds we sleep between executing our task

shutdown()[source]

Stop this thread

class soma.httpupload.FileBuilderInfo(uploadid, basedirectory, filename, filelength)[source]

Class to get info about uploaded file.

Initialize FileBuilderInfo using its file name and file length.

  • uploadid : string

    unique identifier for the upload.

  • basedirectory : string

    relative base directory.

  • filename : string

    file name.

  • filelength : long

    file length.

addFileFragment(filefragment)[source]

Add FileFragment to the current FileBuilderInfo.

addToQueue(queue)[source]

Add FileBuilderInfo to the queue.Queue if not already added.

  • queue : queue.Queue
queue.Queue to add FileBuilderInfo to.
buildResultFile()[source]

Build a file from a FileBuilderInfo. This method can not be executed by multiple threading.Thread simultaneously.

  • filebuilderinfo : FileBuilderInfo

    FileBuilderInfo that contains all information about the file to rebuild (fragments, length, name).

  • filepath : string

    path of the file to rebuild.

checkFileBuild()[source]

Check if the current FileBuilderInfo is complete and contains all needed information to be rebuilt. If it is the case, the current FileBuilderInfo is transfered to the queue of FileBuilderInfo to be rebuilt.

checkFileFragment(filefragment)[source]

Check that FileFragment matches the file to which it belongs.

checkFileIntegrity()[source]

Check that all required :py:class:`FileFragment`s exist and have the correct length.

  • returns: True if all required :py:class:`FileFragment`s exist and have the correct length, False otherwise.
checkResultFile()[source]

Check that result file exists and has a rigth length.

  • returns: True if the result file exists with the rigth length, False otherwise.
getFileFragment(filefragmentsha1)[source]

Get FileFragment using its sha1 key for the current FileBuilderInfo.

getFileFragments()[source]

Get FileFragment for the current FileBuilderInfo.

  • returns: dict containing the :py:class:`FileFragment`s.
getFileHash()[source]

Get the considered file hash for the current FileBuilderInfo.

  • returns: string containing the considered file hash for the current FileBuilderInfo.
getFileLength()[source]

Get the considered file length for the current FileBuilderInfo.

  • returns: long containing the considered file length for the current FileBuilderInfo.
getFileName()[source]

Get the considered file name for the current FileBuilderInfo.

  • returns: string containing the considered file name for the current FileBuilderInfo.
static getHash(uploadid, basedirectory, filename, filelength)[source]

Get the hash for a file name and length.

  • filename : string

    file name to use for creating the hash.

  • filename : long

    file length to use for creating the hash.

  • returns: the hash for a file name and length.

getResultDirectory()[source]

Get result directory for the current FileBuilderInfo. i.e. the name of the directory where the file will be rebuild.

  • returns: string containing the result directory for the current FileBuilderInfo.
getResultFileName()[source]

Get result file name for the current FileBuilderInfo. i.e. the name of the file rebuilt.

  • returns: string containing the result file name for the current FileBuilderInfo.
getUploadId()[source]

Get the upload id for the current FileBuilderInfo.

  • returns: string containing the upload id for the current FileBuilderInfo.
setStatus(value)[source]

Set the FileBuilderInfo status.

class soma.httpupload.FileBuilderInfoManager(*args, **kwargs)[source]

Class to manage FileBuilderInfo.

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

getFileBuilderInfo(uploadid, basedirectory, filename, filelength, new=True, default=None)[source]

Get a FileBuilderInfo using its file name and file length. If the FileBuilderInfo does not exist yet, it is added.

  • uploadid : string

    file name.

  • basedirectory : string

    base directory.

  • filename : string

    file name.

  • filelength : long

    file length.

  • new : boolean

    specify to add a new FileBuilderInfo if None exists for the file name and length.

  • default : object

    default value if None exists for the file name and length. This value is used only if the ‘new’ parameter is set to False.

  • returns: the matching FileBuilderInfo.

getFileBuilderInfosFromKey(filefragmentsha1)[source]

Get a FileBuilderInfo list using the sha1 key of FileFragment. It retrieves all FileBuilderInfo that contains a FileFragment with the matching sha1 key.

  • filefragmentsha1 : string
sha1 key for the FileFragment.
getQueue()[source]

Get the Queue.Queue that is used to put FileBuilderInfo once they are complete.

transferToQueue(filebuilderinfo)[source]

Transfer a FileBuilderInfo to the Queue.Queue.

  • filename : FileBuilderInfo
soma.httpupload.FileBuilderInfoStatus

FileBuilderInfoStatus

class soma.httpupload.FileFragment(headerfile)[source]

Class to get file fragment information. A FileFragment correponds to a part of a file.

Initialize the FileFragment.

  • headerfile : cgi.FieldStorage

    cgi.FieldStorage from the parsed http request.

deleteLocalData()[source]

Delete data for the FileFragment local file.

  • filepath : string

    string containing the file path to delete.

getBaseDirectory()[source]

Get the base directory for the current FileFragment.

  • returns: string containing the base directory for the current FileFragment.
getFileLength()[source]

Get the file length for the current FileFragment.

  • returns: string containing the file length for the current FileFragment.
getFileName()[source]

Get the file name for the current FileFragment.

  • returns: string containing the file name for the current FileFragment.
getLength()[source]

Get the length of the current FileFragment (i.e. the length of the FileFragment data).

  • returns: long containing the length of the current

FileFragment.

getLocalDataPath()[source]

Get a local data path for FileFragment.

  • returns: string containing the local data path.
getOffset()[source]

Get the offset of the current FileFragment (i.e. the index of the first FileFragment data character in the result file).

  • returns: long containing the fragment offset of the current FileFragment.
getSha1()[source]

Get the sha1 key of the current FileFragment (the sha1 key is used to process check sum on the FileFragment data).

  • returns: string containing the sha1 key of the current

FileFragment.

getUploadId()[source]

Get the upload id for the current FileFragment.

  • returns: string containing the upload id for the current FileFragment.
hasValidLocalData()[source]

Check that the current FileFragment has valid local data. Checks the file existence and sha1 key.

  • returns: True if the current FileFragment has valid local data, False otherwise.
hasValidSizeLocalData()[source]

Check that the current FileFragment has valid local data. Checks the file existence and the size of the file.

  • returns: True if the current FileFragment has valid local data using data size, False otherwise.
parseFromXml(document)[source]

Parses FileFragment from an xml document.

  • document : xml.dom.minidom.Document

    xml.dom.minidom.Document containing the parsed xml header for the current FileFragment.

readLocalData()[source]

Read data for the FileFragment from a local file.

writeLocalData(datafile)[source]

Write data to a local file from cgi.FieldStorage. The cgi.FieldStorage comes from a parsed http request.

  • datafile : cgi.FieldStorage

    cgi.FieldStorage containing the data for the FileFragment.

  • returns: True if the data were written to the local file without any issue, False otherwise.

soma.httpupload.LogLevel

LogLevel

class soma.httpupload.LogManager(*args, **kwargs)[source]

LogManager

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

logFile()[source]

Get log file from configuration file.

logLevel()[source]

Get log level from configuration file.

logReset()[source]

Get log reset from configuration file.

write(value, filepath, mode)[source]

write

writeLogInfo(value, filepath=None, mode='a+b', level=EnumValue(<soma.enum.Enum object at 0x2250ed0>, 3, 'INFO'))[source]

writeLogInfo

class soma.httpupload.ResourceManager(*args, **kwargs)[source]

Register the resources directories and manages concurrent resource accesses using locks.

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

getDirectory(key)[source]

Get the managed directories

  • returns: managed directory list.
getFileBuildLength(uploadid, basedirectory, filename, filelength)[source]

Get the status for a file information.

  • uploadid : string

    upload id.

  • basedirectory : string

    relative directory path.

  • filename : string

    file name.

  • filelength : long

    file length.

getResultDirectory(uploadid, basedirectory)[source]

Get result directory path of the file to rebuild.

  • uploadid : string

    upload id.

  • basedirectory : string

    relative base directory path.

  • returns: string containing the result directory path.

static getResultFileName(filename, filelength)[source]

Get result file name for the file name and length. i.e. the name of the file to rebuild.

  • filename : string

    file name.

  • filelength : long

    file length.

  • returns: string containing the result file name.

getResultUploadDirectory(uploadid)[source]

Get upload directory path for an upload id.

  • uploadid : string

    upload id.

  • returns: string containing the upload directory path.

getUploadBuildCount(uploadid)[source]

Get the count of built files for an upload.

  • uploadid : string

    upload id.

getUploadBuildLength(uploadid)[source]

Get the length of built files for an upload.

  • uploadid : string

    upload id.

processFileStorage(filestorage, isheader)[source]

Process a cgi.FileStorage field of an http request to get file fragment information.

  • filestorage: cgi.FileStorage

    cgi.FileStorage field of an http request to get file fragment information. It can contain either header information or data.

  • isheader: bool

    specify if the filestorage contains header information or data.

soma.httpupload.checkSha1(content, sha1)[source]

Check that a string content matches a particular SHA-1 key.

  • content: string

    content to check.

  • sha1: string

    sha1 key to check.

  • returns: True if the content matches the sha1 key, False otherwise.

soma.httpupload.displayFileBuilderInfos()[source]

Display current registered :py:class:`FileBuilderInfo`s.

soma.httpupload.filebuilderinfomanager

filebuilderinfomanager

soma.httpupload.getDirectoryFilesCount(directory)[source]

getDirectoryFilesCount

soma.httpupload.getDirectoryFilesSize(directory)[source]

getDirectoryFilesSize

soma.httpupload.getFileBuildCountResponseDocument(buildcount)[source]

Create xml file build count response document.

  • filelength: long

    specify the file upload build count.

  • returns: xml document containing the response.

soma.httpupload.getFileBuildLengthResponseDocument(filelength)[source]

Create xml file build length response document.

  • filelength: long

    specify the file upload build length.

  • returns: xml document containing the response.

soma.httpupload.getTextValue(node)[source]

Recursively get xml node text content as string value.

  • node: Node

    xml node to go trough.

  • returns: string containing the result of the node as text value.

soma.httpupload.getUploadResponseDocument(upload)[source]

Create xml upload response document.

  • upload: bool

    specify if the data upload must be done or not.

  • returns: xml document containing the response.

soma.httpupload.processHttpUploadQuery(*args, **kwargs)[source]

Main httpupload function entry. It processes arguments and saves uploaded file on the server using it.

  • returns: string containing the response.
soma.httpupload.showNode(node, showattributes=False)[source]

Get xml node content as string value.

  • node: Node

    xml node to go trough.

  • showattributes: bool

    specify if attributes must be shown.

  • returns: string containing the result of the node display.

soma.httpupload.walkTree(node)[source]

Walk trough an xml tree node.

  • node: Node

    xml tree node to go trough.

  • returns: Generator to walk the xml tree node.

SubModule: importer

Utility classes and functions for Python import and sip namespace renaming.

@author: Nicolas Souedet @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.importer.ExtendedImporter(*args, **kwargs)[source]

C{ExtendedImporter} is used to import external modules in a module managing rules that allow ro rename and delete or do anything else on the imported package. As imported packages could modify each others, all the registered rules are applied after each import using this C{ExtendedImporter}.

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

applyRules()[source]

This method apply rules for each extended module.

importInModule(moduleName, globals, locals, importedModuleName, namespacesList=[], handlersList=None, *args, **kwargs)[source]

This method is used to import a module applying rules (rename rule, delete rule, ...) .

@moduleName string: name of the module to import into. @globals dict: globals dictionary of the module to import into. @locals dict: locals dictionary of the module to import into. @importedModuleName string: name of the imported module. @namespacesList list: a list of rules concerned namespaces for the imported module. @handlersList list: a list of handlers to apply during the import of the module.

class soma.importer.ExtendedImporterHelper[source]

Static methods declared to help extended import process.

static getMatchingObject(object, listName, level=0)[source]

This static method is used to get an object contained in another object.

@module object: object to get objects from. @listName list: complete name including name hierarchy split in list.

@return: Object found in the hierarchy or None if no object was found.

static getModuleObject(module, name)[source]

This static method is used to get an object contained in the namespace of a module.

@name string: complete name including namespace of the object to get. @module module: module object to get objects from.

@return: Object found in the module or None if no object was found.

class soma.importer.ExtendedModule(moduleName, globals, locals)[source]

Register a series of rules to apply during the import process of the extended module. An extended module is able to refer to other modules and to apply rules to these other modules. The extended module manages the globals and locals variables declared for the module. Each rule contains the module to which it refers, and the handlers to call for the rule to apply. The calling order is the registering order.

addHandlerRules(module, handlersList=[], *args, **kwargs)[source]

This method is used to add handler rules (renaming rule, deleting rule, ...) .

@module module: module object to apply rules to. @handlersList list: a list of handlers to the module.

addPartialRules(module, partialsList=[])[source]

This method is used to add handler rules (renaming rule, deleting rule, ...) .

@module module: module object to apply rules to. @partialsList list: a list of C{partial} object that will be called during the import of the module.

applyRules()[source]

Apply the C{partial} handler rules (renaming rule, deleting rule, ...) for the C{ExtendedModule}.

class soma.importer.GenericHandlers[source]

Static generic handlers used as import rules.

static moveChildren(namespace, extendedModule, referedModule)[source]

This static method is used to move child objects of a refered module to the extended module.

@namespace string: namespace of the referedModule to get objects to move from. @extendedModule ExtendedModule: C{ExtendeModule} object into which move objects. @referedModule module: refered module object to get objects to move from.

static removeChildren(namespace, prefixes, extendedModule, referedModule)[source]

This static method is used to remove children from the extended module.

@namespace string: namespace of the referedModule. @prefixes list: list of prefixes of objects to remove. @extendedModule ExtendedModule: C{ExtendeModule} object to remove objects from. @referedModule module: refered module object.

SubModule: minf

This module contains all the framework necessary to customize, read and write minf files. A minf file is composed of structured data saved in XML or Python format. The minf framework provides tools to read and write minf files but also to customize the way Python objects are read an written.

There are several submodules in this package but main functions and classes can be imported from C{soma.minf.api}:

  • for reading minf files: L{iterateMinf}, L{readMinf}
  • for writing minf files: L{createMinfWriter}, L{writeMinf}
  • for customizing minf files: L{createReducerAndExpander}, L{registerClass}, L{registerClassAs}

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

soma.minf.api.createMinfWriter(destFile, format='XML', reducer='minf_2.0')[source]

Create a writer for storing objects in destFile. Example:

from soma.minf.api import createMinfWriter
writer = createMinfWriter( '/tmp/test.minf' )
writer.write( 'A string' )
writer.write( { 'A dict key': [ 'A list', 'with two elements' ] } )
writer.close()

@param format: name of the format to write. @type format: string @param reducer: name of the reducer to use (see L{soma.minf.tree} for

more information about reducers).

@type reducer: string

soma.minf.api.iterateMinf(source, targets=None)[source]

Returns an iterator over all objects stored in a minf file.

Example::

from soma.minf.api import iterateMinf

for item in iterateMinf( ‘test.minf’ ):
print repr( item )
@param source: Input file name or file object. If it is a file name, it is
opened with C{open( source )}.
soma.minf.api.minfFormat(source)[source]

Return a pair C{( format, reduction )} identifying the minf format. If C{source} is not a minf file, C{( None, None )} is returned. Otherwise, C{format} is a string representing the format of the minf file: C{‘XML’} or C{‘python’}. C{reduction} is the name of the reducer used to write the minf file or C{None} if format is C{‘python’}.

Example::
from soma.minf.api import minfFormat format, reduction = minfFormat( ‘/home/me/test.minf’ )

If C{source} is a L{BufferAndFile} instance, this call behave as if nothing has been read from the file. This can be useful if you have an opened file that cannot be seeked backward:

Example::

from soma.bufferandfile import BufferAndFile from soma.minf.api import minfFormat, readMinf

bf = BufferAndFile( stream_file_object ) format, reduction = minfFormat( bf ) if format is not None:

minfContent = readMinf( bf )
@param source: Input file name or file object. If it is a file name, it is
opened with C{open( source )}.
soma.minf.api.readMinf(source, targets=None)[source]

Entirerly reads a minf file and returns its content in a tuple. Equivalent to C{tuple( iterateMinf( source ) )}. @see: L{iterateMinf}

soma.minf.api.writeMinf(destFile, args, format='XML', reducer=None)[source]

Creates a minf writer with L{createMinfWriter} and write the content of C{args} in it.

@param destFile: see L{createMinfWriter} @param args: series of values to write @type args: sequence or iterator @param format: see L{createMinfWriter} @param reducer: see L{createMinfWriter}

SubModule: notification

This module provides a notification system that can be used to register callbacks (I{i.e} Python callables) that will all be called by a single L{notify<Notifier.notify>} call.

@author: Yann Cointepas @author: Dominique Geffroy @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.notification.EditableTree(name=None, id=None, modifiable=True, content=[], valid=True)[source]

The base class to model a tree of items. This class can be derived to change implementation.

An EditableTree contains items which can be
  • an item branch : it contains other items as children
  • an item leaf : doesn’t have children

The list of items is an ObservableSortedDictionary which notifies its changes to registred listeners. If the tree is modifiable, new items can be added. Every item is an EditableTree.Item. EditableTree is iterable over its items.

EditableTree also inherits from ObservableAttributes, so registred listeners can be notified of attributes value change.

To call a method when item list changes:: C{editableTree.addListener(callbackMethod)} To call a method when an item list changes at any depth in the tree:: C{editableTree.addListenerRec(callbackMethod)} To call a method when an attribute of the tree changes:: C{editableTree.onAttributeChange(attributeName, callbackMethod)} To call a method when an attribute changes at any depth in the tree:: C{editableTree.onAttributeChangeRec(attributeName, callbackMethod)}

@type defaultName: string @cvar defaultName: default name of the tree

@type name: string @ivar name: the name of the tree @type modifiable: boolean @ivar modifiable: if true, new items can be added, items can be deleted and modified @type unamed: boolean @param unamed: indicates if name parameter was none, so the tree has the default name. @type valid: bool @ivar valid: indicates if the tree is valid (if not it may be hidden in a graphical representation)

class Branch(name=None, id=None, icon=None, tooltip=None, copyEnabled=True, modifiable=True, delEnabled=True, content=[], valid=True)[source]

A Branch is an item that can contain other items. It inherits from L{Item} and from L{ObservableSortedDictionary}, so it can have children items.

All parameters must have default values to be able to create new elements automatically

add(item)[source]

Add an item in the tree. If this item’s id is already present in the tree as a key, add the item’s content in the corresponding key. recursive method

compItems(id1, id2)[source]

Comparison function return 1 if i1 > i2 -1 if i1 < i2 0 if they are equal

sort(func=None)[source]

Recursive sort of the tree : items are sorted in all branches.

class EditableTree.Item(name=None, id=None, icon=None, tooltip=None, copyEnabled=True, modifiable=True, delEnabled=True, valid=True, *args)[source]

Base element of an EditableTree The attributes : - icon : filename of the image that can be used to represent the item - name : Text representation of the item - movable : true if the item can be moved in the tree - modifiable : true if the item can change (add children, change text, icon) - delEnabled : true if deletion of the item is allowed

Item inherits from L{ObservableAttributes}, so it can notify registred listener of attributes changes (for example name).

@type icon: string @ivar icon: file name of an icon to represent the item. @type name: string @ivar name: name of the item @type tooltip: string @ivar tooltip: description associated to the item @type copyEnabled: boolean @ivar copyEnabled: indicates if this item can be copied @type modifiable: boolean @ivar modifiable: indicates if the item can be modified (renamed for example) @type delEnabled: boolean @ivar delEnabled: indicates if the item can be deleted @type valid: bool @ivar valid: indicates if current item is valid (if not it may be hidden in a graphical representation)

isDescendant(item)[source]

Return true if current item is a child or a child’s child... of the item in parameter

isLeaf()[source]

Must be redefined in subclasses to say if the item is a leaf

setAllModificationsEnabled(bool)[source]

Recursivly enables or disables item’s modification.

class EditableTree.Leaf(name='new', id=None, icon=None, tooltip=None, copyEnabled=True, modifiable=True, delEnabled=True, valid=True)[source]

A tree item that cannot have children items

EditableTree.add(item)[source]

Add an item in the tree. If this item’s id is already present in the tree as a key, add the item’s content in the corresponding key. recursive method

EditableTree.addListenerRec(listener)[source]

Add a listener to the tree recursivly at every level. The listener is added to the notifier of all branches of the tree.

EditableTree.compItems(id1, id2)[source]

Comparison function return 1 if i1 > i2 -1 if i1 < i2 0 if they are equal

EditableTree.isDescendant(item)[source]

Return true if current item is a child or a child’s child... of the item in parameter Always false for the root of the tree

EditableTree.onAttributeChangeRec(attributeName, listener)[source]

Add a listener of the changes of this attribute value in the tree recursivly at every level. The listener is added to the attribute change notifier of all branches of the tree.

EditableTree.removeEmptyBranches()[source]

If a branch item doesn’t contain any leaf or branch that contains leaf recursivly, the item is removed from the tree.

EditableTree.sort(func=None)[source]

Recursive sort of the tree : items are sorted in all branches.

class soma.notification.Notifier(parameterCount=None)[source]

Register a series of functions (or Notifiers instances) which are all called whith L{notify} method. The calling order is the registering order. If a Notifier is registered, its L{notify} method is called whenever C{self.notify()} is called.

@type parameterCount: integer @param parameterCount: if not None, each registered function must be callable with that number of arguments (checking is done on registration).

add(listener)[source]

Register a callable or a Notifier that will be called whenever L{notify} is called. If the notifier has a C{parameterCount}, L{checkParameters.checkParameterCount} is used to verify that C{listener} can be called with C{parameterCount} parameters. @type listener: Python callable (function, method, I{etc.}) or L{Notifier} instance @param listener: item to add to the notification list.

delayNotification(ignoreDoubles=False)[source]

Stop notification until L{restartNotification} is called. After a call to L{delayNotification}, all calls to L{notify} will only store the notification parameters until L{restartNotification} is called. @type ignoreDoubles: C{bool} @param ignoreDoubles: If C{True} (the default), all calls to L{notify} with the same parameters as a previous call will be ignored (I{i.e.} notification will be done only once for two identical calls).

notify(*args)[source]

Calls all the registered items in the notification list. All the parameters given to L{notify} are passed to the items in the list. For items in the list that are L{Notifier} instance, their L{notify} method is called @see: L{delayNotification}, L{restartNotification}

remove(listener)[source]

Remove an item from the notification list. Do nothing if C{listener} is not in the list. @type listener: Python callable or L{Notifier} instance @param listener: item previously registered with .L{add}. @rtype: bool @return; C{True} if a listener has been removed, C{False} otherwise.

restartNotification()[source]

Restart notifications that have been delayed by L{delayNotification}. All the calls to L{notify} that has been done between the call to L{delayNotification} and the call to L{restartNotification}, are applied immediately.

class soma.notification.ObservableAttributes(*args, **kwargs)[source]

L{ObservableAttributes} allow to track modification of attributes at runtime. By registering callbacks, it is possible to be warn of the modification (setting and deletion) of any attribute of the instance.

delayAttributeNotification(ignoreDoubles=False)[source]

Stop attribute modification notification until L{restartAttributeNotification} is called. After a call to L{delayAttributeNotification}, all modification notification will only be stored until L{restartAttributeNotification} is called. This call is recursive on all attributes values that are instance of L{ObservableAttributes}.

@type ignoreDoubles: C{bool} @param ignoreDoubles: If C{True} (C{False} is the default), all

notification with the same parameters as a previous notification will be ignored (I{i.e.} notification will be done only once for two identical events).
notifyAttributeChange(name, value, oldValue=<undefined>)[source]

First, call functions registered for modification of the attribute named C{name}, then call functions registered for modification of any attribute.

@see: L{onAttributeChange}

onAttributeChange(first, second=None)[source]

Register a function to be called when an attribute is modified or deleted. To call the function for any attribute modification, use the following syntax:

instance.onAttributeChange( function )
To register a function for a named attribute, use the following syntax::
instance.onAttributeChange( attributeName, function )

The registered function can have 0 to 4 parameters. Depending on its number of parameters, it will be called with the following values:

  • 4 parameters: C{( object, attributeName, newValue, oldValue )}
  • 3 parameters: C{( attributeName, newValue, oldValue )}
  • 2 parameters: C{( newValue, oldValue )}
  • 1 parameter: C{( newValue )}
Where:
  • C{object} is the object whose attribute has been modified or deleted.
  • C{attributeName} is the name of the modified or deleted attribute.
  • C{newValue} is the value of the attribute after modification or C{Undefined} if the attribute has been deleted.
  • C{oldValue} is the value of the attribute before modification. If the attribute where not defined, C{oldValue = L{Undefined}}.

If the function accepts a variable number of parameters, it will be called with the maximum number of arguments possible.

removeOnAttributeChange(first, second=None)[source]

Remove a function previously registered with L{onAttributeChange}. To remove a function, the arguments of L{removeOnAttributeChange} must be the same as those passed to L{onAttributeChange} to register the function.

restartAttributeNotification()[source]

Restart notifications that have been delayed by L{delayAttributeNotification}. All the modifications that happened between the call to L{delayAttributeNotification} and the call to L{restartAttributeNotification}, are notified immediately.

class soma.notification.ObservableList(content=None)[source]

A list that notifies its changes to registred listeners. Inherits from python list and contains an instance of L{soma.notification.Notifier} (onChangeNotifier).

Example:

l=ObservableList() l.addListener(update) l.append(e) -> calls onChangeNotifier.notify(INSERT_ACTION, [e], len(l)) -> calls update(INSERT_ACTION, [e], len(l))

@type INSERT_ACTION: int @cvar INSERT_ACTION: used to notify insertion of new elements in the list @type REMOVE_ACTION: int @cvar REMOVE_ACTION: used to notify elements deletion @type MODIFY_ACTION: int @cvar MODIFY_ACTION: used to notify elements modification

@type onChangeNotifier: Notifier @ivar onChangeNotifier: the Notifier’s notify method is called when the list has changed.

@type content: list @param content: elements to initialize the list’s content

addListener(listener)[source]

Registers the listener callback method in the notifier. The method must take 3 arguments : action, elems list, position

  • INSERT_ACTION : elems have been inserted at position in the list
  • REMOVE_ACTION : elems have been removed [at position] in the list
  • MODIFY_ACTION : at position, some elements have been replaced by elems

The position given in the notify method will be between 0 and len(self)

@type listener: function @param listener: function to call to notify changes

append(elem)[source]

Adds the element at the end of the list. Notifies an insert action.

extend(l)[source]

Adds the content of the list l at the end of current list. Notifies an insert action.

getIndexInRange(i)[source]

Returns an index in the range of the list. if i<0 return 0, if i>len(self), return len(self)

getPositiveIndex(i)[source]

Returns an index between 0 and len(self) - if i is negative, it is replaced by len(self)+i - if index is again negative, it is replaced by 0 - if index is beyond len(self) it is replaced by len(self)

insert(pos, elem)[source]

Inserts elem at position pos in the list. Notifies an insert action.

itemIndex(item)[source]

Returns item’s index in the list. It’s different from index method that returns index of the first element that has the same value as item.

pop(pos=None)[source]

Removes the element at position pos or the last element if pos is None. Notifies a remove action. @return: the removed element

remove(elem)[source]

Removes the first occurence of elem in the list. Notifies a remove action.

reverse()[source]

Inverses the order of the list. Notifies a modify action.

sort(func=None)[source]

Sorts the list using function func to compare elements. Notifies a modify action. @type func: function elem*elem->int @param func: comparison function, return -1 if e1<e2, 1 if e1>e2, 0 if e1==e2

class soma.notification.ObservableNotifier(parameterCount=None)[source]

This notifier can notify when the first listener is added and when the last listener is removed. It enables to use the notifier only when there’s some listeners registred.

@type onAddFirstListener: Notifier @ivar onAddFirstListener: register a listener on this notifier to be called when the first listener is registred on ObservableNotifier @type onRemoveLastListener: Notifier @ivar onRemoveLastListener: register a listener on this notifier to be called when the last listener is removed from ObservableNotifier

@type parameterCount: integer @param parameterCount: if not None, each registered function must be callable with that number of arguments (checking is done on registration).

class soma.notification.ObservableSortedDictionary(*args)[source]

A sorted dictionary that notifies its changes. Inherits from python list and contains an instance of L{soma.notification.Notifier} (onChangeNotifier).

Example:

d=ObservableSortedDictionary() d.addListener(update) d.insert(index, key, e) -> calls onChangeNotifier.notify(INSERT_ACTION, [e], index) -> calls update(INSERT_ACTION, [e], index)

@type INSERT_ACTION: int @cvar INSERT_ACTION: used to notify insertion of new elements in the dictionary @type REMOVE_ACTION: int @cvar REMOVE_ACTION: used to notify elements deletion @type MODIFY_ACTION: int @cvar MODIFY_ACTION: used to notify elements modification

@type onChangeNotifier: Notifier @ivar onChangeNotifier: the Notifier’s notify method is called when the dictionaty has changed.

Initialize the dictionary with a list of ( key, value ) pairs.

addListener(listener)[source]

Registers the listener callback method in the notifier. The method must take 3 arguments : action, elems list, position

  • INSERT_ACTION : elems have been inserted at position in the dictionary
  • REMOVE_ACTION : elems have been removed [at position] in the dict
  • MODIFY_ACTION : at position, some elements have been replaced by elems

The position given in the notify method will be between 0 and len(self)

@type listener: function @param listener: function to call to notify changes

clear()[source]

Remove all items from dictionary

insert(index, key, value)[source]

insert a ( C{key}, C{value} ) pair in sorted dictionary before position C{index}. If C{key} is already in the dictionary, a C{KeyError} is raised. @type index: integer @param index: index of C{key} in the sorted keys @param key: key to insert @param value: value associated to C{key}

sort(func=None)[source]

Sorts the dictionary using function func to compare keys. Notifies a modify action. @type func: function key*key->int @param func: comparison function, return 1 if e1<e2, -1 if e1>e2, 0 if e1==e2

class soma.notification.ReorderedCall(function, parametersOrder)[source]

@todo: documentation

class soma.notification.VariableParametersNotifier(mainParameters, *otherParameters)[source]

This class is a notifier that can register functions with various arguments count.

@see: L{Notifier.__init__} @todo: documentation

add(listener)[source]

@see: L{Notifier.add} @todo: documentation

remove(listener)[source]

@see: L{Notifier.remove} @todo: documentation

SubModule: path

Some useful functions to manage file or directorie names.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

soma.path.find_in_path(file, path=None)[source]

Look for a file in a series of directories. By default, directories are contained in C{PATH} environment variable. But another environment variable name or a sequence of directories names can be given in C{path} parameter.

Examples::
find_in_path( ‘sh’ ) could return ‘/bin/sh’ find_in_path( ‘libpython2.5.so’, ‘LD_LIBRARY_PATH’ ) could return ‘/usr/local/lib/libpython2.5.so’

Read all symlinks in path to return the “real” path.

With the following configuration::
/usr/local/software-1.0 is a directory /usr/local/software-1.0/bin is a directory /usr/local/software-1.0/bin/command is a file /usr/local/software is a symlink to software-1.0 /home/bin/command is a symlink to /usr/local/software/bin/command

no_symlink( ‘/home/bin/command’ ) would return ‘/usr/local/software-1.0/bin/command’

soma.path.path_separator

Character used to separate directories in environment variables such as PATH

soma.path.relative_path(path, referenceDirectory)[source]

Return a relative version of a path given a reference directory.

os.path.join( referenceDirectory, relative_path( path, referenceDirectory ) ) returns os.path.abspath( path )

relative_path( ‘/usr/local/brainvisa-3.1/bin/brainvisa’, ‘/usr/local’ ) returns ‘brainvisa-3.1/bin/brainvisa’

relative_path( ‘/usr/local/brainvisa-3.1/bin/brainvisa’, ‘/usr/local/bin’ ) returns ‘../brainvisa-3.1/bin/brainvisa’

relative_path( ‘/usr/local/brainvisa-3.1/bin/brainvisa’, ‘/tmp/test/brainvisa’ ) returns ‘../../../usr/local/brainvisa-3.1/bin/brainvisa’

soma.path.split_path(path)[source]

Iteratively apply C{os.path.split} to build a list. Ignore trailing directory separator.

Examples::
split_path( ‘/home/myaccount/data/file.csv’ ) returns [ ‘/’, ‘home’, ‘myaccount’, ‘data’, ‘file.csv’ ] split_path( ‘home/myaccount/data/file.csv’ ) returns [ ‘home’, ‘myaccount’, ‘data’, ‘file.csv’ ] split_path( ‘/home/myaccount/data/’ ) returns [ ‘/’, ‘home’, ‘myaccount’, ‘data’ ] split_path( ‘/home/myaccount/data’ ) returns [ ‘/’, ‘home’, ‘myaccount’, ‘data’ ] split_path( ‘’ ) returns [ ‘’ ]

SubModule: plugins

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: pyro

This module provides the class L{ThreadSafeProxy}.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: signature

See L{signature<soma.signature.api>} module for documentation.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: singleton

Singleton pattern.

class soma.singleton.Singleton(*args, **kwargs)[source]

Implements the singleton pattern. A class deriving from Singleton can have only one instance. The first instanciation will create an object and other instanciations return the same object. Note that the __init__() method (if any) is still called at each instanciation (on the same object). Therefore, Singleton derived classes should define __singleton_init__() instead of __init__() because the former is only called once.

Example:

from singleton import Singleton

class MyClass( Singleton ):
  def __singleton_init__( self ):
    self.attribute = 'value'

o1 = MyClass()
o2 = MyClass()
print o1 is o2

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

SubModule: somatime

Utility classes and functions for time handling.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: sorted_dictionary

Sorted dictionary behave like a dictionary but keep the item insertion order.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.sorted_dictionary.SortedDictionary(*args)[source]

Sorted dictionary behave like a dictionary but keep the item insertion order.

Example::
from SortedDictionary import SortedDictionary sd = SortedDictionary( ( ‘fisrt’, 1 ), ( ‘second’, 2 ) ) sd[ ‘third’ ] = 3 sd.insert( 0, ‘zero’, 0 ) sd.items() == [(‘zero’, 0), (‘fisrt’, 1), (‘second’, 2), (‘third’, 3)]

Initialize the dictionary with a list of ( key, value ) pairs.

clear()[source]

Remove all items from dictionary

compValues(key1, key2)[source]

Use this comparaison function in sort method parameter in order to sort the dictionary by values. if data[key1]<data[key2] return -1 if data[key1]>data[key2] return 1 if data[key1]==data[key2] return 0

index(key)[source]

Returns the index of the key in the sorted dictionary, or -1 if this key isn’t in the dictionary.

insert(index, key, value)[source]

insert a ( C{key}, C{value} ) pair in sorted dictionary before position C{index}. If C{key} is already in the dictionary, a C{KeyError} is raised. @type index: integer @param index: index of C{key} in the sorted keys @param key: key to insert @param value: value associated to C{key}

items()[source]

@rtype: list @return: sorted list of C{( key, value)} pairs

iteritems()[source]

returns an iterator over the sorted (key, value) pairs

iterkeys()[source]

returns an iterator over the sorted keys

itervalues()[source]

returns an iterator over the sorted values

keys()[source]

@rtype: list @return: sorted list of keys

sort(func=None)[source]

Sorts the dictionary using function func to compare keys.

@type func: function key*key->int @param func: comparison function, return -1 if e1<e2, 1 if e1>e2, 0 if e1==e2

values()[source]

@rtype: list @return: sorted list of values

SubModule: stringtools

This module contains text related functions.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: tggui

See L{soma.tggui.api} module for documentation.

@author: Nicolas Souedet @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: thread_calls

This module is useful whenever you need to be sure that a function (more exactly anything that can be called by python) is executed in a particular thread.

For exemple, if you use PyMat (which is an interface between Python and a Matlab, see U{http://claymore.engineer.gvsu.edu/~steriana/Python} for more information) it is necessary that all calls to pymat.eval are done by the same thread that called pymat.open. If you want to use PyMat in a multi-threaded application, you will have to build appropriate calling mechanism as the one proposed in this module.

The main idea is to use a list containing functions to be called with their parameters. A single thread is used to get entries from the list and execute the corresponding functions. Any thread can put a call request on the list either asynchonously (the requesting thread continues to run without waiting for the call to be done) or synchronously (the requesting thread is stopped until the call is done and the result available).

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.thread_calls.SingleThreadCalls(thread=None)[source]

Allows the registration of functions that are going to be called from a single thread. This single thread must continuously call L{processFunctions}. Registration can be blocking (see L{call}) or non blocking (see L{push}). Blocking registration wait for the function to be processed and returns its result (or raises its exception). Non blocking registration put the function in the list and return immediately, ignoring any return value or exception.

Example::
stc = SingleThreadCall() processingThread = threading.Thread( target=stc.processingLoop ) stc.setProcessingThread( processingThread ) processingThread.start()

The thread passed in parameter is the processing thread of this SingleThreadCalls. When started (which is not been done by C{SingleThreadCall}) it must continuously call L{processFunctions} to execute the functions that have been registered with L{call} and L{push}. @type thread: C{threading.Thread} instance or C{None} @param thread: Processing thread. If C{None}, C{threading.currentThread()} is used.

call(function, *args, **kwargs)[source]

Register a function call and wait for the processing thread to call it. Returns the result of the function. If an exception is raised during the execution of the function, this exception is also raised by C{call()}. Therefore C{call} behave like a direct call to the function. It is possible to use C{call} from the processing thread. In this case, no registration is done (the function is executed immedialey). @type function: callable @param function: function to execute @param args: parameters of the function @param kwargs: keyword parameters of the function @return: the result of the function call

processFunctions(blocking=False)[source]

This method extracts all functions (along with their parameters) from the queue and execute them. It returns the number of function executed C{None} if self.stop() has been called (in that case the processing loop must end).

The processing thread must continuoulsy call this method until C{None} is returned.

@type blocking: bool @param blocking: if C{blocking} is False (the default), C{processFunctions} returns C{0} immediately if it cannot access the function list (for example if it is locked by another thread that is registering a function). If C{blocking} is True, C{processFunctions} waits until the function list is available.

@see: L{processingLoop}

processingLoop()[source]

Continuously execute L{processFunctions} until it returns C{None} @see: L{processFunctions}

push(function, *args, **kwargs)[source]

Same as L{call} method but always put the function on the queue and returns immediatly. If C{push} is called from the processing thread, the function is called immediately (I{i.e.} synchronously). @type function: callable @param function: function to execute @param args: parameters of the function @param kwargs: keyword parameters of the function @return: C{None}

setProcessingThread(thread)[source]

Define the thread that processes the functions. The behaviour of L{call} and L{push} is different if called from the processing thread or from another one. @type thread: C{threading.Thread} instance @param thread: Processing thread

stop()[source]

Tells the processing thread to finish the processing of functions in the queue and then stop all processing. This call pushes a special value on the function list. All functions that are on the list before this value will be processed but functions registered after the special value will be ignored.

SubModule: translation

Importing this module defines a L{translate} function that translate messages of an application. The current implementation does nothing but defines a minimum API.

Example::

from soma.translation import translate as _ try:

f = open( configurationFileName )
except OSError:
raise RuntimeError( _( ‘Cannot open configuration file “%s”’ ) % ( configurationFileName, ) )

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

soma.translation.translate(message)[source]

Translate C{message} into the current application language. Current implementation does nothing, C{message} is returned untouched.

@param message: message to translate @type message: unicode

SubModule: undefined

L{Undefined} is a constant that can be used as a special value different from any other Python value including C{None}.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

soma.undefined.Undefined

C{Undefined} contains the instance of UndefinedClass and can be used to check wether a value is undefined or not.

Example::

from soma.undefined import Undefined

if object.value is Undefined:
# do something
class soma.undefined.UndefinedClass(*args, **kwargs)[source]

C{UndefinedClass} instance is used to represent an undefined attribute value when C{None} cannot be used because it can be a valid value. Should only be used for value checking.

@see: L{Undefined}

The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.

SubModule: userinfoprovider

class soma.userinfoprovider.BasicUserInfoProvider[source]

C{BasicUserInfoProvider} is the basic implementation of the L{UserInfoProvider} class.

getUsers(users=None, attributes=None, formats=None, sorts=None)[source]

Method that retrieve users info for the C{BasicUserInfoProvider}. It uses a ldap server. @type users : L{list} of L{list}s. @param users : users to use. @type attributes : L{list} @param attributes : list of basic attributes to get (example : [ ‘sn’, ‘givenName’, ‘uid’ ] ). @type formats : L{list} @param formats : list of tuple that contains names of the matching user field and formats to apply

(example : [ ( ‘lastname’, ‘%(sn)s %(givenName)s’), ( ‘login’, ‘%(uid)s’) ] ).

@type sorts : L{list} of L{int} @param sorts : list of sorts to apply to get user information (example : [ 0 ] ).

Values are the indexes of output fields used to sort data.

@return : L{list} of L{list}s containing users infos.

class soma.userinfoprovider.LdapUserInfoProvider[source]

C{LdapUserInfoProvider} is the ldap implementation of the L{UserInfoProvider} class.

getUsers(server=None, base=None, filter=None, attributes=None, formats=None, sorts=None)[source]

Method that retrieve users info for the C{LdapUserInfoProvider}. It uses a ldap server. @type server : L{str} @param server : ldap server address to get data from. @type base : L{str} @param base : ldap base entity to get user info from. @type filter : L{str} @param filter : ldap filter to get filtered user info (example : (cn=*) ). @type attributes : L{list} @param attributes : list of ldap attributes to get (example : [ ‘sn’, ‘givenName’, ‘uid’ ] ). @type formats : L{list} @param formats : list of tuple that contains names of the matching user field and formats to apply

(example : [ ( ‘lastname’, ‘%(sn)s %(givenName)s’), ( ‘login’, ‘%(uid)s’) ] ).

@type sorts : L{list} of L{int} @param sorts : list of sorts to apply to get user information (example : [ 0 ] ).

Values are the indexes of output fields used to sort data.

@return : L{list} of L{list}s containing users infos.

class soma.userinfoprovider.NisUserInfoProvider[source]

C{NisUserInfoProvider} is the nis implementation of the L{UserInfoProvider} class.

getUsers(map=None, domain=None, separator=None, indexes=None, filter=None, attributes=None, formats=None, sorts=None, columnsplitters={4: <function cn_splitter at 0x3258c08>})[source]

Method that retrieve users info for the C{NisUserInfoProvider}. It uses a nis server. @type map : L{str} @param map : nis map to get user info from. @type domain : L{str} @param domain : nis domain to get user info from. @type separator : L{str} @param separator : separator to use to parse nis map records. @type indexes : L{list} @param indexes : list of indexes of the columns to use in nis map records. @type filter : L{str} @param filter : regular expression used to select nis map records to get user info from. @type attributes : L{list} @param attributes : L{list} of attributes to use. They must match the order of indexes

(for example : [ ‘sn’, ‘givenName’, ‘uid’ ], this means that the column corresponding to the first index value (defined by indexes) will contain the sn, the column corresponding to the second index value (defined by indexes) will contain the ‘givenName’, etc ... ).

@type formats : L{list} @param formats : list of tuple that contains names of the matching user field and formats to apply

(example : [ ( ‘lastname’, ‘%(sn)s %(givenName)s’), ( ‘login’, ‘%(uid)s’) ] ).

@type sorts : L{list} of L{int} @param sorts : list of sorts to apply to get user information (example : [ 0 ] ).

Values are the indexes of output fields used to sort data.

@return : L{list} of L{list}s containing users infos.

class soma.userinfoprovider.UserInfo(completename=None, lastname='', firstname='', login='')[source]

C{UserInfo} is a class that contains user information.

getCompleteName()[source]

Get the C{UserInfo} complete name (i.e. Formatted first name and last name). @return : C{UserInfo} complete name.

class soma.userinfoprovider.UserInfoProvider[source]

C{UserInfoProvider} is a base class for providers that deal with user information.

static getUserInfoProviderInstance(providertype=None)[source]

Static method that instanciates a C{UserInfoProvider} object. @type providertype : L{UserInfoProviderType} @param providertype : type of the provider to instanciates. @return : C{UserInfoProvider} instance for the provider

type. If providertype is None, it tries to get provider type from application configuration.
getUsers()[source]

Method that retrieve users info for the C{UserInfoProvider}. @return : L{list} of {list}s containing users infos.

soma.userinfoprovider.getUsersInfo(dataset, formats, keys, sorts)[source]

Get user infos from a dataset (i.e. a L{list} of L{list}s. For each record of the dataset it applies formats. Each new record of the resulting dataset is a list that contains L{UserInfo} objects, resulting of the formats applied to each record. @type dataset : list @param dataset : L{list} of L{list}s that contain data @type formats : list. @param formats : L{list} of L{list} of L{string}s that contains

resulting column name and formats to apply.

@type keys : list. @param keys : L{list} of L{string}s that contain the keys for each

dataset column. The keys must be used in formats. As an example, keys can be [ ‘cn’, ‘guid’ ] and formats [ ‘%(cn)s-%(guid)s’, ‘%(cn)s’, ‘%(guid)s’ ]. This example will result in a dataset with 3 columns.

@type sorts : list. @param sorts : L{list} of L{str}s that contain the name of columns to

use for sorting the resulting dataset.

@return : L{list} of L{UserInfo}s sorted using the sort fields.

SubModule: uuid

Universal unique identifier.

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.uuid.Uuid(uuid=None)[source]

An Uuid instance is a universal unique identifier. It is a 128 bits random value.

Uuid constructor. If C{uuid} is ommited or C{None}, a new random Uuid is created, if it is a string if must be 36 characters long and follow the pattern C{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where C{X} is an hexadecimal digit (example: C{‘ad2d8fb0-7831-50bc-2fb6-5df048304001’}). If C{uuid} is a Uuid instance, no new instance is created, in this case, C{Uuid( uuid )} returns C{uuid}.

SubModule: zipfile

SubModule: wip.application

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

SubModule: wip.configuration

@author: Yann Cointepas @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.wip.configuration.Configuration[source]

A L{Configuration} instance is a list that must contains only objects deriving from L{ConfigurationGroup}.

class soma.wip.configuration.ConfigurationGroup(*args, **kwargs)[source]

Represent one element of a L{Configuration} object.

SubModule: wip.singleton

Singleton pattern.

A class deriving from C{Singleton} can have only one instance. The first instanciation will create an object and other instanciations return the same object. Note that the C{__init__} method (if any) is still called at each instanciation (on the same object). Therefore, C{Singleton} derived class should define C{__singleton_init__} instead of C{__init__} because the former is only called once.

Usage examples :
  • Inherits from a Singleton class
class A(object):
def __init__(self):
print “call init A” super(A, self).__init__()
class As(Singleton, A): # As = singleton(A)
def __singleton_init__(self):
print “call singleton init As” A.__init__(self)
class Asd(As): # Asd = singleton(A) derived class

# derived class must implement __singleton_init__ method, not __init__ method def __singleton_init__(self):

print “call singleton init Asd” As.__singleton_init__(self)

# it is possible to have several class which inherits from the same singleton, # the singleton instance is stored in the derived class context class Asd2(As):

# if __new__ is redefined, it will be called before the new in the super class def __new__(cls, forceNewInstance=False):

if forceNewInstance:
self=object.__new__(cls) self.__singleton_init__()
else:
self=super(Asd2, cls).__new__(cls)

return self

def __singleton_init__(self):
print “call singleton init Asd2” As.__singleton_init__(self)

a=Asd() a2=Asd() print a, a2, a is a2 a3=Asd2() a4=Asd2(forceNewInstance=True) print a3, a4, a3 is a4 print Asd._singleton_instance print Asd2._singleton_instance

@author: Dominique Geffroy @organization: U{NeuroSpin<http://www.neurospin.org>} and U{IFR 49<http://www.ifr49.org>} @license: U{CeCILL version 2<http://www.cecill.info/licences/Licence_CeCILL_V2-en.html>}

class soma.wip.singleton.ObservableSingleton(*args, **kwargs)[source]

Implements the singleton pattern and notifies when the singleton instance is created. It is possible to get the current singleton instance without creating a new instance if it doesn’t exist, using create constructor’s parameter. By default this option is True, so an instance is created the first time the constructor is called.

A class deriving from C{Singleton} can have only one instance. The first instanciation will create an object and other instanciations return the same object. Note that the C{__init__} method (if any) is still called at each instanciation (on the same object). Therefore, C{Singleton} derived class should define C{__singleton_init__} instead of C{__init__} because the former is only called once.

Example:

from soma.wip.singleton import ObservableSingleton

class MyClass( ObservableSingleton ):
  def __singleton_init__( self, *args, **kwargs ):
    self.attribute = 'value'

def onCreate():
  print "MyClass instance created"

MyClass.addCreateListener(onCreate)
o1 = MyClass(create=False)
print o1 # o1 is None
o1 = MyClass()
o2 = MyClass()
print o1 is o2

C{__init__} method of L{Singleton} derived class should do nothing. Derived classes must define C{__singleton_init__} instead of C{__init__}.

SubModule: wip.temporary