Skip to content

Commit

Permalink
python: split flux.job module into multiple files
Browse files Browse the repository at this point in the history
Problem: The Python 'flux.job' module is large (>1200 lines)
and thus getting difficult to read and/or modify. This module will
only get larger since there will certainly be more classes and
methods related to Flux jobs added to the Python API.

Split `src/bindings/python/flux/job.py` into multiple files, while
attempting to keep related code together without making any one component
too large.

For backwards compatibility, recreate the previous `flux.job` namespace
in the __init__.py in the new job directory.
  • Loading branch information
grondo committed Aug 25, 2020
1 parent a367872 commit f0a67dc
Show file tree
Hide file tree
Showing 11 changed files with 714 additions and 575 deletions.
13 changes: 11 additions & 2 deletions src/bindings/python/flux/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ nobase_fluxpy_PYTHON = \
rpc.py \
message.py \
constants.py \
job.py \
util.py \
future.py \
memoized_property.py \
Expand All @@ -14,7 +13,17 @@ nobase_fluxpy_PYTHON = \
core/watchers.py \
core/inner.py \
core/handle.py \
core/trampoline.py
core/trampoline.py \
job/__init__.py \
job/JobID.py \
job/Jobspec.py \
job/event.py \
job/kill.py \
job/kvs.py \
job/list.py \
job/submit.py \
job/wait.py \
job/wrapper.py

if HAVE_FLUX_SECURITY
nobase_fluxpy_PYTHON += security.py
Expand Down
99 changes: 99 additions & 0 deletions src/bindings/python/flux/job/JobID.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
###############################################################
# Copyright 2020 Lawrence Livermore National Security, LLC
# (c.f. AUTHORS, NOTICE.LLNS, COPYING)
#
# This file is part of the Flux resource manager framework.
# For details, see https://github.com/flux-framework.
#
# SPDX-License-Identifier: LGPL-3.0
###############################################################

from flux.job.wrapper import RAW
from _flux._core import ffi


def id_parse(jobid_str):
"""
returns: An integer jobid
:rtype int
"""
jobid = ffi.new("flux_jobid_t[1]")
RAW.id_parse(jobid_str, jobid)
return int(jobid[0])


def id_encode(jobid, encoding="f58"):
"""
returns: Jobid encoded in encoding
:rtype str
"""
buflen = 128
buf = ffi.new("char[]", buflen)
RAW.id_encode(int(jobid), encoding, buf, buflen)
return ffi.string(buf, buflen).decode("utf-8")


class JobID(int):
"""Class used to represent a Flux JOBID
JobID is a subclass of `int`, so may be used in place of integer.
However, a JobID may be created from any valid RFC 19 FLUID
encoding, including:
- decimal integer (no prefix)
- hexidecimal integer (prefix 0x)
- dotted hex (dothex) (xxxx.xxxx.xxxx.xxxx)
- kvs dir (dotted hex with `job.` prefix)
- RFC19 F58: (Base58 encoding with prefix `ƒ` or `f`)
A JobID object also has properties for encoding a JOBID into each
of the above representations, e.g. jobid.f85, jobid.words, jobid.dothex...
"""

def __new__(cls, value):
if isinstance(value, int):
jobid = value
else:
jobid = id_parse(value)
return super(cls, cls).__new__(cls, jobid)

def encode(self, encoding="dec"):
"""Encode a JobID to alternate supported format"""
return id_encode(self, encoding)

@property
def dec(self):
"""Return decimal integer representation of a JobID"""
return self.encode()

@property
def f58(self):
"""Return RFC19 F58 representation of a JobID"""
return self.encode("f58")

@property
def hex(self):
"""Return 0x-prefixed hexidecimal representation of a JobID"""
return self.encode("hex")

@property
def dothex(self):
"""Return dotted hexidecimal representation of a JobID"""
return self.encode("dothex")

@property
def words(self):
"""Return words (mnemonic) representation of a JobID"""
return self.encode("words")

@property
def kvs(self):
"""Return KVS directory path of a JobID"""
return self.encode("kvs")

def __str__(self):
return self.encode("f58")

def __repr__(self):
return f"JobID({self.dec})"
Loading

0 comments on commit f0a67dc

Please sign in to comment.