2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Remove regex import from helpers.py

Ref: https://github.com/inventree/InvenTree/pull/8547
This commit is contained in:
Oliver Walters 2024-11-30 00:22:17 +00:00
parent d0b87a1a12
commit 44c1046a43

View File

@ -3,7 +3,6 @@
import datetime import datetime
import hashlib import hashlib
import io import io
import json
import logging import logging
import os import os
import os.path import os.path
@ -23,12 +22,9 @@ from django.utils.translation import gettext_lazy as _
import bleach import bleach
import pytz import pytz
import regex
from bleach import clean from bleach import clean
from djmoney.money import Money from djmoney.money import Money
from PIL import Image
import InvenTree.version
from common.currency import currency_code_default from common.currency import currency_code_default
from .settings import MEDIA_URL, STATIC_URL from .settings import MEDIA_URL, STATIC_URL
@ -144,6 +140,8 @@ def getStaticUrl(filename):
def TestIfImage(img): def TestIfImage(img):
"""Test if an image file is indeed an image.""" """Test if an image file is indeed an image."""
from PIL import Image
try: try:
Image.open(img).verify() Image.open(img).verify()
return True return True
@ -785,28 +783,22 @@ def strip_html_tags(value: str, raise_error=True, field_name=None):
return cleaned return cleaned
def remove_non_printable_characters( def remove_non_printable_characters(value: str, remove_newline=True) -> str:
value: str, remove_newline=True, remove_ascii=True, remove_unicode=True
):
"""Remove non-printable / control characters from the provided string.""" """Remove non-printable / control characters from the provided string."""
cleaned = value cleaned = value
if remove_ascii: # Remove ASCII control characters
# Remove ASCII control characters # Note that we do not sub out 0x0A (\n) here, it is done separately below
# Note that we do not sub out 0x0A (\n) here, it is done separately below regex = re.compile(r'[\u0000-\u0009\u000B-\u001F\u007F-\u009F]')
cleaned = regex.sub('[\x00-\x09]+', '', cleaned) cleaned = regex.sub('', cleaned)
cleaned = regex.sub('[\x0b-\x1f\x7f]+', '', cleaned)
# Remove Unicode control characters
regex = re.compile(r'[\u200E\u200F\u202A-\u202E]')
cleaned = regex.sub('', cleaned)
if remove_newline: if remove_newline:
cleaned = regex.sub('[\x0a]+', '', cleaned) regex = re.compile(r'[\x0A]')
cleaned = regex.sub('', cleaned)
if remove_unicode:
# Remove Unicode control characters
if remove_newline:
cleaned = regex.sub('[^\P{C}]+', '', cleaned)
else:
# Use 'negative-lookahead' to exclude newline character
cleaned = regex.sub('(?![\x0a])[^\P{C}]+', '', cleaned)
return cleaned return cleaned