# -*- coding: utf-8 -*-
# This software and supporting documentation are distributed by
# Institut Federatif de Recherche 49
# CEA/NeuroSpin, Batiment 145,
# 91191 Gif-sur-Yvette cedex
# 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.
from brainvisa.processes import *
from brainvisa.configuration import neuroConfig
from brainvisa.data.sqlFSODatabase import SQLDatabase
from soma.path import relative_path
from brainvisa.toolboxes import getToolbox
from brainvisa.data.fileSystemOntology import FileSystemOntology
import subprocess
signature = Signature(
'write_graphs', Boolean(),
#'htmlDirectory', WriteDiskItem( 'Any type', 'Directory' ),
)
def initialization( self ):
self.setOptional("write_graphs")
self.write_graphs=True
#self.addLink( 'htmlDirectory', 'ontology', lambda ontology: os.path.join( neuroConfig.mainDocPath, 'ontology-' + ontology ) )
#----------------------------------------------------------------------------
def generateHTMLDocumentation( processInfoOrId, translators, context, ontology ):
processInfo = getProcessInfo( processInfoOrId )
documentation = readProcdoc( processInfo.id )
if context is not None:
# this thing is a trick to allow text to be considered as
# HTML, and the
not escaped, otherwise either "
" appears in
# the process view window, or the log has no line breaks
context.write( 'Generate HTML for process "' + processInfo.name + '"
' )
# english translation is the default
den = documentation.get( 'en', {} )
pen = den.get( 'parameters', {} )
# Generate HTML documentations
for l in neuroConfig._languages:
tr = translators.get( l, None )
if not tr:
tr=neuroConfig.Translator(l)
d = documentation.get( l, {} )
htmlFileName = getHTMLFileName( processInfo.id, documentation, l )
p = os.path.dirname( htmlFileName )
if not os.path.isdir( p ):
os.makedirs( p )
# Main page
f = open( htmlFileName, 'w' )
print >> f, ''
print >> f, '
' short = d.get( 'short' ) if short: short = convertSpecialLinks( short, l, '', tr ) short = XHTML.html( short ) if not short and l != 'en': short = den.get( 'short' ) if short: short = convertSpecialLinks( short, l, '', tr ) short = XHTML.html( short ) print >> f, short print >> f, '' # Description long = d.get( 'long' ) if long: long = convertSpecialLinks( long, l, '', tr ) long = XHTML.html( long ) if not long and l != 'en': long = den.get( 'long' ) if long: long = convertSpecialLinks( long, l, '', tr ) long = XHTML.html( long ) if long: print >> f, '
' print >> f, long print >> f, '' signature = getProcessInstance( processInfo.id ).signature signature = signature.items() supportedFormats = [] if signature: # Parameters p = d.get( 'parameters', {} ) print >> f, '
' + i + ': ' + a_prefix + ti[0][1] + a_suffix f.write( ' ( ' ) if not j.mandatory: f.write( _t_( 'optional, ' ) ) try: if len( ti ) > 1: k, access = ti[ 1 ] else: access = _t_( 'input' ) f.write( access ) except context.UserInterruption: raise except Exception, e: print e f.write( ' )' ) print >> f, '' try: if len( ti ) > 2: supportedFormats.append( ( i, ti[2][1] ) ) except context.UserInterruption: raise except Exception, e: print e # Technical information print >> f, '' print >> f, descr print >> f, '
' print >> f, '' print >> f, '' f.close() #---------------------------------------------------------------------------- def generateHTMLProcessesDocumentation( context, ontology ): import sys #-------------------------------------- # Generate translators #-------------------------------------- translators = {} for l in neuroConfig._languages: translators[ l ] = neuroConfig.Translator( l ) #-------------------------------------- # Generate documentation for processes #-------------------------------------- for pi in allProcessesInfo(): try: generateHTMLDocumentation( pi, translators, context, ontology ) except context.UserInterruption: raise except ValidationError: pass except: context.showException( beforeError=_t_('Cannot generate documentation for %s') % (pi.fileName,) ) #--------------------------------------- # Generate documentation for categories #--------------------------------------- # Find all category_documentation.minf files in # the order of neuroConfig.processesPath categoryDocFiles = {} for procPath in neuroConfig.processesPath: stack = [ '' ] while stack: r = stack.pop() f = os.path.join( procPath, r ) if os.path.basename( r ) == 'category_documentation.minf': category = os.path.dirname(r).lower() if category: categoryDocFiles.setdefault( category, f ) elif os.path.isdir( f ): stack += [ os.path.join( r, c ) for c in os.listdir( f ) ] # Find documentation files in toolboxes # processes are in toolboxesDir/toolboxName/processes by default. anyway they are in toolbox.processesDir # each relative directory dir in processes, matches a category named toolboxName/dir # a documentation for the toolbox may be in toolboxesDir/toolboxId from brainvisa.toolboxes import allToolboxes for toolbox in allToolboxes(): # search for a file category_documentation.minf in toolboxesDir/toolboxId, otherwise it can be in processesDir # It is usefull for my processes toolbox because the toolbox and the processes are not in the same place and the documentation of the toolbox cannot be in the processes directory. toolboxDoc=os.path.join( neuroConfig.toolboxesDir, toolbox.id, "category_documentation.minf") if os.path.exists(toolboxDoc): # if it exists, add it to the doc file for which we have to generate an html file categoryDocFiles.setdefault( toolbox.id, toolboxDoc ) # search for category documentation files in processes directory stack = [('', toolbox.id)] # relative directory, category name while stack: r, cat = stack.pop() # get current relative path and associated category f=os.path.join(toolbox.processesDir, r) currentItem=os.path.basename( r ) if currentItem == 'category_documentation.minf': categoryDocFiles.setdefault( cat, f ) elif os.path.isdir( f ): if currentItem: cat=os.path.join(cat, currentItem.lower()) stack += [ (os.path.join( r, c ), cat) for c in os.listdir( f ) ] # Create category HTML files baseDocDir = os.path.dirname( neuroConfig.docPath ) for category, f in categoryDocFiles.iteritems(): categoryPath=category.split("/") minfContent = readMinf( f )[ 0 ] enContent=minfContent['en'] for l in neuroConfig._languages: #for l, c in minfContent.iteritems(): if l=='en': c=enContent else: c=minfContent.get(l, enContent) tr = translators.get( l ) c = convertSpecialLinks( c, l , '/'.join( ( '..', ) * (len( categoryPath )+1) ), tr ) # base dir for links : processes p = os.path.join( baseDocDir, l, 'processes', 'categories', category ) if not os.path.isdir( p ): os.makedirs( p ) f = open( os.path.join( p, 'category_documentation.html' ), 'w' ) print >> f, '' print >> f, '' print >> f, '' print >> f, '' print >> f, '' print >> f, '' print >> f, XHTML.html( c ) print >> f, '' f.close() #---------------------------------------------------------------------------- def get_link_to_documentation(item): item_name=item.name item_fileName = item_name.replace( '/', '_' ) item_escaped = htmlEscape( item_name ) return '' + item_escaped + '' + tr.translate( 'Toolbox' ) + ' : ' + unicode( tr.translate(get_toolbox_name(processInfo.toolbox) )) + '
' print >> f, '' + tr.translate( 'User level' ) + ' : ' + unicode( processInfo.userLevel ) + '
' print >> f, '' + tr.translate( 'Identifier' ) + ' :
' processFileRef=relative_path(processInfo.fileName, os.path.dirname(htmlFileName)) processFileName=relative_path(processInfo.fileName, os.path.dirname(neuroConfig.mainPath)) print >> f, '' + processInfo.id + '
' + tr.translate( 'File name' ) + ' :
' if supportedFormats: print >> f, '' + processFileName + '
' + tr.translate( 'Supported file formats' ) + ' : ' try: for parameter, formats in supportedFormats: print >> f, '
', parameter, ':' except context.UserInterruption: raise except Exception, e: print e print >> f, '' print >> f, '', formats, '', '
Generate HTML for language ', l, "
" ) # INDEX.HTML htmlDirectory=os.path.join( ontologyDirectory, l ) index = open( os.path.join( htmlDirectory, 'index.html' ), 'w' ) print >> index, '\n\nDefined in file : ' + typeFileName + '
' processes=sorted(processesByTypes.get( type, () ), key=nameKey) for pi in processes: try: href = htmlEscape( relative_path( getHTMLFileName( pi.id, language=l ), os.path.dirname( typeHTML.name ) ) ) print >> typeHTML, '' + htmlEscape( pi.name ) + '' print >> typeHTML, '
' except context.UserInterruption: raise except Exception, e: context.showException( beforeError=_t_( 'error in process doc for: ' )+' %s (%s)' \ % ( pi.id, pi.name ) ) print >> typeHTML, '
' for f in sorted(formatsByTypes.get( type, () ), key=str.lower ): formatFileName = f.replace( '/', '_' ) href = htmlEscape( relative_path( os.path.join( formatsDirectory, formatFileName + '.html' ), os.path.dirname( typeHTML.name ) ) ) print >> typeHTML, '' + htmlEscape( f ) + '' print >> typeHTML, '
' print >> typeHTML, '
" for rule in rules: print >> typeHTML, htmlEscape(rule.pattern.pattern), "' print >> typeHTML, '
" print >> typeHTML, '
" print >> typeHTML, htmlEscape(keys[0]) for key in keys[1:]: print >> typeHTML, ", ", htmlEscape(key) print >> typeHTML, '' print >> typeHTML, '' typeHTML.close() # FORMATS if not os.path.exists( formatsDirectory ): os.mkdir( formatsDirectory ) formats_toolboxes=open(os.path.join( formatsDirectory, 'index_toolboxes.html' ), 'w') print >> formats_toolboxes, '\n\n
' patterns="" for p in format.getPatterns().patterns: if patterns: patterns+=", "+p.pattern else: patterns=""+p.pattern+"" print >> formatHTML, patterns, "" formatFileRef=relative_path(format.fileName, os.path.dirname(htmlFileName)) formatFileName=relative_path(format.fileName, os.path.dirname(neuroConfig.mainPath)) print >> formatHTML, '
Defined in file: ' + formatFileName + '
' processes=sorted(processesByFormats.get( format.name, () ), key=nameKey) for pi in processes: try: href = htmlEscape( relative_path( getHTMLFileName( pi.id, language=l ), os.path.dirname( formatHTML.name ) ) ) print >> formatHTML, '' + htmlEscape( pi.name ) + '' print >> formatHTML, '
' except context.UserInterruption: raise except Exception, e: context.showException( beforeError=_t_( 'error in process doc for:' )+' %s (%s)' \ % ( pi.id, pi.name ) ) print >> formatHTML, '
' for t in sorted(typesByFormats.get( format.name, () ), key=str.lower): typeFileName = t.replace( '/', '_' ) href = htmlEscape( relative_path( os.path.join( typesDirectory, typeFileName + '.html' ), os.path.dirname( formatHTML.name ) ) ) print >> formatHTML, '' + htmlEscape( t ) + '' print >> formatHTML, '' formatHTML.close() # FORMATS LISTS print >> formats, '
' print >> formatHTML, '
' for f in format: fname=f.name.replace( "/", "_" ) print >> formatHTML, "", f.name, "" formatFileRef=relative_path(format.fileName, os.path.dirname(htmlFileName)) formatFileName=relative_path(format.fileName, os.path.dirname(neuroConfig.mainPath)) print >> formatHTML, '
" print >> formatHTML, "
Defined in file: ' + formatFileName + '
' processes=sorted(processesByFormats.get( format.name, () ), key=nameKey) for pi in processes: href = htmlEscape( relative_path( getHTMLFileName( pi.id, language=l ), os.path.dirname( formatHTML.name ) ) ) print >> formatHTML, '' + htmlEscape( pi.name ) + '' print >> formatHTML, '
' print >> formatHTML, '
' for t in sorted(typesByFormats.get( format.name, () ), key=str.lower): typeFileName = t.replace( '/', '_' ) href = htmlEscape( relative_path( os.path.join( typesDirectory, typeFileName + '.html' ), os.path.dirname( formatHTML.name ) ) ) print >> formatHTML, '' + htmlEscape( t ) + '' print >> formatHTML, '' formatHTML.close() print >> formats, '' formats.close() for database in databases: database.currentThreadCleanup()
' print >> formatHTML, '