Skip to content

Commit 73943c7

Browse files
committed
flux-resource: abstract fetch of resource list
Problem: The fetch of reosurce list and config may be a common operation in the flux-resource command, but is currently embedded in the list command function list_handler(). Add a `get_resource_list()` function that combines the common operations of fetching the scheduler resource list and instance config, processing the --states and --include options.
1 parent ade38ce commit 73943c7

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/cmd/flux-resource.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -506,24 +506,15 @@ def resources_uniq_lines(resources, states, formatter, config):
506506
return lines
507507

508508

509-
def list_handler(args):
509+
def get_resource_list(args):
510+
"""
511+
Common function for list_handler() and emit_R()
512+
"""
510513
valid_states = ["up", "down", "allocated", "free", "all"]
511-
headings = {
512-
"state": "STATE",
513-
"queue": "QUEUE",
514-
"properties": "PROPERTIES",
515-
"propertiesx": "PROPERTIES",
516-
"nnodes": "NNODES",
517-
"ncores": "NCORES",
518-
"ngpus": "NGPUS",
519-
"ranks": "RANKS",
520-
"nodelist": "NODELIST",
521-
"rlist": "LIST",
522-
}
523514
config = None
524515

525-
states = args.states.split(",")
526-
for state in states:
516+
args.states = args.states.split(",")
517+
for state in args.states:
527518
if state not in valid_states:
528519
LOGGER.error("Invalid resource state %s specified", state)
529520
sys.exit(1)
@@ -544,11 +535,28 @@ def list_handler(args):
544535
resources.filter(include=args.include)
545536
except (ValueError, TypeError) as exc:
546537
raise ValueError(f"--include: {exc}") from None
538+
return resources, config
539+
540+
541+
def list_handler(args):
542+
headings = {
543+
"state": "STATE",
544+
"queue": "QUEUE",
545+
"properties": "PROPERTIES",
546+
"propertiesx": "PROPERTIES",
547+
"nnodes": "NNODES",
548+
"ncores": "NCORES",
549+
"ngpus": "NGPUS",
550+
"ranks": "RANKS",
551+
"nodelist": "NODELIST",
552+
"rlist": "LIST",
553+
}
554+
resources, config = get_resource_list(args)
547555

548556
fmt = FluxResourceConfig("list").load().get_format_string(args.format)
549557
formatter = flux.util.OutputFormat(fmt, headings=headings)
550558

551-
lines = resources_uniq_lines(resources, states, formatter, config)
559+
lines = resources_uniq_lines(resources, args.states, formatter, config)
552560
formatter.print_items(lines.values(), no_header=args.no_header)
553561

554562

0 commit comments

Comments
 (0)