Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions checkbox-support/checkbox_support/helpers/slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@


def slugify(_string):
"""Transform any string to one that can be used in filenames."""
"""
Slugify a string

Transform any string to one that can be used in filenames and Python
identifers.
"""
valid_chars = frozenset(
"-_.{}{}".format(string.ascii_letters, string.digits)
"_{}{}".format(string.ascii_letters, string.digits)
)
# Python identifiers cannot start with a digit
if _string[0].isdigit():
_string = "_" + _string
return "".join(c if c in valid_chars else "_" for c in _string)
14 changes: 12 additions & 2 deletions checkbox-support/checkbox_support/tests/test_slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
# 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

Expand All @@ -31,3 +29,15 @@ def test_slugify_no_change(self):
def test_slugify_special_chars(self):
result = slugify("C'était une belle journée !")
self.assertEqual(result, "C__tait_une_belle_journ_e__")

def test_slugify_hyphens(self):
result = slugify("usb-vendor")
self.assertEqual(result, "usb_vendor")

def test_slugify_dots(self):
result = slugify("my.funny.valentine")
self.assertEqual(result, "my_funny_valentine")

def test_slugify_string_starting_with_number(self):
result = slugify("123abc")
self.assertEqual(result, "_123abc")
Loading