Skip to content
Snippets Groups Projects
Forked from DM / dm-docs
261 commits behind, 854 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
addExperimentFileCli.py 1.94 KiB
#!/usr/bin/env python

from dm.cat_web_service.api.fileCatApi import FileCatApi
from dm.common.exceptions.invalidRequest import InvalidRequest
from catWebServiceSessionCli import CatWebServiceSessionCli

class AddExperimentFileCli(CatWebServiceSessionCli):
    def __init__(self):
        CatWebServiceSessionCli.__init__(self, validArgCount=self.ANY_NUMBER_OF_POSITIONAL_ARGS)
        self.addOption('', '--file', dest='experimentFilePath', help='Experiment file path.')
        self.addOption('', '--experiment', dest='experimentName', help='Experiment name.')

    def checkArgs(self):
        if self.options.experimentName is None:
            raise InvalidRequest('Experiment name must be provided.')
        if self.options.experimentFilePath is None:
            raise InvalidRequest('Experiment file path must be provided.')

    def getExperimentName(self):
        return self.options.experimentName

    def getExperimentFilePath(self):
        return self.options.experimentFilePath

    def runCommand(self):
        self.parseArgs(usage="""
    dm-add-experiment-file --file=EXPERIMENTFILEPATH --experiment=EXPERIMENTNAME 
        [key1:value1, key2:value2, ...]

Description:
    Adds experiment file to the metadata catalog. All provided key/value pairs 
    are interpreted as file metadata.
        """)
        self.checkArgs()
        api = FileCatApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
        fileInfo = self.splitArgsIntoDict()
        fileInfo['experimentName'] = self.getExperimentName()
        fileInfo['experimentFilePath'] = self.getExperimentFilePath()
        fileMetadata = api.addExperimentFile(fileInfo)
        print fileMetadata.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())

#######################################################################
# Run command.
if __name__ == '__main__':
    cli = AddExperimentFileCli()
    cli.run()