From 73fbd15bcab50684f66d753aa3feffd5cf9ce8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 3 Jun 2020 14:50:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=E2=9E=96=20Switch=20from=20awesome-sl?= =?UTF-8?q?ugify=20to=20python-slugify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 2 +- src/octoprint/filemanager/storage.py | 10 ++++++---- src/octoprint/plugins/announcements/__init__.py | 6 +++--- tests/filemanager/test_localstorage.py | 7 ++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 37a9955f6..d95e60e60 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,7 @@ INSTALL_REQUIRES = [ "semantic_version>=2.8.5,<3", "psutil>=5.7,<6", "Click>=7.1.2,<8", - "awesome-slugify>=1.6.5,<2", + "python-slugify>=4,<5", "feedparser>=5.2.1,<6", "future>=0.18.2,<1", "websocket-client>=0.57,<1", diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 925e1e812..ffc16f2c0 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -24,7 +24,7 @@ from contextlib import contextmanager from past.builtins import basestring from emoji import demojize -from slugify import Slugify +import slugify import octoprint.filemanager @@ -456,15 +456,17 @@ class LocalFileStorage(StorageInterface): def _no_unicode_variations(cls, text): return cls._UNICODE_VARIATIONS.sub("", text) - _SLUGIFY = Slugify() - _SLUGIFY.safe_chars = "-_.()[] " + _SLUGIFY_ALLOWED_REGEX = re.compile(r"[^-_.()[]a-zA-Z0-9]+") @classmethod def _slugify(cls, text): text = to_unicode(text) text = cls._no_unicode_variations(text) text = demojize(text, delimiters=("", "")) - return cls._SLUGIFY(text) + return slugify.slugify(text, + regex_pattern=cls._SLUGIFY_ALLOWED_REGEX, + separator="_", + lowercase=False) def __init__(self, basefolder, create=False): """ diff --git a/src/octoprint/plugins/announcements/__init__.py b/src/octoprint/plugins/announcements/__init__.py index 44719840c..f0837d59f 100644 --- a/src/octoprint/plugins/announcements/__init__.py +++ b/src/octoprint/plugins/announcements/__init__.py @@ -42,9 +42,9 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin, self._cached_channel_configs = None self._cached_channel_configs_mutex = threading.RLock() - from slugify import Slugify - self._slugify = Slugify() - self._slugify.safe_chars = "-_." + from slugify import slugify + pattern = re.compile(r"^[-_.a-zA-Z0-9]+") + self._slugify = lambda x: slugify(x, regex_pattern=pattern) # Additional permissions hook diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index 811493f46..4258e57f2 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -55,7 +55,7 @@ class LocalStorageTest(unittest.TestCase): def get_file_type(name): if name.lower().endswith(".stl"): return ["model", "stl"] - elif name.lower().endswith(".gco") or name.lower().endswith(".gcode") or name.lower.endswith(".g"): + elif name.lower().endswith(".gco") or name.lower().endswith(".gcode") or name.lower().endswith(".g"): return ["machinecode", "gcode"] else: return None @@ -628,12 +628,13 @@ class LocalStorageTest(unittest.TestCase): @data( ("test", "test"), ("\u2764", "red_heart"), - ("\u2764\ufe00", "red_heart") + ("\u2764\ufe00", "red_heart"), + ("Häußge", "Haussge") ) @unpack def test_slugify(self, input, expected): output = LocalFileStorage._slugify(input) - self.assertEqual(output, expected) + self.assertEqual(expected, output) def _add_and_verify_file(self, path, expected_path, file_object, links=None, overwrite=False, display=None): """Adds a file to the storage and verifies the sanitized path."""