|
| 1 | +import time |
| 2 | +import sys, json |
| 3 | +from dataikuapi.utils import DataikuException |
| 4 | + |
| 5 | +class DSSMacro(object): |
| 6 | + """ |
| 7 | + A macro on the DSS instance |
| 8 | +
|
| 9 | + :param client: an api client to connect to the DSS backend |
| 10 | + :param project_key: identifier of the project to access the macro from |
| 11 | + :param runnable_type: identifier of the macro |
| 12 | + :param definition: if available, the macro's definition |
| 13 | + """ |
| 14 | + def __init__(self, client, project_key, runnable_type, definition=None): |
| 15 | + self.client = client |
| 16 | + self.runnable_type = runnable_type |
| 17 | + self.project_key = project_key |
| 18 | + self.definition = definition |
| 19 | + |
| 20 | + def get_definition(self): |
| 21 | + """ |
| 22 | + Get the macro definition. The result contains : |
| 23 | +
|
| 24 | + * identification of the macro |
| 25 | + * display info like label |
| 26 | + * what kind of result the macro returns (html, file, url, ...) |
| 27 | + * the list of parameters to pass in order to start a run (adminParams is |
| 28 | + empty unless the authentication of the api client covers admin rights) |
| 29 | + """ |
| 30 | + if self.definition is None: |
| 31 | + self.definition = self.client._perform_json( |
| 32 | + "GET", "/projects/%s/runnables/%s" % (self.project_key, self.runnable_type)) |
| 33 | + return self.definition |
| 34 | + |
| 35 | + |
| 36 | + def run(self, params={}, admin_params={}, wait=True): |
| 37 | + """ |
| 38 | + Run the macro from the project |
| 39 | +
|
| 40 | + :param params: parameters to the macro run |
| 41 | + :param admin_params: admin parameters to the macro run (if the authentication of |
| 42 | + the api client does not cover admin rights, they are ignored) |
| 43 | + :param wait: if True, the call blocks until the run is finished |
| 44 | + :returns: a run identifier to use to abort or retrieve results |
| 45 | + """ |
| 46 | + return self.client._perform_json( |
| 47 | + "POST", "/projects/%s/runnables/%s" % (self.project_key, self.runnable_type), |
| 48 | + params={'wait':wait}, body={'params':params, 'adminParams':admin_params})['runId'] |
| 49 | + |
| 50 | + def abort(self, run_id): |
| 51 | + """ |
| 52 | + Aborts a run of the macro |
| 53 | +
|
| 54 | + :param run_id: a run identifier, as return from the run() method |
| 55 | + """ |
| 56 | + self.client._perform_empty( |
| 57 | + "POST", "/projects/%s/runnables/%s/abort/%s" % (self.project_key, self.runnable_type, run_id)) |
| 58 | + |
| 59 | + def get_status(self, run_id): |
| 60 | + """ |
| 61 | + Polls the status of a run of the macro |
| 62 | + |
| 63 | + :param run_id: a run identifier, as return from the run() method |
| 64 | + :returns: a dict holding information about whether the run exists, is still running, |
| 65 | + errors that might have happened during the run, and result type if it finished. |
| 66 | + """ |
| 67 | + return self.client._perform_json( |
| 68 | + "GET", "/projects/%s/runnables/%s/state/%s" % (self.project_key, self.runnable_type, run_id)) |
| 69 | + |
| 70 | + def get_result(self, run_id, as_type=None): |
| 71 | + """ |
| 72 | + Retrieve the result of a run of the macro |
| 73 | + |
| 74 | + :param run_id: a run identifier, as return from the run() method |
| 75 | + :param as_type: if not None, one of 'string' or 'json' |
| 76 | + :returns: the result of the macro run, as a file-like is as_type is None; as a str if |
| 77 | + as_type is "string"; as an object if as_type is "json". If the macro is still |
| 78 | + running, an Exception is raised |
| 79 | + """ |
| 80 | + resp = self.client._perform_raw( |
| 81 | + "GET", "/projects/%s/runnables/%s/result/%s" % (self.project_key, self.runnable_type, run_id)) |
| 82 | + if as_type == 'string': |
| 83 | + with resp.raw as s: |
| 84 | + return s.read() |
| 85 | + elif as_type == 'json': |
| 86 | + with resp.raw as s: |
| 87 | + return json.load(s) |
| 88 | + else: |
| 89 | + return resp.raw |
| 90 | + |
| 91 | + |
0 commit comments