Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
cloudooo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
cloudooo
Commits
7658d236
Commit
7658d236
authored
Oct 11, 2016
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x2t: change y format document.docy = zip(body.txt + media/**)
parent
7b6a8e21
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
15 deletions
+75
-15
cloudooo/handler/x2t/handler.py
cloudooo/handler/x2t/handler.py
+40
-15
cloudooo/util.py
cloudooo/util.py
+35
-0
No files found.
cloudooo/handler/x2t/handler.py
View file @
7658d236
...
...
@@ -29,12 +29,13 @@ from xml.etree import ElementTree
from
subprocess
import
Popen
,
PIPE
from
tempfile
import
NamedTemporaryFile
,
mktemp
import
sys
import
os
from
zope.interface
import
implements
from
cloudooo.interfaces.handler
import
IHandler
from
cloudooo.file
import
File
from
cloudooo.util
import
logger
from
cloudooo.util
import
logger
,
zipTree
,
unzip
AVS_OFFICESTUDIO_FILE_UNKNOWN
=
"0"
AVS_OFFICESTUDIO_FILE_DOCUMENT_DOCX
=
"65"
...
...
@@ -60,6 +61,7 @@ format_code_map = {
"pptx"
:
AVS_OFFICESTUDIO_FILE_PRESENTATION_PPTX
,
}
yformat_tuple
=
(
"docy"
,
"xlsy"
,
"ppty"
)
class
Handler
(
object
):
"""ImageMagic Handler is used to handler images."""
...
...
@@ -78,23 +80,39 @@ class Handler(object):
self
.
base_folder_url
=
base_folder_url
self
.
file
=
File
(
base_folder_url
,
data
,
source_format
)
self
.
environment
=
kw
.
get
(
"env"
,
{})
#self.environment['LD_LIBRARY_PATH'] = converter_lib_dirname
def
convert
(
self
,
destination_format
=
None
,
**
kw
):
""" Convert the inputed file to output as format that were informed """
logger
.
debug
(
"x2t convert: %s > %s"
%
(
self
.
file
.
source_format
,
destination_format
))
source_format
=
self
.
file
.
source_format
logger
.
debug
(
"x2t convert: %s > %s"
%
(
source_format
,
destination_format
))
in_format
=
format_code_map
[
self
.
file
.
source_format
]
# init vars and xml configuration file
in_format
=
format_code_map
[
source_format
]
out_format
=
format_code_map
[
destination_format
]
root_dir
=
self
.
file
.
directory_name
input_dir
=
os
.
path
.
join
(
root_dir
,
"input"
);
output_dir
=
os
.
path
.
join
(
root_dir
,
"output"
);
final_file_name
=
os
.
path
.
join
(
root_dir
,
"document.%s"
%
destination_format
)
input_file_name
=
self
.
file
.
getUrl
()
output_file_name
=
final_file_name
config_file_name
=
os
.
path
.
join
(
root_dir
,
"config.xml"
)
if
source_format
in
yformat_tuple
:
os
.
mkdir
(
input_dir
)
unzip
(
self
.
file
.
getUrl
(),
input_dir
)
for
_
,
_
,
files
in
os
.
walk
(
input_dir
):
input_file_name
,
=
files
break
input_file_name
=
os
.
path
.
join
(
input_dir
,
input_file_name
)
if
destination_format
in
yformat_tuple
:
os
.
mkdir
(
output_dir
)
output_file_name
=
os
.
path
.
join
(
output_dir
,
"body.txt"
)
config_file
=
open
(
config_file_name
,
"w"
)
# create files in folder which will be trashed
output_file_name
=
mktemp
(
suffix
=
".%s"
%
destination_format
,
dir
=
self
.
file
.
directory_name
)
temp_xml
=
NamedTemporaryFile
(
suffix
=
".xml"
,
dir
=
self
.
file
.
directory_name
,
delete
=
False
)
# write xml configuration file
config
=
{
# 'm_sKey': 'from',
'm_sFileFrom'
:
self
.
file
.
getUrl
()
,
'm_sFileFrom'
:
input_file_name
,
'm_nFormatFrom'
:
in_format
,
'm_sFileTo'
:
output_file_name
,
'm_nFormatTo'
:
out_format
,
...
...
@@ -107,12 +125,12 @@ class Handler(object):
root
=
ElementTree
.
Element
(
'root'
)
for
key
,
value
in
config
.
items
():
ElementTree
.
SubElement
(
root
,
key
).
text
=
value
ElementTree
.
ElementTree
(
root
).
write
(
temp_xml
,
encoding
=
'utf-8'
,
xml_declaration
=
True
,
default_namespace
=
None
,
method
=
"xml"
)
temp_xml
.
close
()
ElementTree
.
ElementTree
(
root
).
write
(
config_file
,
encoding
=
'utf-8'
,
xml_declaration
=
True
,
default_namespace
=
None
,
method
=
"xml"
)
config_file
.
close
()
# run convertion binary
p
=
Popen
(
[
"x2t"
,
temp_xml
.
name
],
[
"x2t"
,
config_file
.
name
],
stdout
=
PIPE
,
stderr
=
PIPE
,
close_fds
=
True
,
...
...
@@ -120,9 +138,16 @@ class Handler(object):
)
stdout
,
stderr
=
p
.
communicate
()
if
p
.
returncode
!=
0
:
raise
RuntimeError
(
"x2t: exit code %d != 0
\
n
+ %s
\
n
> stdout: %s
\
n
> stderr: %s@ x2t xml:
\
n
%s"
%
(
p
.
returncode
,
" "
.
join
([
"x2t"
,
temp_xml
.
name
]),
stdout
,
stderr
,
" "
+
open
(
temp_xml
.
name
).
read
().
replace
(
"
\
n
"
,
"
\
n
"
)))
raise
RuntimeError
(
"x2t: exit code %d != 0
\
n
+ %s
\
n
> stdout: %s
\
n
> stderr: %s@ x2t xml:
\
n
%s"
%
(
p
.
returncode
,
" "
.
join
([
"x2t"
,
config_file
.
name
]),
stdout
,
stderr
,
" "
+
open
(
config_file
.
name
).
read
().
replace
(
"
\
n
"
,
"
\
n
"
)))
if
destination_format
in
yformat_tuple
:
zipTree
(
final_file_name
,
(
output_file_name
,
""
),
(
os
.
path
.
join
(
os
.
path
.
dirname
(
output_file_name
),
"media"
),
""
),
)
self
.
file
.
reload
(
output
_file_name
)
self
.
file
.
reload
(
final
_file_name
)
try
:
return
self
.
file
.
getContent
()
finally
:
...
...
cloudooo/util.py
View file @
7658d236
...
...
@@ -29,6 +29,8 @@
import
logging
import
mimetypes
import
pkg_resources
import
os
from
zipfile
import
ZipFile
,
ZIP_DEFLATED
logger
=
logging
.
getLogger
(
'Cloudooo'
)
...
...
@@ -98,3 +100,36 @@ def convertStringToBool(string):
return
False
else
:
return
None
def
zipTree
(
destination
,
*
tree_path_list
):
"""
destination may be a path or a StringIO
tree_path_list is a list that may contain a path or a couple(path, archive_root)
"""
def
archive
(
arg
,
archive_root
):
archive_name
=
os
.
path
.
join
(
archive_root
,
os
.
path
.
basename
(
arg
))
if
os
.
path
.
islink
(
arg
):
pass
# XXX logger.warn("zipTree: symlink %r ignored\n" % arg)
elif
os
.
path
.
isdir
(
arg
):
for
r
,
_
,
ff
in
os
.
walk
(
arg
):
zfile
.
write
(
r
,
archive_name
)
for
f
in
ff
:
archive
(
os
.
path
.
join
(
r
,
f
),
archive_name
)
elif
os
.
path
.
isfile
(
arg
):
zfile
.
write
(
arg
,
archive_name
)
else
:
pass
# XXX logger.warn("zipTree: unknown %r ignored\n" % arg)
zfile
=
ZipFile
(
destination
,
"w"
,
ZIP_DEFLATED
)
for
tree_path
in
tree_path_list
:
if
isinstance
(
tree_path
,
tuple
):
archive
(
*
tree_path
)
else
:
archive
(
tree_path
,
os
.
path
.
dirname
(
tree_path
))
zfile
.
close
()
return
destination
def
unzip
(
source
,
destination
):
zipfile
=
ZipFile
(
source
)
zipfile
.
extractall
(
destination
)
zipfile
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment