Skip to content

Commit

Permalink
simplify Terminate exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Jun 27, 2024
1 parent d147e4c commit e3abf1a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
16 changes: 8 additions & 8 deletions spalloc_client/scripts/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def power_job(client, timeout, job_id, power):
raise Terminate(7) from exc
else:
# In an unknown state, perhaps the job was queued etc.
raise Terminate(8, "Error: Cannot power {} job {} in state {}",
"on" if power else "off",
job_id, _state_name(state))
raise Terminate(
8, (f"Error: Cannot power {'on' if power else 'off'} "
f"job {job_id} in state {_state_name(state)}"))


def list_ips(client, timeout, job_id):
Expand All @@ -267,7 +267,7 @@ def list_ips(client, timeout, job_id):
info = client.get_job_machine_info(job_id, timeout=timeout)
connections = info["connections"]
if connections is None:
raise Terminate(9, "Job {} is queued or does not exist", job_id)
raise Terminate(9, f"Job {job_id} is queued or does not exist")
print("x,y,hostname")
for ((x, y), hostname) in sorted(connections):
print("{},{},{}".format(x, y, hostname))
Expand Down Expand Up @@ -308,11 +308,11 @@ def get_job_id(self, client, args):
jobs = client.list_jobs(timeout=args.timeout)
job_ids = [job["job_id"] for job in jobs if job["owner"] == args.owner]
if not job_ids:
raise Terminate(3, "Owner {} has no live jobs", args.owner)
raise Terminate(3, f"Owner {args.owner} has no live jobs")
elif len(job_ids) > 1:
raise Terminate(3, "Ambiguous: {} has {} live jobs: {}",
args.owner, len(job_ids),
", ".join(map(str, job_ids)))
msg = (f"Ambiguous: {args.owner} has {len(job_ids)} live jobs: "
f"{', '.join(map(str, job_ids))}")
raise Terminate(3, msg)
return job_ids[0]

def get_parser(self, cfg):
Expand Down
2 changes: 1 addition & 1 deletion spalloc_client/scripts/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _get_machine(machines, machine_name):
if machine["name"] == machine_name:
return machine
# No matching machine
raise Terminate(6, "No machine '{}' was found", machine_name)
raise Terminate(6, f"No machine '{machine_name}' was found")


def show_machine(t, machines, jobs, machine_name, compact=False):
Expand Down
16 changes: 5 additions & 11 deletions spalloc_client/scripts/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import sys
from typing import Optional
from spalloc_client import (
config, ProtocolClient, ProtocolError, ProtocolTimeoutError,
SpallocServerException)
Expand All @@ -24,17 +25,10 @@


class Terminate(Exception):
def __init__(self, code, *args):
def __init__(self, code: int, message: Optional[str] = None):
super().__init__()
self._code = code
args = list(args)
message = args.pop(0) if args else None
if message is None:
self._msg = None
elif args:
self._msg = message.format(*args)
else:
self._msg = message
self._msg = message

def exit(self):
if self._msg is not None:
Expand All @@ -45,8 +39,8 @@ def exit(self):
def version_verify(client, timeout):
version = tuple(map(int, client.version(timeout=timeout).split(".")))
if not (VERSION_RANGE_START <= version < VERSION_RANGE_STOP):
raise Terminate(2, "Incompatible server version ({})",
".".join(map(str, version)))
raise Terminate(
2, f"Incompatible server version ({'.'.join(map(str, version))})")


class Script(object):
Expand Down

0 comments on commit e3abf1a

Please sign in to comment.