|
| 1 | +import pydoc |
| 2 | + |
| 3 | +import terminaltables |
| 4 | + |
1 | 5 | from paperspace.commands import CommandBase |
| 6 | +from paperspace.utils import get_terminal_lines |
2 | 7 |
|
3 | 8 |
|
4 | 9 | class JobsCommandBase(CommandBase): |
@@ -35,3 +40,44 @@ def execute(self, job_id): |
35 | 40 | self._log_message(response, |
36 | 41 | "Job stopped", |
37 | 42 | "Unknown error while stopping job") |
| 43 | + |
| 44 | + |
| 45 | +class ListJobsCommand(JobsCommandBase): |
| 46 | + def execute(self): |
| 47 | + response = self.api.get("/jobs/getJobs/", json=None) |
| 48 | + |
| 49 | + try: |
| 50 | + data = response.json() |
| 51 | + if not response.ok: |
| 52 | + self.logger.log_error_response(data) |
| 53 | + return |
| 54 | + except (ValueError, KeyError) as e: |
| 55 | + self.logger.log("Error while parsing response data: {}".format(e)) |
| 56 | + else: |
| 57 | + self._log_jobs_list(data) |
| 58 | + |
| 59 | + def _log_jobs_list(self, data): |
| 60 | + if not data: |
| 61 | + self.logger.log("No jobs found") |
| 62 | + else: |
| 63 | + table_str = self._make_table(data) |
| 64 | + if len(table_str.splitlines()) > get_terminal_lines(): |
| 65 | + pydoc.pager(table_str) |
| 66 | + else: |
| 67 | + self.logger.log(table_str) |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def _make_table(jobs): |
| 71 | + data = [("ID", "Name", "Project", "Cluster", "Machine Type", "Created")] |
| 72 | + for job in jobs: |
| 73 | + id_ = job.get("id") |
| 74 | + name = job.get("name") |
| 75 | + project = job.get("project") |
| 76 | + cluster = job.get("cluster") |
| 77 | + machine_type = job.get("machineType") |
| 78 | + created = job.get("dtCreated") |
| 79 | + data.append((id_, name, project, cluster, machine_type, created)) |
| 80 | + |
| 81 | + ascii_table = terminaltables.AsciiTable(data) |
| 82 | + table_string = ascii_table.table |
| 83 | + return table_string |
0 commit comments