# -*- coding: utf-8 -*- # This software and supporting documentation are distributed by # bioPICSEL # CEA/DSV/I²BM/MIRCen/LMN, Batiment 61, # 18, route du Panorama # 92265 Fontenay-aux-Roses # France # # This software is governed by the CeCILL license version 2 under # French law and abiding by the rules of distribution of free software. # You can use, modify and/or redistribute the software under the # terms of the CeCILL license version 2 as circulated by CEA, CNRS # and INRIA at the following URL "http://www.cecill.info". # # As a counterpart to the access to the source code and rights to copy, # modify and redistribute granted by the license, users are provided only # with a limited warranty and the software's author, the holder of the # economic rights, and the successive licensors have only limited # liability. # # In this respect, the user's attention is drawn to the risks associated # with loading, using, modifying and/or developing or reproducing the # software by the user in light of its specific status of free software, # that may mean that it is complicated to manipulate, and that also # therefore means that it is reserved for developers and experienced # professionals having in-depth computer knowledge. Users are therefore # encouraged to load and test the software's suitability as regards their # requirements in conditions enabling the security of their systems and/or # data to be ensured and, more generally, to use and operate it in the # same conditions as regards security. # # The fact that you are presently reading this means that you have had # knowledge of the CeCILL license version 2 and that you accept its terms. ''' Utility to manage user interface checks for BrainRAT toolbox. @author: Nicolas Souedet @organization: U{MIRCen} @license: U{CeCILL version 2} ''' __docformat__ = "epytext en" import operator, types, os, string, csv from soma.html import htmlEscape # Constants advanceduserlevel = 3 operatorsymbols = { operator.lt : '<', operator.le : '<=', operator.gt : '>', operator.ge : '>=', operator.eq : '==' } # Standard error messages messages = { 'processingerrormessage' : 'An error occured during process excution. Please refer to log (menu \'BrainVISA->Show Log\') for details.', 'valueerrormessage' : '\'%(fieldname)s\' value is not valid. A value %(operator)s %(checkvalue)s is required.', 'listvalueerrormessage' : '\'%(fieldname)s\' values are not valid. A list of values %(operator)s %(checkvalue)s is required.', 'overwritemessage' : 'No \'%(outputfield)s\' set. Do you want to overwrite existing \'%(inputfield)s\' :\n \'%(file)s\'?' } if '_t_' in globals()[ '__builtins__' ] : # A brainvisa translator was found so we apply translations for key, value in messages.iteritems() : messages[ key ] = _t_( value ) def checkvalue( fieldname, value, checkvalues, checkoperators, messagekey = 'valueerrormessage' ) : result = None if not type(checkvalues) in [ types.TupleType, types.ListType ] : checkvalues = [ checkvalues ] if not type(checkoperators) in [ types.TupleType, types.ListType ] : checkoperators = [ checkoperators ] # Complete chechvalues if needed if len( checkvalues ) > len( checkoperators ) : checkoperators += ( checkoperators[ -1 ], ) * ( len( checkvalues ) - len( checkoperators ) ) elif len( checkoperators ) > len( checkvalues ) : checkvalues += ( checkvalues[ -1 ], ) * ( len( checkoperators ) - len( checkvalues ) ) for index in xrange( len( checkoperators ) ) : if ( not checkoperators[ index ]( value, checkvalues[ index ] ) ) : if messagekey in messages : message = messages[ messagekey ] else : message = messagekey return message % { 'fieldname' : fieldname, 'operator' : operatorsymbols[ checkoperators[ index ] ], 'checkvalue' : unicode( checkvalues[ index ] ) } return None def value( context, fieldname, value, checkvalues, checkoperators, messagekey = 'valueerrormessage' ) : check = checkvalue( fieldname, value, checkvalues, checkoperators, messagekey ) if ( check ) : context.error( htmlEscape( check ) ) return False else : return True def objectattr( context, object, fieldname, checkvalues, checkoperators, messagekey = 'valueerrormessage' ) : attributevalue = getattr( object, fieldname, None ) return value( context, fieldname, attributevalue, checkvalues, checkoperators, messagekey ) def objectlistattr( context, object, fieldname, checkvalues, checkoperators, messagekey = 'listvalueerrormessage' ) : attributelistvalue = getattr( object, fieldname, None ) for attributevalue in attributelistvalue : if not value( context, fieldname, attributevalue, checkvalues, checkoperators, messagekey ) : return False return True def formatAsHTMLTable( table, linesep = os.linesep, columnsep = "|", quoting = csv.QUOTE_NONE ) : res = '' #for l in string.splitfields(table, sep = linesep) : for l in csv.reader(table, delimiter = columnsep, quoting = quoting) : res += '' for c in l : res += '' res += '' res += '
' res += htmlEscape( c ) res += '
' #htmltable = '
' + htmltable #htmltable = htmltable.replace( linesep, "
" ) #htmltable = htmltable.replace(columnsep, "" ) #htmltable = htmltable + '
' return res