From fff9c94e4fa0729ebe7012c13434bdd5f6745970 Mon Sep 17 00:00:00 2001 From: "Jason C. Nucciarone" Date: Tue, 9 Jul 2024 17:04:43 -0400 Subject: [PATCH] feat: add kebabizer for formatting slurm keys Signed-off-by: Jason C. Nucciarone --- lib/charms/hpc_libs/v0/slurm_ops.py | 25 +++++++++++++++++++++++++ tests/unit/test_slurm_ops.py | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/lib/charms/hpc_libs/v0/slurm_ops.py b/lib/charms/hpc_libs/v0/slurm_ops.py index 7f184b8..7016dc1 100644 --- a/lib/charms/hpc_libs/v0/slurm_ops.py +++ b/lib/charms/hpc_libs/v0/slurm_ops.py @@ -57,6 +57,7 @@ def _on_install(self, _) -> None: """ __all__ = [ + "format_key", "install", "version", "ServiceType", @@ -65,6 +66,7 @@ def _on_install(self, _) -> None: import json import logging +import re import subprocess from collections.abc import Mapping from enum import Enum @@ -86,6 +88,29 @@ def _on_install(self, _) -> None: PYDEPS = ["pyyaml>=6.0.1"] _logger = logging.getLogger(__name__) +_acronym = re.compile(r"(?<=[A-Z])(?=[A-Z][a-z])") +_kebabize = re.compile(r"(?<=[a-z0-9])(?=[A-Z])") + + +def format_key(key: str) -> str: + """Format Slurm configuration keys from SlurmCASe into kebab case. + + Args: + key: Slurm configuration key to convert to kebab case. + + Notes: + Slurm configuration syntax does not follow proper PascalCasing + format, so we cannot put keys directly through a kebab case converter + to get the desired format. Some additional processing is needed for + certain keys before the key can properly kebabized. + + For example, without additional preprocessing, the key `CPUs` will + become `cp-us` if put through a kebabizer with being preformatted to `Cpus`. + """ + if "CPUs" in key: + key = key.replace("CPUs", "Cpus") + key = _acronym.sub(r"-", key) + return _kebabize.sub(r"-", key).lower() def install() -> None: diff --git a/tests/unit/test_slurm_ops.py b/tests/unit/test_slurm_ops.py index c02b5cc..48e6279 100644 --- a/tests/unit/test_slurm_ops.py +++ b/tests/unit/test_slurm_ops.py @@ -44,6 +44,11 @@ class TestSlurmOps(TestCase): def setUp(self) -> None: self.setUpPyfakefs() + def test_format_key(self, _) -> None: + """Test that `kebabize` properly formats slurm keys.""" + self.assertEqual(slurm.format_key("CPUs"), "cpus") + self.assertEqual(slurm.format_key("AccountingStorageHost"), "accounting-storage-host") + def test_install(self, subcmd) -> None: """Test that `slurm_ops` calls the correct install command.""" slurm.install()