From 207da9cda1f45d83a6c3c5c55b3efa297063f3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 5 Jun 2020 17:51:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Vendor=20bundle=20awesome-slugif?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise there's a namespace clash with python-slugify which now is required by cookiecutter. --- setup.py | 8 +- src/octoprint/filemanager/storage.py | 2 +- .../plugins/announcements/__init__.py | 2 +- .../vendor/awesome_slugify/__init__.py | 37 +++ .../vendor/awesome_slugify/alt_translates.py | 27 ++ src/octoprint/vendor/awesome_slugify/main.py | 244 ++++++++++++++++++ 6 files changed, 315 insertions(+), 5 deletions(-) create mode 100644 src/octoprint/vendor/awesome_slugify/__init__.py create mode 100644 src/octoprint/vendor/awesome_slugify/alt_translates.py create mode 100644 src/octoprint/vendor/awesome_slugify/main.py diff --git a/setup.py b/setup.py index beb790f48..c4f62d783 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,6 @@ INSTALL_REQUIRES = [ "Flask-Login>=0.5,<1", "Flask-Babel>=1.0,<2", "Flask-Assets>=2.0,<3", - "blinker>=1.4,<2", # dependency of the now vendor bundled flask_principal "werkzeug>=1.0.1,<2", "cachelib>=0.1,<1", "PyYAML>=5.3.1,<6", @@ -57,7 +56,6 @@ INSTALL_REQUIRES = [ "semantic_version>=2.8.5,<3", "psutil>=5.7,<6", "Click>=7.1.2,<8", - "awesome-slugify>=1.6.5,<2", "feedparser>=5.2.1,<6", "future>=0.18.2,<1", "websocket-client>=0.57,<1", @@ -65,7 +63,11 @@ INSTALL_REQUIRES = [ "emoji>=0.5.4,<1", "frozendict>=1.2,<2", "sentry-sdk>=0.14.4,<1", - "filetype>=1.0.7,<2" + "filetype>=1.0.7,<2", + + # vendor bundle dependencies + "unidecode>=0.04.14,<0.05", # dependency of awesome-slugify + "blinker>=1.4,<2" # dependency of flask_principal ] # Python 2 specific requirements diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 925e1e812..2eab1d372 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 +from octoprint.vendor.awesome_slugify import Slugify import octoprint.filemanager diff --git a/src/octoprint/plugins/announcements/__init__.py b/src/octoprint/plugins/announcements/__init__.py index 44719840c..d88d935d0 100644 --- a/src/octoprint/plugins/announcements/__init__.py +++ b/src/octoprint/plugins/announcements/__init__.py @@ -42,7 +42,7 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin, self._cached_channel_configs = None self._cached_channel_configs_mutex = threading.RLock() - from slugify import Slugify + from octoprint.vendor.awesome_slugify import Slugify self._slugify = Slugify() self._slugify.safe_chars = "-_." diff --git a/src/octoprint/vendor/awesome_slugify/__init__.py b/src/octoprint/vendor/awesome_slugify/__init__.py new file mode 100644 index 000000000..e0e2a564f --- /dev/null +++ b/src/octoprint/vendor/awesome_slugify/__init__.py @@ -0,0 +1,37 @@ + +from .main import Slugify, UniqueSlugify +from .alt_translates import * + + +slugify = Slugify() +unique_slugify = UniqueSlugify() +slugify_unicode = Slugify(translate=None) + +slugify_url = Slugify() +slugify_url.to_lower = True +slugify_url.stop_words = ('a', 'an', 'the') +slugify_url.max_length = 200 + +slugify_filename = Slugify() +slugify_filename.separator = '_' +slugify_filename.safe_chars = '-.' +slugify_filename.max_length = 255 + +slugify_ru = Slugify(pretranslate=CYRILLIC) +slugify_de = Slugify(pretranslate=GERMAN) +slugify_el = Slugify(pretranslate=GREEK) + + +# Legacy code +def deprecate_init(Klass): + class NewKlass(Klass): + def __init__(self, *args, **kwargs): + import warnings + warnings.simplefilter('once') + warnings.warn("'slugify.get_slugify' is deprecated; use 'slugify.Slugify' instead.", + DeprecationWarning, stacklevel=2) + super(NewKlass, self).__init__(*args, **kwargs) + return NewKlass + +# get_slugify was deprecated in 2014, march 31 +get_slugify = deprecate_init(Slugify) diff --git a/src/octoprint/vendor/awesome_slugify/alt_translates.py b/src/octoprint/vendor/awesome_slugify/alt_translates.py new file mode 100644 index 000000000..edfc99165 --- /dev/null +++ b/src/octoprint/vendor/awesome_slugify/alt_translates.py @@ -0,0 +1,27 @@ +# coding=utf8 + +CYRILLIC = { # instead of: + u'ё': u'e', # io / yo + u'у': u'y', # u + u'х': u'h', # kh + u'щ': u'sch', # shch + u'ю': u'u', # iu / yu + u'я': u'ya', # ia +} + +GERMAN = { # instead of: + u'ä': u'ae', # a + u'ö': u'oe', # o + u'ü': u'ue', # u +} + +GREEK = { # instead of: + u'Ξ': u'X', # Ks + u'χ': u'ch', # kh + + u'ϒ': u'Y', # U + u'υ': u'y', # u + u'ύ': u'y', # ... + u'ϋ': u'y', + u'ΰ': u'y', +} diff --git a/src/octoprint/vendor/awesome_slugify/main.py b/src/octoprint/vendor/awesome_slugify/main.py new file mode 100644 index 000000000..dfb07b063 --- /dev/null +++ b/src/octoprint/vendor/awesome_slugify/main.py @@ -0,0 +1,244 @@ +# coding=utf8 + +import sys + +from unidecode import unidecode +import regex as re + + +# Don't set regex.DEFAULT_VERSION to regex.VERSION1 cause +# this option will influence on 3rd party libs. E.g. `mailgun` and `flanker`. +# Use regex.VERSION1 regex flag. + +# re.VERSION1 - New enhanced behaviour with nested sets and set operations + + +if sys.version_info[0] == 2: + str_type = unicode # Python 2 +else: + str_type = str # Python 3 + + +def join_words(words, separator, max_length=None): + """ + words - iterator or list + """ + + if not max_length: + return separator.join(words) + + words = iter(words) # List to Generator + try: + text = next(words) + except StopIteration: + return u'' + + for word in words: + if len(text + separator + word) <= max_length: + text += separator + word + + return text[:max_length] + +# uppercase letters to translate to uppercase letters, NOT camelcase +UPPER_TO_UPPER_LETTERS_RE = \ + u''' + ( + \p{Uppercase_Letter} {2,} # 2 or more adjacent letters - UP always + | + \p{Uppercase_Letter} # target one uppercase letter, then + (?= + [^\p{Lowercase_Letter}…\p{Term}--,،﹐,]+ # not chars breaks possible UP (…abc.?!:;) + \p{Uppercase_Letter} {2} # and 2 uppercase letters + ) + | + (?<= + \p{Uppercase_Letter} {2} # 2 uppercase letters + [^\p{Lowercase_Letter}…\p{Term}--,،﹐,]+ # not chars breaks possible UP (…abc.?!:;), then + ) + \p{Uppercase_Letter} # target one uppercase letter, then + (?! + \p{Lowercase_Letter} # not lowercase letter + | + […\p{Term}--,،﹐,]\p{Uppercase_Letter} # and not dot (.?…!:;) with uppercase letter + ) + ) + ''' + + +class Slugify(object): + + upper_to_upper_letters_re = re.compile(UPPER_TO_UPPER_LETTERS_RE, re.VERBOSE | re.VERSION1) + _safe_chars = '' + _stop_words = () + + def __init__(self, pretranslate=None, translate=unidecode, safe_chars='', stop_words=(), + to_lower=False, max_length=None, separator=u'-', capitalize=False, + fold_abbrs=False): + + self.pretranslate = pretranslate + self.translate = translate + self.safe_chars = safe_chars + self.stop_words = stop_words + + self.to_lower = to_lower + self.max_length = max_length + self.separator = separator + self.capitalize = capitalize + self.fold_abbrs = fold_abbrs + + def pretranslate_dict_to_function(self, convert_dict): + + # add uppercase letters + for letter, translation in list(convert_dict.items()): + letter_upper = letter.upper() + if letter_upper != letter and letter_upper not in convert_dict: + convert_dict[letter_upper] = translation.capitalize() + + self.convert_dict = convert_dict + PRETRANSLATE = re.compile(u'(\L)', options=convert_dict) + + # translate some letters before translating + return lambda text: PRETRANSLATE.sub(lambda m: convert_dict[m.group(1)], text) + + def set_pretranslate(self, pretranslate): + if isinstance(pretranslate, dict): + pretranslate = self.pretranslate_dict_to_function(pretranslate) + + elif pretranslate is None: + pretranslate = lambda text: text + + elif not callable(pretranslate): + error_message = u"Keyword argument 'pretranslate' must be dict, None or callable. Not {0.__class__.__name__}".format(pretranslate) + raise ValueError(error_message) + + self._pretranslate = pretranslate + + pretranslate = property(fset=set_pretranslate) + + def set_translate(self, func): + if func: + self._translate = func + else: + self._translate = lambda text: text + + translate = property(fset=set_translate) + + def set_safe_chars(self, safe_chars): + self._safe_chars = safe_chars + self.apostrophe_is_not_safe = "'" not in safe_chars + self.calc_unwanted_chars_re() + + safe_chars = property(fset=set_safe_chars) + + def set_stop_words(self, stop_words): + self._stop_words = tuple(stop_words) + self.calc_unwanted_chars_re() + + stop_words = property(fset=set_stop_words) + + def calc_unwanted_chars_re(self): + unwanted_chars_re = u'[^\p{{AlNum}}{safe_chars}]+'.format(safe_chars=re.escape(self._safe_chars or '')) + self.unwanted_chars_re = re.compile(unwanted_chars_re, re.IGNORECASE) + + if self._stop_words: + unwanted_chars_and_words_re = unwanted_chars_re + u'|(?)(?!\p{AlNum})' + self.unwanted_chars_and_words_re = re.compile(unwanted_chars_and_words_re, re.IGNORECASE, stop_words=self._stop_words) + else: + self.unwanted_chars_and_words_re = None + + def sanitize(self, text): + if self.apostrophe_is_not_safe: + text = text.replace("'", '').strip() # remove ' + + if self.unwanted_chars_and_words_re: + words = [word for word in self.unwanted_chars_and_words_re.split(text) if word] + if words: + return words + + words = filter(None, self.unwanted_chars_re.split(text)) + return words + + def __call__(self, text, **kwargs): + + max_length = kwargs.get('max_length', self.max_length) + separator = kwargs.get('separator', self.separator) + + if not isinstance(text, str_type): + text = text.decode('utf8', 'ignore') + + if kwargs.get('fold_abbrs', self.fold_abbrs): + text = re.sub(r'(?