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
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
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
jvoisin
mat2
Commits
080d6769
Commit
080d6769
authored
6 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Make pylint even happier
parent
86fe3aa5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
libmat2/office.py
+26
-21
26 additions, 21 deletions
libmat2/office.py
with
26 additions
and
21 deletions
libmat2/office.py
+
26
−
21
View file @
080d6769
...
...
@@ -20,18 +20,18 @@ assert Pattern
def
_parse_xml
(
full_path
:
str
):
"""
This function parse XML with namespace support.
"""
def
parse_map
(
f
):
# etree support for ns is a bit rough
n
s
_map
=
dict
()
for
_
,
(
k
,
v
)
in
ET
.
iterparse
(
f
,
(
"
start-ns
"
,
)):
n
s
_map
[
k
]
=
v
return
n
s
_map
n
amespace
_map
=
dict
()
for
_
,
(
k
ey
,
v
alue
)
in
ET
.
iterparse
(
f
,
(
"
start-ns
"
,
)):
n
amespace
_map
[
k
ey
]
=
v
alue
return
n
amespace
_map
n
s
=
parse_map
(
full_path
)
n
amespace_map
=
parse_map
(
full_path
)
# Register the namespaces
for
k
,
v
in
n
s
.
items
():
ET
.
register_namespace
(
k
,
v
)
for
k
ey
,
v
alue
in
n
amespace_map
.
items
():
ET
.
register_namespace
(
k
ey
,
v
alue
)
return
ET
.
parse
(
full_path
),
n
s
return
ET
.
parse
(
full_path
),
n
amespace_map
class
ArchiveBasedAbstractParser
(
abstract
.
AbstractParser
):
...
...
@@ -53,15 +53,18 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser):
def
_specific_cleanup
(
self
,
full_path
:
str
)
->
bool
:
"""
This method can be used to apply specific treatment
to files present in the archive.
"""
# pylint: disable=unused-argument,no-self-use
return
True
# pragma: no cover
def
_clean_zipinfo
(
self
,
zipinfo
:
zipfile
.
ZipInfo
)
->
zipfile
.
ZipInfo
:
@staticmethod
def
_clean_zipinfo
(
zipinfo
:
zipfile
.
ZipInfo
)
->
zipfile
.
ZipInfo
:
zipinfo
.
create_system
=
3
# Linux
zipinfo
.
comment
=
b
''
zipinfo
.
date_time
=
(
1980
,
1
,
1
,
0
,
0
,
0
)
return
zipinfo
def
_get_zipinfo_meta
(
self
,
zipinfo
:
zipfile
.
ZipInfo
)
->
Dict
[
str
,
str
]:
@staticmethod
def
_get_zipinfo_meta
(
zipinfo
:
zipfile
.
ZipInfo
)
->
Dict
[
str
,
str
]:
metadata
=
{}
if
zipinfo
.
create_system
==
3
:
#metadata['create_system'] = 'Linux'
...
...
@@ -142,29 +145,30 @@ class MSOfficeParser(ArchiveBasedAbstractParser):
'
^docProps/
'
,
}))
def
__remove_revisions
(
self
,
full_path
:
str
)
->
bool
:
@staticmethod
def
__remove_revisions
(
full_path
:
str
)
->
bool
:
"""
In this function, we
'
re changing the XML
document in two times, since we don
'
t want
to change the tree we
'
re iterating on.
"""
try
:
tree
,
n
s
=
_parse_xml
(
full_path
)
tree
,
n
amespace
=
_parse_xml
(
full_path
)
except
ET
.
ParseError
:
return
False
# No revisions are present
del_presence
=
tree
.
find
(
'
.//w:del
'
,
n
s
)
ins_presence
=
tree
.
find
(
'
.//w:ins
'
,
n
s
)
del_presence
=
tree
.
find
(
'
.//w:del
'
,
n
amespace
)
ins_presence
=
tree
.
find
(
'
.//w:ins
'
,
n
amespace
)
if
del_presence
is
None
and
ins_presence
is
None
:
return
True
parent_map
=
{
c
:
p
for
p
in
tree
.
iter
()
for
c
in
p
}
elements
=
list
([
element
for
element
in
tree
.
iterfind
(
'
.//w:del
'
,
n
s
)])
elements
=
list
([
element
for
element
in
tree
.
iterfind
(
'
.//w:del
'
,
n
amespace
)])
for
element
in
elements
:
parent_map
[
element
].
remove
(
element
)
elements
=
list
()
for
element
in
tree
.
iterfind
(
'
.//w:ins
'
,
n
s
):
for
element
in
tree
.
iterfind
(
'
.//w:ins
'
,
n
amespace
):
for
position
,
item
in
enumerate
(
tree
.
iter
()):
#pragma: no cover
if
item
==
element
:
for
children
in
element
.
iterfind
(
'
./*
'
):
...
...
@@ -231,17 +235,18 @@ class LibreOfficeParser(ArchiveBasedAbstractParser):
}))
def
__remove_revisions
(
self
,
full_path
:
str
)
->
bool
:
@staticmethod
def
__remove_revisions
(
full_path
:
str
)
->
bool
:
try
:
tree
,
n
s
=
_parse_xml
(
full_path
)
tree
,
n
amespace
=
_parse_xml
(
full_path
)
except
ET
.
ParseError
:
return
False
if
'
office
'
not
in
n
s
.
keys
():
# no revisions in the current file
if
'
office
'
not
in
n
amespace
.
keys
():
# no revisions in the current file
return
True
for
text
in
tree
.
getroot
().
iterfind
(
'
.//office:text
'
,
n
s
):
for
changes
in
text
.
iterfind
(
'
.//text:tracked-changes
'
,
n
s
):
for
text
in
tree
.
getroot
().
iterfind
(
'
.//office:text
'
,
n
amespace
):
for
changes
in
text
.
iterfind
(
'
.//text:tracked-changes
'
,
n
amespace
):
text
.
remove
(
changes
)
tree
.
write
(
full_path
,
xml_declaration
=
True
)
...
...
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