Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated clean_filename and clean_timestamp utilities #1820

Merged
merged 2 commits into from
Jan 24, 2025
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
103 changes: 0 additions & 103 deletions traits/util/clean_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,110 +13,7 @@
"""

# Standard library imports.
import copy
import datetime
import keyword
import re
import unicodedata
import warnings


def clean_filename(name, replace_empty=""):
"""
Make a user-supplied string safe for filename use.

Returns an ASCII-encodable string based on the input string that's safe for
use as a component of a filename or URL. The returned value is a string
containing only lowercase ASCII letters, digits, and the characters '-' and
'_'.

This does not give a faithful representation of the original string:
different input strings can result in the same output string.

.. deprecated:: 6.3.0
This function will be removed in a future version of Traits.

Parameters
----------
name : str
The string to be made safe.
replace_empty : str, optional
The return value to be used in the event that the sanitised
string ends up being empty. No validation is done on this
input - it's up to the user to ensure that the default is
itself safe. The default is to return the empty string.

Returns
-------
safe_string : str
A filename-safe version of string.

"""
warnings.warn(
"clean_filename is deprecated and will eventually be removed",
DeprecationWarning,
stacklevel=2,
)

# Code is based on Django's slugify utility.
# https://docs.djangoproject.com/en/1.9/_modules/django/utils/text/#slugify
name = (
unicodedata.normalize('NFKD', name)
.encode('ascii', 'ignore')
.decode('ascii')
)
name = re.sub(r'[^\w\s-]', '', name).strip().lower()
safe_name = re.sub(r'[-\s]+', '-', name)
if safe_name == "":
return replace_empty
return safe_name


def clean_timestamp(dt=None, microseconds=False):
"""
Return a timestamp that has been cleansed of characters that might
cause problems in filenames, namely colons. If no datetime object
is provided, then uses the current time.

The timestamp is in ISO-8601 format with the following exceptions:

* Colons ':' are replaced by underscores '_'.
* Microseconds are not displayed if the 'microseconds' parameter is
False.

.. deprecated:: 6.3.0
This function will be removed in a future version of Traits.

Parameters
----------
dt : None or datetime.datetime
If None, then the current time is used.
microseconds : bool
Display microseconds or not.

Returns
-------
A string timestamp.
"""
warnings.warn(
"clean_timestamp is deprecated and will eventually be removed",
DeprecationWarning,
stacklevel=2,
)

if dt is None:
dt = datetime.datetime.now()
else:
# Operate on a copy.
dt = copy.copy(dt)

if not microseconds:
# The microseconds are largely uninformative but annoying.
dt = dt.replace(microsecond=0)

stamp = dt.isoformat().replace(":", "_")

return stamp


def python_name(name):
Expand Down
89 changes: 0 additions & 89 deletions traits/util/tests/test_clean_strings.py

This file was deleted.

Loading