Skip to content

Commit ba17b8f

Browse files
committed
Implement part of new plugins API in Python client
1 parent c9ddd5d commit ba17b8f

File tree

2 files changed

+132
-31
lines changed

2 files changed

+132
-31
lines changed

dataikuapi/dss/plugin.py

Lines changed: 93 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
from .apiservice import DSSAPIService
88
import sys
99

10+
class DSSPluginSettings(object):
11+
"""
12+
The settings of a plugin.
13+
"""
14+
15+
def __init__(self, client, plugin_id, settings):
16+
"""Do not call this directly, use :meth:`DSSPlugin.get_settings`"""
17+
self.client = client
18+
self.plugin_id = plugin_id
19+
self.settings = settings
20+
21+
def get_raw(self):
22+
"""Returns the raw settings object"""
23+
return self.settings
24+
25+
def save(self):
26+
"""Saves the settings to DSS"""
27+
self.client._perform_empty("POST", "/plugins/%s/settings" % (self.plugin_id), body=self.settings)
28+
1029
class DSSPlugin(object):
1130
"""
1231
A plugin on the DSS instance
@@ -16,65 +35,109 @@ def __init__(self, client, plugin_id):
1635
self.plugin_id = plugin_id
1736

1837
########################################################
19-
# plugin upload/update as zip
38+
# Settings
2039
########################################################
2140

22-
def upload(self, file_path):
41+
def get_settings(self):
42+
"""Return the plugin-level settings
43+
44+
:return: a :class:`DSSPluginSettings`
2345
"""
24-
Upload the given file as a plugin
46+
settings = self.client._perform_json("GET", "/plugins/%s/settings" % (self.plugin_id))
47+
return DSSPluginSettings(self.client, self.plugin_id, settings)
2548

26-
Note: this call requires an API key with admin rights
27-
28-
:param: file_path : the path to the zip file of the plugin
49+
########################################################
50+
# Code env
51+
########################################################
52+
53+
def create_code_env(self, python_interpreter=None, conda=False):
2954
"""
30-
with open(file_path, 'rb') as f:
31-
return self.client._perform_json_upload("POST", "/plugins/%s/upload" % (self.plugin_id), 'plugin.zip', f).text
55+
Starts the creation of the code env of the plugin
3256
33-
def update(self, file_path):
57+
:return: a :class:`dataikuapi.dssfuture.DSSFuture`
3458
"""
35-
Update the plugin with the given file
59+
ret = self.client._perform_json("POST", "/plugins/%s/code-env/actions/create" % (self.plugin_id), body={
60+
"conda": conda,
61+
"pythonInterpreter": python_interpreter
62+
})
63+
return self.client.get_future(ret["jobId"])
3664

37-
Note: this call requires an API key with admin rights
38-
39-
:param: file_path : the path to the zip file of the plugin
65+
66+
def update_code_env(self):
4067
"""
41-
with open(file_path, 'rb') as f:
42-
return self.client._perform_json_upload("POST", "/plugins/%s/update" % (self.plugin_id), 'plugin.zip', f).text
68+
Starts an update of the code env of the plugin
69+
70+
:return: a :class:`dataikuapi.dss.future.DSSFuture`
71+
"""
72+
ret = self.client._perform_json("POST", "/plugins/%s/code-env/actions/update" % (self.plugin_id))
73+
return self.client.get_future(ret["jobId"])
74+
75+
76+
########################################################
77+
# Plugin update
78+
########################################################
79+
80+
def update_from_zip(self, fp):
81+
"""
82+
Updates the plugin from a plugin archive (as a file object)
83+
84+
:param object fp: A file-like object pointing to a plugin archive zip
85+
"""
86+
files = {'file': fp }
87+
self.client._perform_json("POST", "/plugins/%s/actions/updateFromZip" % (self.plugin_id), files=files)
88+
89+
def update_from_store(self):
90+
"""
91+
Updates the plugin from the Dataiku plugin store
92+
93+
:return: a :class:`~dataikuapi.dss.future.DSSFuture`
94+
"""
95+
ret = self.client._perform_json("POST", "/plugins/%s/actions/updateFromStore" % (self.plugin_id))
96+
return self.client.get_future(ret["jobId"])
97+
98+
def update_from_git(self, repository_url, checkout = "master", subpath=None):
99+
"""
100+
Updates the plugin from a Git repository. DSS must be setup to allow access to the repository.
101+
102+
:param str repository_url: URL of a Git remote
103+
:param str checkout: branch/tag/SHA1 to commit. For example "master"
104+
:param str subpath: Optional, path within the repository to use as plugin. Should contain a 'plugin.json' file
105+
:return: a :class:`~dataikuapi.dss.future.DSSFuture`
106+
"""
107+
ret = self.client._perform_json("POST", "/plugins/%s/actions/updateFromGit" % (self.plugin_id), body={
108+
"gitRepositoryUrl": repository_url,
109+
"gitCheckout" : checkout,
110+
"gitSubpath": subpath
111+
})
112+
return self.client.get_future(ret["jobId"])
43113

44114
########################################################
45115
# Managing the dev plugin's contents
46116
########################################################
47117

48118
def list_files(self):
49119
"""
50-
Get the hierarchy of files in the plugin
51-
52-
Returns:
53-
the plugins's contents
120+
Get the hierarchy of files in the plugin (dev plugins only)
54121
"""
55122
return self.client._perform_json("GET", "/plugins/%s/contents" % (self.plugin_id))
56123

57124
def get_file(self, path):
58125
"""
59-
Get a file from the plugin folder
60-
61-
Args:
62-
path: the name of the file, from the root of the plugin
126+
Get a file from the plugin folder (dev plugins only)
63127
64-
Returns:
65-
the file's content, as a stream
128+
:param str path: the path of the file, relative to the root of the plugin
129+
130+
:return: a file-like object containing the file's content
66131
"""
67132
return self.client._perform_raw("GET", "/plugins/%s/contents/%s" % (self.plugin_id, path)).raw
68133

69134
def put_file(self, path, f):
70135
"""
71-
Update a file in the plugin folder
136+
Update a file in the plugin folder (dev plugins only)
72137
73-
Args:
74-
f: the file contents, as a stream
75-
path: the name of the file, from the root of the plugin
138+
:param file-like f: the file contents, as a file-like object
139+
:param str path: the path of the file, relative ot the root of the plugin
76140
"""
77-
78141
file_name = path.split('/')[-1]
79142
data = f.read() # eat it all, because making it work with a path variable and a MultifilePart in swing looks complicated
80143
return self.client._perform_empty("POST", "/plugins/%s/contents/%s" % (self.plugin_id, path), raw_body=data)

dataikuapi/dssclient.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,47 @@ def list_plugins(self):
169169
"""
170170
return self._perform_json("GET", "/plugins/")
171171

172+
def install_plugin_from_archive(self, fp):
173+
"""
174+
Install a plugin from a plugin archive (as a file object)
175+
176+
:param object fp: A file-like object pointing to a plugin archive zip
177+
"""
178+
files = {'file': fp }
179+
self._perform_json("POST", "/plugins/actions/installFromZip", files=files)
180+
181+
def install_plugin_from_store(self, plugin_id):
182+
"""
183+
Install a plugin from the Dataiku plugin store
184+
185+
:param str plugin_id: identifier of the plugin to install
186+
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the install process
187+
"""
188+
f = self._perform_json("POST", "/plugins/actions/installFromStore", body={
189+
"pluginId": plugin_id
190+
})
191+
print(f)
192+
return DSSFuture(self, f["jobId"])
193+
194+
def install_plugin_from_git(self, repository_url, checkout = "master", subpath=None):
195+
"""
196+
Install a plugin from a Git repository. DSS must be setup to allow access to the repository.
197+
198+
:param str repository_url: URL of a Git remote
199+
:param str checkout: branch/tag/SHA1 to commit. For example "master"
200+
:param str subpath: Optional, path within the repository to use as plugin. Should contain a 'plugin.json' file
201+
:return: A :class:`~dataikuapi.dss.future.DSSFuture` representing the install process
202+
"""
203+
f = self._perform_json("POST", "/plugins/actions/installFromGit", body={
204+
"gitRepositoryUrl": repository_url,
205+
"gitCheckout" : checkout,
206+
"gitSubpath": subpath
207+
})
208+
return DSSFuture(self, f["jobId"])
209+
172210
def get_plugin(self, plugin_id):
173211
"""
174-
Get a handle to interact with a specific plugin (plugin in "development" mode only).
212+
Get a handle to interact with a specific plugin
175213
176214
:param str plugin_id: the identifier of the desired plugin
177215
:returns: A :class:`dataikuapi.dss.project.DSSPlugin`

0 commit comments

Comments
 (0)