Skip to content
GitLab
Menu
Why GitLab
Pricing
Contact Sales
Explore
Why GitLab
Pricing
Contact Sales
Explore
Sign in
Get free trial
Primary navigation
Search or go to…
Project
S
sanitize-filename
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Privacy statement
Keyboard shortcuts
?
What's new
4
Snippets
Groups
Projects
Show more breadcrumbs
jplusplus
sanitize-filename
Compare revisions
1.0.1 to 1.1.0
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
jplusplus/sanitize-filename
Select target project
No results found
1.1.0
Select Git revision
Branches
master
Tags
1.0.0
1.0.0-dev2
1.0.0-dev3
1.0.0.dev1
1.0.1
1.1.0
1.2.0
Swap
Target
jplusplus/sanitize-filename
Select target project
jplusplus/sanitize-filename
laggykiller/sanitize-filename
2 results
1.0.1
Select Git revision
Branches
master
Tags
1.0.0
1.0.0-dev2
1.0.0-dev3
1.0.0.dev1
1.0.1
1.1.0
1.2.0
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (3)
changelog
· 73b4478b
Leo Wallentin
authored
5 years ago
73b4478b
try preserving extensions
· 9b5c39fb
Leo Wallentin
authored
5 years ago
9b5c39fb
1.1.0
· beb4b8e0
Leo Wallentin
authored
5 years ago
beb4b8e0
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.md
+8
-0
8 additions, 0 deletions
README.md
sanitize_filename/sanitize_filename.py
+9
-2
9 additions, 2 deletions
sanitize_filename/sanitize_filename.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
test/test_sanitize.py
+13
-0
13 additions, 0 deletions
test/test_sanitize.py
with
31 additions
and
3 deletions
README.md
View file @
beb4b8e0
...
...
@@ -39,6 +39,14 @@ Examples:
## Changelog
-
1.1.0
-
Try to preserve filename extensions if possible
-
1.0.1
-
First release (as 1.0.1 due to a version number mix-up in 1.0.0)
-
1.0.0-dev3
-
Black list low code point characters (<32)
...
...
This diff is collapsed.
Click to expand it.
sanitize_filename/sanitize_filename.py
View file @
beb4b8e0
"""
A permissive filename sanitizer.
"""
import
unicodedata
from
os
import
path
def
sanitize
(
filename
):
...
...
@@ -29,6 +30,12 @@ def sanitize(filename):
if
len
(
filename
)
==
0
:
filename
=
"
__
"
if
len
(
filename
)
>
255
:
filename
=
filename
[:
255
]
filename
=
filename
.
rstrip
(
"
.
"
)
# Re-check last character
(
base
,
ext
)
=
path
.
splitext
(
filename
)
if
len
(
ext
)
>
254
:
ext
=
ext
[
254
:]
maxl
=
255
-
len
(
ext
)
filename
=
filename
[:
maxl
]
filename
=
filename
+
ext
# Re-check last character (if there was no extension)
filename
=
filename
.
rstrip
(
"
.
"
)
return
filename
This diff is collapsed.
Click to expand it.
setup.py
View file @
beb4b8e0
...
...
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools
.
setup
(
name
=
"
sanitize_filename
"
,
version
=
"
1.
0.1
"
,
version
=
"
1.
1.0
"
,
author
=
"
Leo Wallentin | J++ Stockholm
"
,
author_email
=
"
mejl@leowallentin.se
"
,
description
=
"
A permissive filename sanitizer.
"
,
...
...
This diff is collapsed.
Click to expand it.
test/test_sanitize.py
View file @
beb4b8e0
...
...
@@ -12,6 +12,7 @@ def test_invalid_suffix():
"""
Dots are not allowed at the end.
"""
assert
(
sanitize
(
"
def.
"
)
==
"
def
"
)
assert
(
sanitize
(
"
def.ghi
"
)
==
"
def.ghi
"
)
assert
(
sanitize
(
"
X
"
*
1000
+
"
.
"
).
endswith
(
"
X
"
))
def
test_reserved_words
():
...
...
@@ -23,8 +24,20 @@ def test_reserved_words():
def
test_long_names
():
"""
Make sure long names are truncated.
"""
assert
(
len
(
sanitize
(
"
X
"
*
300
))
==
255
)
assert
(
len
(
sanitize
(
"
.
"
.
join
([
"
X
"
*
100
,
"
X
"
*
100
,
"
X
"
*
100
])))
==
255
)
assert
(
len
(
sanitize
(
"
.
"
.
join
([
"
X
"
*
300
,
"
X
"
*
300
,
"
X
"
*
300
])))
==
255
)
def
test_unicode_normalization
():
"""
Names should be NFKD normalized.
"""
assert
(
sanitize
(
"
ў
"
)
==
chr
(
1091
)
+
chr
(
774
))
def
test_extensions
():
"""
Filename extensions should be preserved when possible.
"""
really_long_name
=
"
X
"
*
1000
+
"
.pdf
"
assert
(
sanitize
(
really_long_name
).
endswith
(
"
.pdf
"
))
assert
(
sanitize
(
"
X
"
*
1000
).
endswith
(
"
X
"
))
assert
(
sanitize
(
"
X
"
*
100
+
"
.
"
+
"
X
"
*
100
+
"
.pdf
"
).
endswith
(
"
.pdf
"
))
assert
(
sanitize
(
"
X
"
*
100
+
"
.
"
+
"
X
"
*
400
).
endswith
(
"
X
"
))
assert
(
sanitize
(
"
X
"
*
100
+
"
.
"
+
"
X
"
*
400
+
"
.pdf
"
).
endswith
(
"
.pdf
"
))
This diff is collapsed.
Click to expand it.