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
Model registry
Operate
Environments
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
tails
mat2
Commits
12b3b39d
Commit
12b3b39d
authored
7 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Add support for .odt
parent
0bbafc4c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/libreoffice.py
+54
-0
54 additions, 0 deletions
src/libreoffice.py
tests/data/dirty.odt
+0
-0
0 additions, 0 deletions
tests/data/dirty.odt
tests/test_libmat2.py
+25
-1
25 additions, 1 deletion
tests/test_libmat2.py
with
79 additions
and
1 deletion
src/libreoffice.py
0 → 100644
+
54
−
0
View file @
12b3b39d
import
re
import
subprocess
import
json
import
zipfile
import
tempfile
import
shutil
import
os
from
.
import
abstract
,
parser_factory
class
LibreOfficeParser
(
abstract
.
AbstractParser
):
mimetypes
=
{
'
application/vnd.oasis.opendocument.text
'
,
}
def
get_meta
(
self
):
"""
Yes, I know that parsing xml with regexp ain
'
t pretty,
be my guest and fix it if you want.
"""
metadata
=
{}
zipin
=
zipfile
.
ZipFile
(
self
.
filename
)
for
item
in
zipin
.
namelist
():
if
item
==
'
meta.xml
'
:
content
=
zipin
.
read
(
item
).
decode
(
'
utf-8
'
)
for
(
key
,
value
)
in
re
.
findall
(
r
"
<((?:meta|dc).+?)>(.+)</\1>
"
,
content
,
re
.
I
):
metadata
[
key
]
=
value
if
not
metadata
:
# better safe than sorry
metadata
[
item
]
=
'
harmful content
'
zipin
.
close
()
return
metadata
def
remove_all
(
self
):
zin
=
zipfile
.
ZipFile
(
self
.
filename
,
'
r
'
)
zout
=
zipfile
.
ZipFile
(
self
.
output_filename
,
'
w
'
)
temp_folder
=
tempfile
.
mkdtemp
()
for
item
in
zin
.
infolist
():
if
item
.
filename
[
-
1
]
==
'
/
'
:
continue
# `is_dir` is added in Python3.6
elif
item
.
filename
==
'
meta.xml
'
:
continue
# don't keep metadata files
zin
.
extract
(
member
=
item
,
path
=
temp_folder
)
tmp_parser
=
parser_factory
.
get_parser
(
os
.
path
.
join
(
temp_folder
,
item
.
filename
))
if
tmp_parser
is
None
:
print
(
"
%s isn
'
t supported
"
%
item
.
filename
)
continue
tmp_parser
.
remove_all
()
zout
.
write
(
tmp_parser
.
output_filename
,
item
.
filename
)
shutil
.
rmtree
(
temp_folder
)
zout
.
close
()
zin
.
close
()
return
True
This diff is collapsed.
Click to expand it.
tests/data/dirty.odt
0 → 100644
+
0
−
0
View file @
12b3b39d
File added
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
25
−
1
View file @
12b3b39d
...
...
@@ -4,7 +4,7 @@ import unittest
import
shutil
import
os
from
src
import
pdf
,
png
,
jpg
,
audio
,
office
from
src
import
pdf
,
png
,
jpg
,
audio
,
office
,
libreoffice
class
TestGetMeta
(
unittest
.
TestCase
):
def
test_pdf
(
self
):
...
...
@@ -46,6 +46,14 @@ class TestGetMeta(unittest.TestCase):
self
.
assertEqual
(
meta
[
'
dc:creator
'
],
'
julien voisin
'
)
self
.
assertEqual
(
meta
[
'
Application
'
],
'
LibreOffice/5.4.5.1$Linux_X86_64 LibreOffice_project/40m0$Build-1
'
)
def
test_libreoffice
(
self
):
p
=
libreoffice
.
LibreOfficeParser
(
'
./tests/data/dirty.odt
'
)
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
meta:initial-creator
'
],
'
jvoisin
'
)
self
.
assertEqual
(
meta
[
'
meta:creation-date
'
],
'
2011-07-26T03:27:48
'
)
self
.
assertEqual
(
meta
[
'
meta:generator
'
],
'
LibreOffice/3.3$Unix LibreOffice_project/330m19$Build-202
'
)
class
TestCleaning
(
unittest
.
TestCase
):
def
test_pdf
(
self
):
...
...
@@ -153,3 +161,19 @@ class TestCleaning(unittest.TestCase):
self
.
assertEqual
(
p
.
get_meta
(),
{})
os
.
remove
(
'
./tests/data/clean.docx
'
)
def
test_libreoffice
(
self
):
shutil
.
copy
(
'
./tests/data/dirty.odt
'
,
'
./tests/data/clean.odt
'
)
p
=
libreoffice
.
LibreOfficeParser
(
'
./tests/data/clean.odt
'
)
meta
=
p
.
get_meta
()
self
.
assertIsNotNone
(
meta
)
ret
=
p
.
remove_all
()
self
.
assertTrue
(
ret
)
p
=
libreoffice
.
LibreOfficeParser
(
'
./tests/data/clean.odt.cleaned
'
)
self
.
assertEqual
(
p
.
get_meta
(),
{})
os
.
remove
(
'
./tests/data/clean.odt
'
)
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