Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Romain
mat2
Commits
61dce89f
Commit
61dce89f
authored
4 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Raise a ValueError explicitly
parent
88b7ec2c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
libmat2/parser_factory.py
+6
-7
6 additions, 7 deletions
libmat2/parser_factory.py
mat2
+10
-2
10 additions, 2 deletions
mat2
tests/test_corrupted_files.py
+14
-12
14 additions, 12 deletions
tests/test_corrupted_files.py
with
30 additions
and
21 deletions
libmat2/parser_factory.py
+
6
−
7
View file @
61dce89f
...
...
@@ -40,7 +40,10 @@ def _get_parsers() -> List[T]:
def
get_parser
(
filename
:
str
)
->
Tuple
[
Optional
[
T
],
Optional
[
str
]]:
"""
Return the appropriate parser for a given filename.
"""
"""
Return the appropriate parser for a given filename.
:raises ValueError: Raised if the instantiation of the parser went wrong.
"""
mtype
,
_
=
mimetypes
.
guess_type
(
filename
)
_
,
extension
=
os
.
path
.
splitext
(
filename
)
...
...
@@ -53,10 +56,6 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
for
parser_class
in
_get_parsers
():
# type: ignore
if
mtype
in
parser_class
.
mimetypes
:
try
:
return
parser_class
(
filename
),
mtype
except
ValueError
as
e
:
logging
.
info
(
"
Got an exception when trying to instantiate
"
"
%s for %s: %s
"
,
parser_class
,
filename
,
e
)
return
None
,
mtype
# This instantiation might raise a ValueError on malformed files
return
parser_class
(
filename
),
mtype
return
None
,
mtype
This diff is collapsed.
Click to expand it.
mat2
+
10
−
2
View file @
61dce89f
...
...
@@ -85,7 +85,11 @@ def show_meta(filename: str, sandbox: bool):
if
not
__check_file
(
filename
):
return
p
,
mtype
=
parser_factory
.
get_parser
(
filename
)
# type: ignore
try
:
p
,
mtype
=
parser_factory
.
get_parser
(
filename
)
# type: ignore
except
ValueError
as
e
:
print
(
"
[-] something went wrong when processing %s: %s
"
%
(
filename
,
e
))
return
if
p
is
None
:
print
(
"
[-] %s
'
s format (%s) is not supported
"
%
(
filename
,
mtype
))
return
...
...
@@ -126,7 +130,11 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool
if
not
__check_file
(
filename
,
mode
):
return
False
p
,
mtype
=
parser_factory
.
get_parser
(
filename
)
# type: ignore
try
:
p
,
mtype
=
parser_factory
.
get_parser
(
filename
)
# type: ignore
except
ValueError
as
e
:
print
(
"
[-] something went wrong when cleaning %s: %s
"
%
(
filename
,
e
))
return
False
if
p
is
None
:
print
(
"
[-] %s
'
s format (%s) is not supported
"
%
(
filename
,
mtype
))
return
False
...
...
This diff is collapsed.
Click to expand it.
tests/test_corrupted_files.py
+
14
−
12
View file @
61dce89f
...
...
@@ -65,8 +65,10 @@ class TestCorruptedEmbedded(unittest.TestCase):
def
test_docx
(
self
):
shutil
.
copy
(
'
./tests/data/embedded_corrupted.docx
'
,
'
./tests/data/clean.docx
'
)
parser
,
_
=
parser_factory
.
get_parser
(
'
./tests/data/clean.docx
'
)
self
.
assertFalse
(
parser
.
remove_all
())
self
.
assertIsNotNone
(
parser
.
get_meta
())
with
self
.
assertRaises
(
ValueError
):
parser
.
remove_all
()
with
self
.
assertRaises
(
ValueError
):
self
.
assertIsNotNone
(
parser
.
get_meta
())
os
.
remove
(
'
./tests/data/clean.docx
'
)
def
test_odt
(
self
):
...
...
@@ -120,8 +122,8 @@ class TestCorruptedFiles(unittest.TestCase):
def
test_png2
(
self
):
shutil
.
copy
(
'
./tests/test_libmat2.py
'
,
'
./tests/clean.png
'
)
parser
,
_
=
parser_factory
.
get_parser
(
'
./tests/clean.png
'
)
self
.
assertIsNone
(
parser
)
with
self
.
assertRaises
(
ValueError
):
parser_factory
.
get_parser
(
'
./tests/clean.png
'
)
os
.
remove
(
'
./tests/clean.png
'
)
def
test_torrent
(
self
):
...
...
@@ -237,10 +239,10 @@ class TestCorruptedFiles(unittest.TestCase):
zout
.
write
(
'
./tests/data/embedded_corrupted.docx
'
)
p
,
mimetype
=
parser_factory
.
get_parser
(
'
./tests/data/clean.zip
'
)
self
.
assertEqual
(
mimetype
,
'
application/zip
'
)
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
tests/data/dirty.flac
'
][
'
comments
'
],
'
Thank you for using MAT !
'
)
self
.
assert
Equal
(
meta
[
'
tests/data/dirty.docx
'
][
'
word/media/image1.png
'
][
'
Comment
'
],
'
This is a comment, be careful!
'
)
self
.
assertFalse
(
p
.
remove_all
())
with
self
.
assertRaises
(
ValueError
):
p
.
get_meta
(
)
with
self
.
assert
Raises
(
ValueError
):
self
.
assertFalse
(
p
.
remove_all
())
os
.
remove
(
'
./tests/data/clean.zip
'
)
def
test_html
(
self
):
...
...
@@ -315,10 +317,10 @@ class TestCorruptedFiles(unittest.TestCase):
zout
.
addfile
(
tarinfo
,
f
)
p
,
mimetype
=
parser_factory
.
get_parser
(
'
./tests/data/clean.tar
'
)
self
.
assertEqual
(
mimetype
,
'
application/x-tar
'
)
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
./tests/data/dirty.flac
'
][
'
comments
'
],
'
Thank you for using MAT !
'
)
self
.
assert
Equal
(
meta
[
'
./tests/data/dirty.docx
'
][
'
word/media/image1.png
'
][
'
Comment
'
],
'
This is a comment, be careful!
'
)
self
.
assertFalse
(
p
.
remove_all
())
with
self
.
assertRaises
(
ValueError
):
p
.
get_meta
(
)
with
self
.
assert
Raises
(
ValueError
):
self
.
assertFalse
(
p
.
remove_all
())
os
.
remove
(
'
./tests/data/clean.tar
'
)
shutil
.
copy
(
'
./tests/data/dirty.png
'
,
'
./tests/data/clean.tar
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment