# # Generates a master data registration script for a running openBIS server. # # To execute it, please run the ./export-master-data.sh # import sys import re VARNAME_PREFIXES = { "EXPERIMENT" : "exp_type_", "SAMPLE" : "samp_type_", "DATA_SET" : "data_set_type_", "MATERIAL" : "material_type_", "PROPERTY" : "prop_type_", "ASSIGNMENT" : "assignment_", "FILE_FORMAT" : "file_type_", "VOCABULARY" : "vocabulary_", "VOCABULARY_TERM" : "vocabulary_term_" } def getVarName(type, var): # remove minuses, dots and colons # they are not valid characters for Python variables normalized = re.sub("[\\-\\.:\\$]+", "", var) return VARNAME_PREFIXES[type] + normalized def strLiteral(var): if var: return "'" + var + "'" else: return 'None' def codeLiteral(code): normalized = code if code[0] == "$": normalized = code[1:] return strLiteral(normalized) def exportFileFormatType(fileType, out): var = getVarName("FILE_FORMAT", fileType.getCode()) code = codeLiteral(fileType.getCode()) description = strLiteral(fileType.getDescription()) snippet = """ %(var)s = tr.createNewFileFormatType(%(code)s) %(var)s.setDescription(%(description)s) """ % vars() out.write(snippet) def exportVocabulary(vocabulary, out): var = getVarName("VOCABULARY", vocabulary.getCode()) code = codeLiteral(vocabulary.getCode()) description = strLiteral(vocabulary.getDescription()) urlTemplate = strLiteral(vocabulary.getUrlTemplate()) isManagedInternally = vocabulary.isManagedInternally(); isInternalNamespace = vocabulary.isInternalNamespace(); isChosenFromList = vocabulary.isChosenFromList(); out.write(""" %(var)s = tr.createNewVocabulary(%(code)s) %(var)s.setDescription(%(description)s) %(var)s.setUrlTemplate(%(urlTemplate)s) %(var)s.setManagedInternally(%(isManagedInternally)s) %(var)s.setInternalNamespace(%(isInternalNamespace)s) %(var)s.setChosenFromList(%(isChosenFromList)s) """ % vars()) for term in vocabulary.getTerms(): term_var = getVarName("VOCABULARY_TERM", vocabulary.getCode() + "_" + term.getCode()) term_code = codeLiteral(term.getCode()) term_description = strLiteral(term.getDescription()) term_url = strLiteral(term.getUrl()) term_label = strLiteral(term.getLabel()) term_ordinal = term.getOrdinal() out.write(""" %(term_var)s = tr.createNewVocabularyTerm(%(term_code)s) %(term_var)s.setDescription(%(term_description)s) %(term_var)s.setUrl(%(term_url)s) %(term_var)s.setLabel(%(term_label)s) %(term_var)s.setOrdinal(%(term_ordinal)s) %(var)s.addTerm(%(term_var)s) """ % vars()) def exportExperimentType(experimentType, out): var = getVarName("EXPERIMENT", experimentType.getCode()) code = codeLiteral(experimentType.getCode()) description = strLiteral(experimentType.getDescription()) snippet = """ %(var)s = tr.createNewExperimentType(%(code)s) %(var)s.setDescription(%(description)s) """ % vars() out.write(snippet) def exportSampleType(sampleType, out): var = getVarName("SAMPLE", sampleType.getCode()) code = codeLiteral(sampleType.getCode()) description = strLiteral(sampleType.getDescription()) listable = sampleType.isListable() isSubcodeUnique = sampleType.isSubcodeUnique() isAutoGeneratedCode = sampleType.isAutoGeneratedCode() generatedCodePrefix = strLiteral(sampleType.getGeneratedCodePrefix()) snippet = """ %(var)s = tr.createNewSampleType(%(code)s) %(var)s.setDescription(%(description)s) %(var)s.setListable(%(listable)s) %(var)s.setSubcodeUnique(%(isSubcodeUnique)s) %(var)s.setAutoGeneratedCode(%(isAutoGeneratedCode)s) %(var)s.setGeneratedCodePrefix(%(generatedCodePrefix)s) """ % vars() out.write(snippet) def exportDataSetType(dataSetType, out): var = getVarName("DATA_SET", dataSetType.getCode()) code = codeLiteral(dataSetType.getCode()) description = strLiteral(dataSetType.getDescription()) isContainerType = dataSetType.isContainerType() snippet = """ %(var)s = tr.createNewDataSetType(%(code)s) %(var)s.setDescription(%(description)s) %(var)s.setContainerType(%(isContainerType)s) """ % vars() out.write(snippet) def exportMaterialType(materialType, out): var = getVarName("MATERIAL", materialType.getCode()) code = codeLiteral(materialType.getCode()) description = strLiteral(materialType.getDescription()) snippet = """ %(var)s = tr.createNewMaterialType(%(code)s) %(var)s.setDescription(%(description)s) """ % vars() out.write(snippet) def exportPropertyType(propertyType, out): var = getVarName("PROPERTY", propertyType.getCode()) code = codeLiteral(propertyType.getCode()) label = strLiteral(propertyType.getLabel()) dataType = propertyType.getDataType().name() specialSetters = "" if propertyType.getMaterialType(): materialVar = getVarName("MATERIAL", propertyType.getMaterialType().getCode()) specialSetters = "%(var)s.setMaterialType(%(materialVar)s)" % vars() if propertyType.getVocabulary(): vocabularyVar = getVarName("VOCABULARY", propertyType.getVocabulary().getCode()) specialSetters = specialSetters + "\n%(var)s.setVocabulary(%(vocabularyVar)s)" % vars() isManagedInternally = propertyType.isManagedInternally() isInternalNamespace = propertyType.isInternalNamespace() snippet = """ %(var)s = tr.createNewPropertyType(%(code)s, DataType.%(dataType)s) %(var)s.setLabel(%(label)s) %(var)s.setManagedInternally(%(isManagedInternally)s) %(var)s.setInternalNamespace(%(isInternalNamespace)s) %(specialSetters)s """ % vars() out.write(snippet) def exportAssignment(assignment, out): var = getVarName("ASSIGNMENT", assignment.getEntityKind().name() + "_" + assignment.getEntityTypeCode()+ "_" + assignment.getPropertyTypeCode()) entityVar = getVarName(assignment.getEntityKind().name(), assignment.getEntityTypeCode()) propertyVar = getVarName("PROPERTY", assignment.getPropertyTypeCode()) isMandatory = assignment.isMandatory() section = strLiteral(assignment.getSection()) posInForms = assignment.getPositionInForms() snippet = """ %(var)s = tr.assignPropertyType(%(entityVar)s, %(propertyVar)s) %(var)s.setMandatory(%(isMandatory)s) %(var)s.setSection(%(section)s) %(var)s.setPositionInForms(%(posInForms)s) """ % vars() out.write(snippet) out = sys.stdout print "# " print "# Exporting master data to ", out.name, "..." print "# " tr = service.transaction() out.write(""" import ch.systemsx.cisd.openbis.generic.server.jython.api.v1.DataType as DataType tr = service.transaction() """) for fileType in tr.listFileFormatTypes(): exportFileFormatType(fileType, out) for vocabulary in tr.listVocabularies(): exportVocabulary(vocabulary, out) for expType in tr.listExperimentTypes(): exportExperimentType(expType, out) for sampleType in tr.listSampleTypes(): exportSampleType(sampleType, out) for dataSetType in tr.listDataSetTypes(): exportDataSetType(dataSetType, out) for materialType in tr.listMaterialTypes(): exportMaterialType(materialType, out) for propertyType in tr.listPropertyTypes(): exportPropertyType(propertyType, out) for assignment in tr.listPropertyAssignments(): exportAssignment(assignment, out) out.close()