Skip to content

Commit

Permalink
feat: add kebabizer for formatting slurm keys
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Nucciarone <[email protected]>
  • Loading branch information
NucciTheBoss committed Jul 9, 2024
1 parent 571f75f commit fff9c94
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/charms/hpc_libs/v0/slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def _on_install(self, _) -> None:
"""

__all__ = [
"format_key",
"install",
"version",
"ServiceType",
Expand All @@ -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
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fff9c94

Please sign in to comment.