77from .apiservice import DSSAPIService
88import 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 set_code_env (self , code_env_name ):
26+ """Sets the name of the code env to use for this plugin"""
27+ self .settings ["codeEnvName" ] = code_env_name
28+
29+ def save (self ):
30+ """Saves the settings to DSS"""
31+ self .client ._perform_empty ("POST" , "/plugins/%s/settings" % (self .plugin_id ), body = self .settings )
32+
1033class DSSPlugin (object ):
1134 """
1235 A plugin on the DSS instance
@@ -16,65 +39,110 @@ def __init__(self, client, plugin_id):
1639 self .plugin_id = plugin_id
1740
1841 ########################################################
19- # plugin upload/update as zip
42+ # Settings
2043 ########################################################
2144
22- def upload (self , file_path ):
45+ def get_settings (self ):
46+ """Return the plugin-level settings
47+
48+ :return: a :class:`DSSPluginSettings`
2349 """
24- Upload the given file as a plugin
50+ settings = self .client ._perform_json ("GET" , "/plugins/%s/settings" % (self .plugin_id ))
51+ return DSSPluginSettings (self .client , self .plugin_id , settings )
2552
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
53+ ########################################################
54+ # Code env
55+ ########################################################
56+
57+ def create_code_env (self , python_interpreter = None , conda = False ):
2958 """
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
59+ Starts the creation of the code env of the plugin
3260
33- def update ( self , file_path ):
61+ :return: a :class:`dataikuapi.dssfuture.DSSFuture`
3462 """
35- Update the plugin with the given file
63+ ret = self .client ._perform_json ("POST" , "/plugins/%s/code-env/actions/create" % (self .plugin_id ), body = {
64+ "deploymentMode" : "PLUGIN_MANAGED" ,
65+ "conda" : conda ,
66+ "pythonInterpreter" : python_interpreter
67+ })
68+ return self .client .get_future (ret ["jobId" ])
3669
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
70+
71+ def update_code_env (self ):
72+ """
73+ Starts an update of the code env of the plugin
74+
75+ :return: a :class:`dataikuapi.dss.future.DSSFuture`
76+ """
77+ ret = self .client ._perform_json ("POST" , "/plugins/%s/code-env/actions/update" % (self .plugin_id ))
78+ return self .client .get_future (ret ["jobId" ])
79+
80+
81+ ########################################################
82+ # Plugin update
83+ ########################################################
84+
85+ def update_from_zip (self , fp ):
86+ """
87+ Updates the plugin from a plugin archive (as a file object)
88+
89+ :param object fp: A file-like object pointing to a plugin archive zip
90+ """
91+ files = {'file' : fp }
92+ self .client ._perform_json ("POST" , "/plugins/%s/actions/updateFromZip" % (self .plugin_id ), files = files )
93+
94+ def update_from_store (self ):
95+ """
96+ Updates the plugin from the Dataiku plugin store
97+
98+ :return: a :class:`~dataikuapi.dss.future.DSSFuture`
99+ """
100+ ret = self .client ._perform_json ("POST" , "/plugins/%s/actions/updateFromStore" % (self .plugin_id ))
101+ return self .client .get_future (ret ["jobId" ])
102+
103+ def update_from_git (self , repository_url , checkout = "master" , subpath = None ):
104+ """
105+ Updates the plugin from a Git repository. DSS must be setup to allow access to the repository.
106+
107+ :param str repository_url: URL of a Git remote
108+ :param str checkout: branch/tag/SHA1 to commit. For example "master"
109+ :param str subpath: Optional, path within the repository to use as plugin. Should contain a 'plugin.json' file
110+ :return: a :class:`~dataikuapi.dss.future.DSSFuture`
40111 """
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
112+ ret = self .client ._perform_json ("POST" , "/plugins/%s/actions/updateFromGit" % (self .plugin_id ), body = {
113+ "gitRepositoryUrl" : repository_url ,
114+ "gitCheckout" : checkout ,
115+ "gitSubpath" : subpath
116+ })
117+ return self .client .get_future (ret ["jobId" ])
43118
44119 ########################################################
45120 # Managing the dev plugin's contents
46121 ########################################################
47122
48123 def list_files (self ):
49124 """
50- Get the hierarchy of files in the plugin
51-
52- Returns:
53- the plugins's contents
125+ Get the hierarchy of files in the plugin (dev plugins only)
54126 """
55127 return self .client ._perform_json ("GET" , "/plugins/%s/contents" % (self .plugin_id ))
56128
57129 def get_file (self , path ):
58130 """
59- Get a file from the plugin folder
60-
61- Args:
62- path: the name of the file, from the root of the plugin
131+ Get a file from the plugin folder (dev plugins only)
63132
64- Returns:
65- the file's content, as a stream
133+ :param str path: the path of the file, relative to the root of the plugin
134+
135+ :return: a file-like object containing the file's content
66136 """
67137 return self .client ._perform_raw ("GET" , "/plugins/%s/contents/%s" % (self .plugin_id , path )).raw
68138
69139 def put_file (self , path , f ):
70140 """
71- Update a file in the plugin folder
141+ Update a file in the plugin folder (dev plugins only)
72142
73- Args:
74- f: the file contents, as a stream
75- path: the name of the file, from the root of the plugin
143+ :param file-like f: the file contents, as a file-like object
144+ :param str path: the path of the file, relative ot the root of the plugin
76145 """
77-
78146 file_name = path .split ('/' )[- 1 ]
79147 data = f .read () # eat it all, because making it work with a path variable and a MultifilePart in swing looks complicated
80148 return self .client ._perform_empty ("POST" , "/plugins/%s/contents/%s" % (self .plugin_id , path ), raw_body = data )
0 commit comments