Skip to content

Commit

Permalink
Add checkbox-support helper function to slugify a given string
Browse files Browse the repository at this point in the history
This slugify function was duplicated in  many places (checkbox-support,
providers...). Putting it in a centralized location for use by other
providers or by other functions in checkbox-support.
  • Loading branch information
pieqq committed Dec 11, 2024
1 parent 027b98a commit 2cf5b12
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
26 changes: 26 additions & 0 deletions checkbox-support/checkbox_support/helpers/slugify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This file is part of Checkbox.
#
# Copyright 2024 Canonical Ltd.
# Written by:
# Pierre Equoy <[email protected]>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

import string

def slugify(_string):
"""Transform any string to one that can be used in filenames."""
valid_chars = frozenset(
"-_.{}{}".format(string.ascii_letters, string.digits)
)
return "".join(c if c in valid_chars else "_" for c in _string)
9 changes: 1 addition & 8 deletions checkbox-support/checkbox_support/parsers/udevadm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import re
import string

from checkbox_support.helpers.slugify import slugify
from checkbox_support.lib.bit import get_bitmask
from checkbox_support.lib.bit import test_bit
from checkbox_support.lib.input import Input
Expand Down Expand Up @@ -96,14 +97,6 @@
CAMERA_RE = re.compile(r"Camera", re.I)


def slugify(_string):
"""Transform any string to one that can be used in job IDs."""
valid_chars = frozenset(
"-_.{}{}".format(string.ascii_letters, string.digits)
)
return "".join(c if c in valid_chars else "_" for c in _string)


def find_pkname_is_root_mountpoint(devname, lsblk=None):
"""Check for partition mounted as root for a DISK device."""
if lsblk:
Expand Down
33 changes: 33 additions & 0 deletions checkbox-support/checkbox_support/tests/test_slugify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is part of Checkbox.
#
# Copyright 2024 Canonical Ltd.
# Written by:
# Pierre Equoy <[email protected]>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

from unittest import TestCase
from unittest.mock import patch
from io import StringIO

from checkbox_support.helpers.slugify import slugify


class TestSlugify(TestCase):
def test_slugify_no_change(self):
result = slugify("abc123")
self.assertEqual(result, "abc123")

def test_slugify_special_chars(self):
result = slugify("C'était une belle journée !")
self.assertEqual(result, "C__tait_une_belle_journ_e__")
10 changes: 1 addition & 9 deletions providers/resource/bin/graphics_card_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import collections
import subprocess
import shlex
import string
import re

from checkbox_support.helpers.slugify import slugify

def get_ubuntu_version():
"""Get Ubuntu release version for checking."""
Expand Down Expand Up @@ -59,14 +59,6 @@ def compare_ubuntu_release_version(_version):
return os_version >= _version


def slugify(_string):
"""Transform any string to one that can be used in job IDs."""
valid_chars = frozenset(
"-_.{}{}".format(string.ascii_letters, string.digits)
)
return "".join(c if c in valid_chars else "_" for c in _string)


def subprocess_lines_generator(command):
"""
Generator that opens a subprocess and spits out lines
Expand Down
10 changes: 1 addition & 9 deletions providers/resource/bin/snapd_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import argparse
import os
import string
import sys

from checkbox_support.helpers.slugify import slugify
from checkbox_support.snap_utils.asserts import decode
from checkbox_support.snap_utils.asserts import model_to_resource
from checkbox_support.snap_utils.asserts import serial_to_resource
Expand All @@ -21,14 +21,6 @@
from collections import namedtuple


def slugify(_string):
"""Transform string to one that can be used as the key in a resource job"""
valid_chars = frozenset(
"_{}{}".format(string.ascii_letters, string.digits)
)
return "".join(c if c in valid_chars else "_" for c in _string)


class ModelAssertion:

def invoked(self):
Expand Down

0 comments on commit 2cf5b12

Please sign in to comment.