<s> from __future__ import print_function <EOL> import fileErrors as errors <EOL> import stringOperations <EOL> import json <EOL> import os <EOL> import sys <EOL> class fileHandling : <EOL> def __init__ ( self , toolsPath , pipelinesPath , definedPath ) : <EOL> self . errors = errors . fileErrors ( ) <EOL> self . userConfigurationFiles = self . getValidJsonFiles ( os . listdir ( definedPath ) ) if definedPath else [ ] <EOL> self . pipelines = self . getValidJsonFiles ( os . listdir ( pipelinesPath ) ) <EOL> self . tools = self . getValidJsonFiles ( os . listdir ( toolsPath ) ) <EOL> def checkPipeline ( self , toolsPath , pipelinesPath , definedPath , pipeline ) : <EOL> if pipeline in self . userConfigurationFiles : return definedPath + '<STR_LIT:/>' + pipeline + '<STR_LIT>' <EOL> elif pipeline in self . pipelines : return pipelinesPath + '<STR_LIT:/>' + pipeline + '<STR_LIT>' <EOL> else : <EOL> rankedPipelines = stringOperations . rankListByString ( self . pipelines , pipeline ) <EOL> self . errors . invalidPipelineName ( rankedPipelines , pipeline ) <EOL> def checkFileExistence ( self , fileList , resourcesPath , toolsPath ) : <EOL> missingFiles = [ ] <EOL> for filename in fileList : <EOL> if filename . startswith ( '<STR_LIT>' ) : updatedFilename = filename . replace ( '<STR_LIT>' , '<STR_LIT:.>' ) <EOL> elif filename . startswith ( '<STR_LIT>' ) : updatedFilename = filename . replace ( '<STR_LIT>' , resourcesPath ) <EOL> elif filename . startswith ( '<STR_LIT>' ) : updatedFilename = filename . replace ( '<STR_LIT>' , toolsPath ) <EOL> else : updatedFilename = filename <EOL> if not os . path . exists ( updatedFilename ) : missingFiles . append ( filename ) <EOL> if missingFiles : <EOL> self . errors . missingFiles ( missingFiles ) <EOL> return False <EOL> else : return True <EOL> def getRandomString ( self , pipeline ) : <EOL> files = [ ] <EOL> for filename in os . listdir ( "<STR_LIT>" ) : <EOL> if filename . startswith ( pipeline ) and filename . endswith ( '<STR_LIT>' ) : files . append ( filename ) <EOL> if len ( files ) != <NUM_LIT:1> : return False <EOL> randomString = files [ <NUM_LIT:0> ] . replace ( pipeline + '<STR_LIT:.>' , '<STR_LIT>' ) <EOL> return randomString . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def getValidJsonFiles ( files ) : <EOL> validFiles = [ ] <EOL> for filename in files : <EOL> if filename . endswith ( '<STR_LIT>' ) and not filename . endswith ( '<STR_LIT>' ) : validFiles . append ( filename . rstrip ( '<STR_LIT>' ) [ : - <NUM_LIT:1> ] ) <EOL> return validFiles <EOL> @ staticmethod <EOL> def openFileForReading ( filename ) : <EOL> try : return open ( filename ) <EOL> except : return False <EOL> @ staticmethod <EOL> def readConfigurationFile ( filename , allowTermination = True ) : <EOL> try : jsonData = open ( filename ) <EOL> except : <EOL> if allowTermination : errors . fileErrors ( ) . noFile ( filename ) <EOL> else : return False <EOL> try : data = json . load ( jsonData ) <EOL> except : <EOL> if allowTermination : errors . fileErrors ( ) . notJson ( filename , sys . exc_info ) <EOL> else : return False <EOL> return data <EOL> @ staticmethod <EOL> def openFileForWriting ( filename ) : <EOL> return open ( filename , '<STR_LIT:w>' ) <EOL> @ staticmethod <EOL> def closeFile ( filehandle ) : <EOL> filehandle . close ( ) </s>
