-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle things like URLs in Package ID when exporting ezEML data package.
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import re | ||
import unicodedata | ||
|
||
def sanitize_filename(value): | ||
""" | ||
Sanitize a string to make it safe for use as a filename. | ||
Parameters: | ||
value (str): The original string. | ||
Returns: | ||
str: A sanitized string safe for filenames. | ||
""" | ||
# Normalize the string to remove accents and special characters | ||
value = unicodedata.normalize('NFKD', value) | ||
value = value.encode('ascii', 'ignore').decode('ascii') | ||
|
||
# Replace invalid characters with an underscore | ||
value = re.sub(r'[<>:"/\\|?*]', '_', value) | ||
|
||
# Remove any remaining non-word characters (except spaces and hyphens) | ||
value = re.sub(r'[^\w\s-]', '', value) | ||
|
||
# Replace spaces and hyphens with a single underscore | ||
value = re.sub(r'[\s-]+', '_', value) | ||
|
||
# Trim leading and trailing underscores | ||
value = value.strip('_') | ||
|
||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters