<s> from cryptography . fernet import Fernet <EOL> from girder . utility import config <EOL> from girder . utility . model_importer import ModelImporter <EOL> from girder . plugins . minerva . constants import PluginSettings <EOL> def findNamedFolder ( currentUser , user , parent , parentType , name , create = False , <EOL> public = False ) : <EOL> folders = [ ModelImporter . model ( '<STR_LIT>' ) . filter ( folder , currentUser ) for folder in <EOL> ModelImporter . model ( '<STR_LIT>' ) . childFolders ( parent = parent , <EOL> parentType = parentType , user = currentUser , filters = { '<STR_LIT:name>' : name } ) ] <EOL> if len ( folders ) == <NUM_LIT:0> : <EOL> if create and currentUser : <EOL> folder = ModelImporter . model ( '<STR_LIT>' ) . createFolder ( <EOL> parent , name , parentType = parentType , public = public , <EOL> creator = currentUser ) <EOL> return folder <EOL> else : <EOL> return None <EOL> else : <EOL> return folders [ <NUM_LIT:0> ] <EOL> def findMinervaFolder ( currentUser , user , create = False ) : <EOL> return findNamedFolder ( currentUser , user , user , '<STR_LIT:user>' , <EOL> PluginSettings . MINERVA_FOLDER , create ) <EOL> def findDatasetFolder ( currentUser , user , create = False ) : <EOL> minervaFolder = findMinervaFolder ( currentUser , user , create ) <EOL> if minervaFolder is None : <EOL> return minervaFolder <EOL> else : <EOL> return findNamedFolder ( currentUser , user , minervaFolder , '<STR_LIT>' , <EOL> PluginSettings . DATASET_FOLDER , create ) <EOL> def findSourceFolder ( currentUser , user , create = False ) : <EOL> minervaFolder = findMinervaFolder ( currentUser , user , create ) <EOL> if minervaFolder is None : <EOL> return minervaFolder <EOL> else : <EOL> return findNamedFolder ( currentUser , user , minervaFolder , '<STR_LIT>' , <EOL> PluginSettings . SOURCE_FOLDER , create ) <EOL> def findSessionFolder ( currentUser , user , create = False ) : <EOL> minervaFolder = findMinervaFolder ( currentUser , user , create ) <EOL> if minervaFolder is None : <EOL> return minervaFolder <EOL> else : <EOL> return findNamedFolder ( currentUser , user , minervaFolder , '<STR_LIT>' , <EOL> PluginSettings . SESSION_FOLDER , create ) <EOL> def findNamedCollection ( currentUser , name , create = False ) : <EOL> collections = [ ModelImporter . model ( '<STR_LIT>' ) . filter ( c , currentUser ) for c in <EOL> ModelImporter . model ( '<STR_LIT>' ) . textSearch ( name , user = currentUser ) ] <EOL> if len ( collections ) == <NUM_LIT:0> : <EOL> if create : <EOL> return ModelImporter . model ( '<STR_LIT>' ) . createCollection ( <EOL> name , description = '<STR_LIT>' , public = True , creator = currentUser ) <EOL> else : <EOL> return None <EOL> else : <EOL> return collections [ <NUM_LIT:0> ] <EOL> def findMinervaCollection ( currentUser , create = False ) : <EOL> return findNamedCollection ( currentUser , PluginSettings . MINERVA_COLLECTION , <EOL> create ) <EOL> def findAnalysisFolder ( currentUser , create = False ) : <EOL> minervaCollection = findMinervaCollection ( currentUser , create ) <EOL> if minervaCollection is None : <EOL> return None <EOL> else : <EOL> analysisFolder = findNamedFolder ( currentUser , currentUser , <EOL> minervaCollection , '<STR_LIT>' , <EOL> '<STR_LIT>' , create , public = True ) <EOL> return analysisFolder <EOL> def findAnalysisByName ( currentUser , name ) : <EOL> analysisFolder = findAnalysisFolder ( currentUser ) <EOL> filters = { } <EOL> filters [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : name <EOL> } <EOL> analyses = [ ModelImporter . model ( '<STR_LIT>' ) . filter ( item , currentUser ) <EOL> for item in <EOL> ModelImporter . model ( '<STR_LIT>' ) . childItems ( folder = analysisFolder , <EOL> filters = filters ) ] <EOL> if len ( analyses ) > <NUM_LIT:0> : <EOL> return analyses [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def mM ( item , minerva_metadata = None ) : <EOL> if minerva_metadata is None : <EOL> if '<STR_LIT>' not in item or '<STR_LIT>' not in item [ '<STR_LIT>' ] : <EOL> return { } <EOL> return item [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> else : <EOL> return updateMinervaMetadata ( item , minerva_metadata ) <EOL> def updateMinervaMetadata ( item , minerva_metadata ) : <EOL> if '<STR_LIT>' not in item : <EOL> item [ '<STR_LIT>' ] = { } <EOL> item [ '<STR_LIT>' ] [ '<STR_LIT>' ] = minerva_metadata <EOL> ModelImporter . model ( '<STR_LIT>' ) . setMetadata ( item , item [ '<STR_LIT>' ] ) <EOL> return item [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def decryptCredentials ( credentials ) : <EOL> cur_config = config . getConfig ( ) <EOL> key = cur_config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> f = Fernet ( key ) <EOL> return f . decrypt ( bytes ( credentials ) ) <EOL> def encryptCredentials ( credentials ) : <EOL> cur_config = config . getConfig ( ) <EOL> key = cur_config [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> f = Fernet ( key ) <EOL> return f . encrypt ( bytes ( credentials ) ) <EOL> def jobMM ( job , minerva_metadata = None , save = True ) : <EOL> if minerva_metadata is None : <EOL> if '<STR_LIT>' not in job or '<STR_LIT>' not in job [ '<STR_LIT>' ] : <EOL> return { } <EOL> return job [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> else : <EOL> if '<STR_LIT>' not in job : <EOL> job [ '<STR_LIT>' ] = { } <EOL> job [ '<STR_LIT>' ] [ '<STR_LIT>' ] = minerva_metadata <EOL> if save : <EOL> ModelImporter . model ( '<STR_LIT>' , '<STR_LIT>' ) . save ( job ) <EOL> return job [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def addJobOutput ( job , output , output_type = '<STR_LIT>' , save = True ) : <EOL> mm = jobMM ( job ) <EOL> outputs = mm . get ( '<STR_LIT>' , [ ] ) <EOL> job_output = None <EOL> if output_type == '<STR_LIT>' : <EOL> job_output = { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : output . get ( '<STR_LIT>' ) <EOL> } <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' % output_type ) <EOL> outputs . append ( job_output ) <EOL> mm [ '<STR_LIT>' ] = outputs <EOL> jobMM ( job , mm , save ) </s>
