def get_byline_image(self, force_new=False): slugify = Slugify() if not force_new and self.byline_photo: return self.byline_photo imagefiles = glob.glob(os.path.join(settings.BYLINE_PHOTO_DIR, '*.jpg')) name = self.name name_last_first = re.sub(r'^(.*) (\S+)$', r'\2 \1', name) name_slug_title = slugify(name) + '.jpg' name_slug = name_slug_title.lower() name_slug_reverse = slugify(name_last_first).lower() + '.jpg' bestratio = 90 bestmatch = None for path in imagefiles: filename = os.path.split(path)[1].lower() ratio = max(fuzz.ratio(filename, name_slug), fuzz.ratio(filename, name_slug_reverse)) if ratio > bestratio: bestmatch = path bestratio = ratio if ratio == 100: break if bestmatch: msg = 'found match: name:{}, img:{}, ratio:{} '.format( name_slug, bestmatch, ratio) logger.debug(msg) with open(bestmatch, 'rb') as source: content = File(source) img = ProfileImage() img.source_file.save(name_slug_title, content) self.byline_photo = img self.save() return img
def test_fold_abbr_4(self): slugify = Slugify(fold_abbrs=True) self.assertEqual('mind-in-a-box', slugify('mind.in.a.box')) self.assertEqual('mind-in-a-b-c-box', slugify('mind.in.a.b.c.box')) self.assertEqual('a-b-c-box', slugify('.a.b.c.box')) self.assertEqual('abcbox', slugify('a.b.c.box')) self.assertEqual('abcb-ox', slugify('a.b.c.b ox'))
def __init__(self, abs_path): self.id = abs_path self.type = self.file_type() self.mod_path = None self.slug_path = None self.file_ext = None self.slug_gen = Slugify()
def upload_folder(instance, filename): slugify = Slugify(to_lower=True) customer_name = re.sub(r'\s+', '', slugify(instance.customer.name))[:15].lower() path = '/'.join(['adverts', customer_name, filename]) logger.debug(path) return path
def __init__(self, basefolder, create=False): """ Initializes a ``LocalFileStorage`` instance under the given ``basefolder``, creating the necessary folder if necessary and ``create`` is set to ``True``. :param string basefolder: the path to the folder under which to create the storage :param bool create: ``True`` if the folder should be created if it doesn't exist yet, ``False`` otherwise """ self._logger = logging.getLogger(__name__) self.basefolder = os.path.realpath(os.path.abspath(basefolder)) if not os.path.exists(self.basefolder) and create: os.makedirs(self.basefolder) if not os.path.exists(self.basefolder) or not os.path.isdir(self.basefolder): raise RuntimeError("{basefolder} is not a valid directory".format(**locals())) import threading self._metadata_lock = threading.Lock() self._metadata_cache = pylru.lrucache(10) from slugify import Slugify self._slugify = Slugify() self._slugify.safe_chars = "-_.() " self._old_metadata = None self._initialize_metadata()
def slugify_field(self, field_name): pretranslate = load_slug_rule(self.language) slugify_url = Slugify(pretranslate=pretranslate) slugify_url.to_lower = True slugify_url.stop_words = () slugify_url.max_length = 1000 return slugify_url(self.field_dict[field_name], max_length=1000)
def slugify_filename(self): """ rename source file if needed. Also convert gif to png """ slugify = Slugify(safe_chars='.') try: old_path = self.source_file.path except FileNotFoundError: # noqa old_path = os.path.join(self.source_file.storage.location, self.old_file_path) folder, filename = os.path.split(old_path) new_filename = slugify(filename).replace('-.', '.') new_path = os.path.join(folder, new_filename) if new_path.endswith('.gif'): new_path = new_path.replace('.gif', '.png') im = Image.open(old_path) #transparency = im.info['transparency'] im.save(new_path) self.source_file = new_path.replace( self.source_file.storage.location, '').strip('/') self.save() elif old_path != new_path: if not os.path.exists(new_path): os.rename(old_path, new_path) self.source_file = new_path.replace( self.source_file.storage.location, '').strip('/') # logger.debug('changed file name to {}'.format(new_filename)) self.save()
def get_byline_image(self, force_new=False): slugify = Slugify(to_lower=True) if not force_new and self.byline_photo: return self.byline_photo imagefiles = glob.glob(BYLINE_PHOTO_FOLDER + '/*.jpg') name = self.name.lower() name_last_first = re.sub(r'^(.*) (\S+)$', r'\2 \1', name) name_slug = slugify(name) + '.jpg' name_slug_reverse = slugify(name_last_first) + '.jpg' bestratio = 90 bestmatch = None for path in imagefiles: filename = os.path.split(path)[1].lower() ratio = max( fuzz.ratio(filename, name_slug), fuzz.ratio(filename, name_slug_reverse) ) if ratio > bestratio: bestmatch = path bestratio = ratio if ratio == 100: break if bestmatch: msg = 'found match: name:{}, img:{}, ratio:{} '.format( name_slug, bestmatch, ratio) logger.debug(msg) img, _ = ImageFile.objects.get_or_create(source_file=bestmatch) img.autocrop() self.byline_photo = img self.save() return img
def test_to_lower_arg(self): slugify = Slugify() slugify.to_lower = True self.assertEqual(slugify('Test TO lower'), 'test-to-lower') self.assertEqual(slugify('Test TO lower', to_lower=False), 'Test-TO-lower')
def __init__(self): self._cached_channel_configs = None self._cached_channel_configs_mutex = threading.RLock() from slugify import Slugify self._slugify = Slugify() self._slugify.safe_chars = "-_."
def test_safe_chars(self): slugify = Slugify() slugify.safe_chars = '_' self.assertEqual(slugify('test_sanitize'), 'test_sanitize') slugify.safe_chars = "'" self.assertEqual(slugify('Конь-Огонь'), "Kon'-Ogon'")
def slugify_filename(filename: str) -> Path: """make filename url safe and normalized""" slugify = Slugify(safe_chars='.-', separator='-') fn = Path(filename) stem = Path(filename).stem.split('.')[0] stem = re.sub(r'-+', '-', slugify(re.sub(r'_.{7}$', '', stem))).strip('-') suffix = ''.join(s.lower().replace('jpeg', 'jpg') for s in fn.suffixes) return Path(f'{stem}{suffix}')
def on_model_change(self, form, model, is_created): if is_created: model.created_by = current_user unique_slug = UniqueSlugify(to_lower=True) model.slug = unique_slug(model.name) else: slug = Slugify(to_lower=True) model.slug = slug(model.name)
def create_slug(document, collection_name): collection_slug_map = { 'places': { 'En': 'place', 'He': u'מקום', }, 'familyNames': { 'En': 'familyname', 'He': u'שםמשפחה', }, 'lexicon': { 'En': 'lexicon', 'He': u'מלון', }, 'photoUnits': { 'En': 'image', 'He': u'תמונה', }, 'photos': { 'En': 'image', 'He': u'תמונה', }, # TODO: remove references to the genTreeIndividuals collection - it is irrelevant and not in use 'genTreeIndividuals': { 'En': 'person', 'He': u'אדם', }, 'synonyms': { 'En': 'synonym', 'He': u'שם נרדף', }, 'personalities': { 'En': 'luminary', 'He': u'אישיות', }, 'movies': { 'En': 'video', 'He': u'וידאו', }, } try: headers = document['Header'].items() except KeyError: # persons collection will be handled here as the cllection's docs don't have a Header # it's the calling function responsibility to add a slug # TODO: refactor to more specific logic, instead of relying on them not having a Header return ret = {} slugify = Slugify(translate=None, safe_chars='_') for lang, val in headers: if val: collection_slug = collection_slug_map[collection_name].get(lang) if collection_slug: slug = slugify('_'.join([collection_slug, val.lower()])) ret[lang] = slug.encode('utf8') return ret
def __init__(self, url, title, artist, genre, album): self.url = url self.title = title self.artist = artist self.genre = genre self.album = album self.slugify = Slugify(separator=' ', safe_chars='-.&:\'', translate=None)
def __init__(self, settings): self.settings = settings self.redshift = psycopg2.connect(host=self.settings.DB_HOST, port=self.settings.DB_PORT, user=self.settings.DB_USER, password=self.settings.DB_PASS, database=self.settings.DB_NAME) self.redshift_cursor = self.redshift.cursor() self.custom_slugify = Slugify(to_lower=True) self.custom_slugify.separator = '_'
def setUpClass(self): self.url = '_pbR605pYo8' self.title = 'título' self.artist = 'artista' self.genre = 'genero' self.album = 'album' slugify = Slugify(translate=None, separator=' ', safe_chars='-.') self.video_title = slugify('O menor video do youtube-Han...') self.downloader = Downloader(self.url, self.title, self.artist, self.genre, self.album) self.mp3_path = self.downloader.download()
def create_slug(document, collection_name): collection_slug_map = { 'places': { 'En': 'place', 'He': u'מקום', }, 'familyNames': { 'En': 'familyname', 'He': u'שםמשפחה', }, 'lexicon': { 'En': 'lexicon', 'He': u'מלון', }, 'photoUnits': { 'En': 'image', 'He': u'תמונה', }, 'photos': { 'En': 'image', 'He': u'תמונה', }, 'genTreeIndividuals': { 'En': 'person', 'He': u'אדם', }, 'synonyms': { 'En': 'synonym', 'He': u'שם נרדף', }, 'personalities': { 'En': 'luminary', 'He': u'אישיות', }, 'movies': { 'En': 'video', 'He': u'וידאו', }, } try: headers = document['Header'].items() except KeyError: return ret = {} slugify = Slugify(translate=None, safe_chars='_') for lang, val in headers: if val: collection_slug = collection_slug_map[collection_name][lang] slug = slugify('_'.join([collection_slug, val.lower()])) ret[lang] = slug.encode('utf8') return ret
def __init__( self, target_case="lower", separator=" ", ): #load needed python module from slugify import Slugify self.slug_ = Slugify( separator=separator, to_lower=(target_case == "lower"), )
def __init__(self): self.db = MySQLdb.connect( user='******', passwd='root', host='localhost', db='myanilist', charset='utf8', use_unicode=True ) self.cursor = self.db.cursor() # Enforce UTF-8 for the connection. self.cursor.execute('SET NAMES utf8mb4') self.cursor.execute('SET CHARACTER SET utf8mb4') self.cursor.execute('SET character_set_connection=utf8mb4') # Slugify instance self.slugger = Slugify(to_lower=True)
def slugify_name(name): """Get a slugifier used to ensure asset names are safe""" # Create the slugifier slugifier = Slugify() # Configure the slugifier slugifier.to_lower = True slugifier.safe_chars = '-/' slugifier.max_length = 200 # Names cannot start or end with forward slashes '/' return slugifier(name).strip('/')
def test_pretranslate(self): EMOJI_TRANSLATION = { u'ʘ‿ʘ': u'smiling', u'ಠ_ಠ': u'disapproval', u'♥‿♥': u'enamored', u'♥': u'love', u'(c)': u'copyright', u'©': u'copyright', } slugify_emoji = Slugify(pretranslate=EMOJI_TRANSLATION) self.assertEqual(slugify_emoji(u'ʘ‿ʘ'), u'smiling') self.assertEqual(slugify_emoji(u'ಠ_ಠ'), u'disapproval') self.assertEqual(slugify_emoji(u'(c)'), u'copyright') self.assertEqual(slugify_emoji(u'©'), u'copyright')
def test_stop_words(self): slugify = Slugify(stop_words=['a', 'the']) self.assertEqual(slugify('A red apple'), 'red-apple') self.assertEqual(slugify('The4 red apple'), 'The4-red-apple') self.assertEqual(slugify('_The_red_the-apple'), 'red-apple') self.assertEqual(slugify('The__red_apple'), 'red-apple') slugify.safe_chars = '*' self.assertEqual(slugify('*The*red*apple'), '*-*red*apple') self.assertEqual(slugify('The**red*apple'), '**red*apple') slugify.stop_words = ['x', 'y'] self.assertEqual(slugify('x y n'), 'n')
def form_valid(self, form): form.instance.job_title = 'MG' property_id = create_unique_identifier() PropertyIdentifier.objects.create(identifier=property_id) form.instance.property_id = property_id property_name = form.cleaned_data['works_for'] c_slugify = Slugify(to_lower=True) property_slug = c_slugify(property_name) Hotel.objects.create(name=property_name, slug=property_slug, property_id=property_id) return super().form_valid(form)
def _add_slug(self, new_doc, title, lang): if title: collection = new_doc.get("collection", "") slug_parts = [] if collection in constants.SLUG_LANGUAGES_MAP: if lang in constants.SLUG_LANGUAGES_MAP[collection]: slug_collection = constants.SLUG_LANGUAGES_MAP[collection][lang] else: slug_collection = constants.SLUG_LANGUAGES_MAP[collection]["en"] else: slug_collection = None if new_doc["source"] != "clearmash" or slug_collection is None or lang not in ["en", "he"]: slug_parts.append(new_doc["source"]) if slug_collection: slug_parts.append(slug_collection) slug_parts.append(title.lower()) slugify = Slugify(translate=None, safe_chars='_') slug = slugify(u'_'.join([p.replace("_", "-") for p in slug_parts])) new_doc["slug_{}".format(lang)] = slug
def slugify(values, ensure_unique=False, **kwargs): """ Given a sequence of strings, returns a standardized version of the sequence. If ``ensure_unique`` is True, any duplicate strings will be appended with a unique identifier. Any kwargs will be passed to the Slugify or UniqueSlugify class constructor See: https://github.com/dimka665/awesome-slugify """ # Default to all lowercase slug_args = {'to_lower': True} slug_args.update(kwargs) if ensure_unique: custom_slugify = UniqueSlugify(**slug_args) else: custom_slugify = Slugify(**slug_args) return tuple(custom_slugify(value) for value in values)
def slugify(self): if (self.field_dict["product_name"] is not None and self.field_dict["brand"] is not None and self.field_dict["fingerprint"] is not None): key_word_list = self.keyword_list_lookup() pretranslate = load_slug_rule(self.language) slugify_url = Slugify(pretranslate=pretranslate) slugify_url.to_lower = True slugify_url.stop_words = () slugify_url.max_length = 1000 first_bit = slugify_url( "-".join(key_word for key_word in key_word_list if key_word).lower(), max_length=1000, ) if len(first_bit) == 0: return "" friendly_id = "-".join([first_bit, self.field_dict["fingerprint"]]) return friendly_id
def init_prometheus(self): self.slugify = Slugify(to_lower=True) self.slugify.separator = "_" self.slugify.pretranslate = {"+": "plus", "-": "minus"} for n, r in enumerate(registers): s = self.slugify(f"empro_{r['short_name']}") registers[n]["slug"] = s if s in self.metrics: print(s, r) if r["datatype"] in [ "uint8", "uint16", "sint16", "uint32", "sint32", "fl32", ]: self.metrics[s] = Gauge(s, r["description"], ["unit", "register"]) else: self.metrics[s] = Info(s, r["description"], ["register"])
def ajouter_article(): title = "Ajouter article" #Formulaire d'ajout des informations de l'article form = AjouterArticleForm() #Envoi du formulaire des informations if form.validate_on_submit(): if form.picture.data: #Enregistrement des infromation de la poste titre_cap = form.titre.data titre_slugify = Slugify(to_lower=True) imagefile_thumb = save_picture_thumb(form.picture.data) post = Contenu(titre=titre_cap.capitalize(), cont=form.cont.data, thumb=imagefile_thumb, slug=titre_slugify(form.titre.data), rub_cont=form.rubrique.data, cont_user=current_user) db.session.add(post) db.session.commit() flash('ajouter reussie', 'success') return redirect(url_for('posts.tous_articles')) return render_template('posts/ajouterpost.html', title=title, form=form)
def test_prevent_double_pretranslation(self): slugify = Slugify(pretranslate={'s': 'ss'}) self.assertEqual(slugify('BOOST'), 'BOOSST')