Many miscelaneous all-purpose classes and functions...
BufferAndFile instances are used to read data from a file (for instance for identification) and “put back” the data on the file.
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.
Change the internal file object (keeps the internal buffer untouched).
Return a new L{BufferAndFile} instance with the same internale buffer and the same internal file object as C{self}.
Open a file with built-in open() and create a BufferAndFile instance.
@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>}
Utility classes and functions for debugging.
Return a dictionary that gives information about a frame corresponding to a function call. The directory contains the following items:
This module contains decorators.
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
Raised when creating an Enum with non-string keys
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__.
Utility classes and functions for Python callable.
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
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.
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
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
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)
@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>}
Utility functions for HTML format.
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.
Builder threading.Thread for files that were uploaded by fragments.
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.
Add FileFragment to the current FileBuilderInfo.
filefragment : FileFragment
FileFragment to add to the current FileBuilderInfo.
Add FileBuilderInfo to the queue.Queue if not already added.
queue.Queue to add FileBuilderInfo to.
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.
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.
Check that FileFragment matches the file to which it belongs.
filefragment : FileFragment
FileFragment to check that it belongs current FileBuilderInfo.
returns: True if the FileFragment belongs to the FileBuilderInfo, False otherwise.
Check that all required :py:class:`FileFragment`s exist and have the correct length.
Check that result file exists and has a rigth length.
Get FileFragment using its sha1 key for the current FileBuilderInfo.
filefragmentsha1 : string
sha1 key for the FileFragment.
returns: the found FileFragment or None if not found.
Get FileFragment for the current FileBuilderInfo.
Get the considered file hash for the current FileBuilderInfo.
Get the considered file length for the current FileBuilderInfo.
Get the considered file name for the current FileBuilderInfo.
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.
Get result directory for the current FileBuilderInfo. i.e. the name of the directory where the file will be rebuild.
Get result file name for the current FileBuilderInfo. i.e. the name of the file rebuilt.
Get the upload id for the current FileBuilderInfo.
Set the FileBuilderInfo status.
value : FileBuilderInfoStatus
FileBuilderInfoStatus that specify if the FileBuilderInfo status.
Class to manage FileBuilderInfo.
The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.
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.
Get a FileBuilderInfo list using the sha1 key of FileFragment. It retrieves all FileBuilderInfo that contains a FileFragment with the matching sha1 key.
sha1 key for the FileFragment.
Get the Queue.Queue that is used to put FileBuilderInfo once they are complete.
Transfer a FileBuilderInfo to the Queue.Queue.
FileBuilderInfo to put in Queue.Queue.
FileBuilderInfoStatus
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.
Delete data for the FileFragment local file.
filepath : string
string containing the file path to delete.
Get the base directory for the current FileFragment.
Get the file length for the current FileFragment.
Get the file name for the current FileFragment.
Get the length of the current FileFragment (i.e. the length of the FileFragment data).
Get a local data path for FileFragment.
Get the offset of the current FileFragment (i.e. the index of the first FileFragment data character in the result file).
Get the sha1 key of the current FileFragment (the sha1 key is used to process check sum on the FileFragment data).
Get the upload id for the current FileFragment.
Check that the current FileFragment has valid local data. Checks the file existence and sha1 key.
Check that the current FileFragment has valid local data. Checks the file existence and the size of the file.
Parses FileFragment from an xml document.
document : xml.dom.minidom.Document
xml.dom.minidom.Document containing the parsed xml header for the current FileFragment.
Read data for the FileFragment from a local file.
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.
LogLevel
LogManager
The __init__ method of Singleton derived class should do nothing. Derived classes must define __singleton_init__() instead of __init__.
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__.
Get the status for a file information.
uploadid : string
upload id.
basedirectory : string
relative directory path.
filename : string
file name.
filelength : long
file length.
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.
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.
Get upload directory path for an upload id.
uploadid : string
upload id.
returns: string containing the upload directory path.
Get the count of built files for an upload.
uploadid : string
upload id.
Get the length of built files for an upload.
uploadid : string
upload id.
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.
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.
Display current registered :py:class:`FileBuilderInfo`s.
filebuilderinfomanager
Create xml file build count response document.
filelength: long
specify the file upload build count.
returns: xml document containing the response.
Create xml file build length response document.
filelength: long
specify the file upload build length.
returns: xml document containing the response.
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.
Create xml upload response document.
upload: bool
specify if the data upload must be done or not.
returns: xml document containing the response.
Main httpupload function entry. It processes arguments and saves uploaded file on the server using it.
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>}
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__.
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.
Static methods declared to help extended import process.
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.
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.
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.
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.
Static generic handlers used as import rules.
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.
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.
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>}
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
Returns an iterator over all objects stored in a minf file.
from soma.minf.api import iterateMinf
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’}.
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:
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 )
Entirerly reads a minf file and returns its content in a tuple. Equivalent to C{tuple( iterateMinf( source ) )}. @see: L{iterateMinf}
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}
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>}
The base class to model a tree of items. This class can be derived to change implementation.
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)
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 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
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)
A tree item that cannot have children items
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
Add a listener to the tree recursivly at every level. The listener is added to the notifier of all branches of the tree.
Comparison function return 1 if i1 > i2 -1 if i1 < i2 0 if they are equal
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
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.
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).
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.
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).
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 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.
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.
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).
First, call functions registered for modification of the attribute named C{name}, then call functions registered for modification of any attribute.
@see: L{onAttributeChange}
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 )
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 )}
If the function accepts a variable number of parameters, it will be called with the maximum number of arguments possible.
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
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
Adds the content of the list l at the end of current list. Notifies an insert action.
Returns an index in the range of the list. if i<0 return 0, if i>len(self), return len(self)
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)
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.
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).
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.
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
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}
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>}
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.
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’
Character used to separate directories in environment variables such as PATH
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’
Iteratively apply C{os.path.split} to build a list. Ignore trailing directory separator.
@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>}
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>}
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>}
Singleton pattern.
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__.
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>}
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>}
Sorted dictionary behave like a dictionary but keep the item insertion order.
Initialize the dictionary with a list of ( key, value ) pairs.
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
Returns the index of the key in the sorted dictionary, or -1 if this key isn’t in the dictionary.
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}
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>}
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>}
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>}
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.
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.
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
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}
Continuously execute L{processFunctions} until it returns C{None} @see: L{processFunctions}
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}
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
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.
Importing this module defines a L{translate} function that translate messages of an application. The current implementation does nothing but defines a minimum API.
from soma.translation import translate as _ try:
f = open( 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>}
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>}
C{Undefined} contains the instance of UndefinedClass and can be used to check wether a value is undefined or not.
from soma.undefined import Undefined
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__.
C{BasicUserInfoProvider} is the basic implementation of the L{UserInfoProvider} class.
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.
C{LdapUserInfoProvider} is the ldap implementation of the L{UserInfoProvider} class.
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.
C{NisUserInfoProvider} is the nis implementation of the L{UserInfoProvider} class.
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.
C{UserInfo} is a class that contains user information.
C{UserInfoProvider} is a base class for providers that deal with user information.
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.
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.
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>}
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}.
@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>}
@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>}
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.
# 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>}
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__}.