Skip to content

Commit b009c6c

Browse files
committed
add macros
1 parent 12c5b67 commit b009c6c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

dataikuapi/dss/macro.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+

dataikuapi/dss/project.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os.path as osp
1111
from .future import DSSFuture
1212
from .notebook import DSSNotebook
13+
from .macro import DSSMacro
1314
from dataikuapi.utils import DataikuException
1415

1516

@@ -567,6 +568,36 @@ def set_tags(self, tags={}):
567568
return self.client._perform_empty("PUT", "/projects/%s/tags" % self.project_key, body = tags)
568569

569570

571+
########################################################
572+
# Macros
573+
########################################################
574+
575+
def list_macros(self, as_objects=False):
576+
"""
577+
List the macros accessible in this project
578+
579+
:param as_objects: if True, return the macros as :class:`dataikuapi.dss.macro.DSSMacro`
580+
macro handles instead of raw JSON
581+
:returns: the list of the macros
582+
"""
583+
macros = self.client._perform_json(
584+
"GET", "/projects/%s/runnables/" % self.project_key)
585+
if as_objects:
586+
return [DSSMacro(self.client, self.project_key, m["runnableType"], m) for m in macros]
587+
else:
588+
return macros
589+
590+
def get_macro(self, runnable_type):
591+
"""
592+
Get a handle to interact with a specific macro
593+
594+
:param runnable_type: the identifier of a macro
595+
:returns: A :class:`dataikuapi.dss.macro.DSSMacro` macro handle
596+
"""
597+
return DSSMacro(self.client, self.project_key, runnable_type)
598+
599+
600+
570601
class JobDefinitionBuilder(object):
571602
def __init__(self, project_key, job_type="NON_RECURSIVE_FORCED_BUILD"):
572603
"""

0 commit comments

Comments
 (0)