|
| 1 | +from ..utils import DataikuException |
| 2 | +import json |
| 3 | +from datetime import datetime |
| 4 | +from .future import DSSFuture |
| 5 | + |
| 6 | +class DSSCodeStudioObjectListItem(object): |
| 7 | + """An item in a list of code studios. Do not instantiate this class, use :meth:`dataikuapi.dss.project.DSSProject.list_code_studios`""" |
| 8 | + def __init__(self, client, project_key, data): |
| 9 | + self.client = client |
| 10 | + self.project_key = project_key |
| 11 | + self._data = data |
| 12 | + |
| 13 | + def to_code_studio(self): |
| 14 | + """Gets the :class:`DSSCodeStudioObject` corresponding to this code studio object """ |
| 15 | + return DSSCodeStudioObject(self.client, self.project_key, self._data["id"]) |
| 16 | + |
| 17 | + @property |
| 18 | + def name(self): |
| 19 | + return self._data["name"] |
| 20 | + @property |
| 21 | + def id(self): |
| 22 | + return self._data["id"] |
| 23 | + @property |
| 24 | + def owner(self): |
| 25 | + return self._data["owner"] |
| 26 | + @property |
| 27 | + def template_id(self): |
| 28 | + return self._data["templateId"] |
| 29 | + @property |
| 30 | + def template_label(self): |
| 31 | + return self._data.get('desc', {}).get('label', self._data['id']) |
| 32 | + @property |
| 33 | + def template_description(self): |
| 34 | + return self._data.get('desc', {}).get('shortDesc', '') |
| 35 | + |
| 36 | + |
| 37 | +class DSSCodeStudioObject(object): |
| 38 | + """ |
| 39 | + A handle to manage a code studio object of a project |
| 40 | + """ |
| 41 | + def __init__(self, client, project_key, code_studio_id): |
| 42 | + """Do not call directly, use :meth:`dataikuapi.dss.project.DSSProject.get_code_studio`""" |
| 43 | + self.client = client |
| 44 | + self.project_key = project_key |
| 45 | + self.code_studio_id = code_studio_id |
| 46 | + |
| 47 | + def delete(self): |
| 48 | + """ |
| 49 | + Delete the code studio |
| 50 | + """ |
| 51 | + self.client._perform_empty("DELETE", "/projects/%s/code-studios/%s" % (self.project_key, self.code_studio_id)) |
| 52 | + |
| 53 | + def get_settings(self): |
| 54 | + """ |
| 55 | + Get the code studio object's definition |
| 56 | +
|
| 57 | + :returns: a handle to manage the code studio definition |
| 58 | + :rtype: :class:`dataikuapi.dss.codestudio.DSSCodeStudioObjectSettings` |
| 59 | + """ |
| 60 | + settings = self.client._perform_json("GET", "/projects/%s/code-studios/%s" % (self.project_key, self.code_studio_id)) |
| 61 | + return DSSCodeStudioObjectSettings(self.client, self.project_key, self.code_studio_id, settings) |
| 62 | + |
| 63 | + def get_status(self): |
| 64 | + """ |
| 65 | + Get the code studio object's state |
| 66 | +
|
| 67 | + :returns: a handle to inspect the code studio state |
| 68 | + :rtype: :class:`dataikuapi.dss.codestudio.DSSCodeStudioObjectStatus` |
| 69 | + """ |
| 70 | + status = self.client._perform_json("GET", "/projects/%s/code-studios/%s/status" % (self.project_key, self.code_studio_id)) |
| 71 | + return DSSCodeStudioObjectStatus(self.client, self.project_key, self.code_studio_id, status) |
| 72 | + |
| 73 | + def stop(self): |
| 74 | + """ |
| 75 | + Stop a running code studio |
| 76 | +
|
| 77 | + :returns: a future to wait on the stop, or None if already stopped |
| 78 | + :rtype: :class:`dataikuapi.dss.future.DSSFuture` |
| 79 | + """ |
| 80 | + ret = self.client._perform_json("POST", "/projects/%s/code-studios/%s/stop" % (self.project_key, self.code_studio_id)) |
| 81 | + return DSSFuture.from_resp(self.client, ret) |
| 82 | + |
| 83 | + def restart(self): |
| 84 | + """ |
| 85 | + Restart a code studio |
| 86 | +
|
| 87 | + :returns: a future to wait on the start |
| 88 | + :rtype: :class:`dataikuapi.dss.future.DSSFuture` |
| 89 | + """ |
| 90 | + ret = self.client._perform_json("POST", "/projects/%s/code-studios/%s/restart" % (self.project_key, self.code_studio_id)) |
| 91 | + return DSSFuture.from_resp(self.client, ret) |
| 92 | + |
| 93 | +class DSSCodeStudioObjectSettings(object): |
| 94 | + """ |
| 95 | + Settings for the code studio object |
| 96 | + """ |
| 97 | + def __init__(self, client, project_key, code_studio_id, settings): |
| 98 | + """Do not call directly, use :meth:`dataikuapi.dss.codestudio.DSSCodeStudioObject.get_settings`""" |
| 99 | + self.client = client |
| 100 | + self.project_key = project_key |
| 101 | + self.code_studio_id = code_studio_id |
| 102 | + self.settings = settings |
| 103 | + |
| 104 | + def get_raw(self): |
| 105 | + """ |
| 106 | + Gets all settings as a raw dictionary. This returns a reference to the raw settings, not a copy, |
| 107 | + """ |
| 108 | + return self.settings |
| 109 | + |
| 110 | + @property |
| 111 | + def template_id(self): |
| 112 | + return self.settings["templateId"] |
| 113 | + @property |
| 114 | + def lib_name(self): |
| 115 | + return self.settings["libName"] |
| 116 | + |
| 117 | + |
| 118 | +class DSSCodeStudioObjectStatus(object): |
| 119 | + """ |
| 120 | + Status of a code studio object |
| 121 | + """ |
| 122 | + def __init__(self, client, project_key, code_studio_id, status): |
| 123 | + """Do not call directly, use :meth:`dataikuapi.dss.codestudio.DSSCodeStudioObject.get_state`""" |
| 124 | + self.client = client |
| 125 | + self.project_key = project_key |
| 126 | + self.code_studio_id = code_studio_id |
| 127 | + self.status = status |
| 128 | + |
| 129 | + def get_raw(self): |
| 130 | + """ |
| 131 | + Gets the status as a raw dictionary. This returns a reference to the raw status, not a copy, |
| 132 | + """ |
| 133 | + return self.status |
| 134 | + |
| 135 | + @property |
| 136 | + def state(self): |
| 137 | + return self.status["state"] |
| 138 | + @property |
| 139 | + def last_state_change(self): |
| 140 | + ts = self.status.get("lastStateChange", 0) |
| 141 | + if ts > 0: |
| 142 | + return datetime.fromtimestamp(ts / 1000) |
| 143 | + else: |
| 144 | + return None |
0 commit comments