| author | gibus |
| Thu, 21 Jul 2011 15:02:50 +0200 | |
| changeset 365 | a478cb9786fd |
| parent 361 | 5f2a1237050a |
| child 366 | 98af3be91847 |
| permissions | -rw-r--r-- |
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
1 |
import os |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
2 |
import tempfile |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
3 |
import re |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
4 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
5 |
import pexpect |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
6 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
7 |
from abi_error import AbiConverterError, AbiCommandError |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
8 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
9 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
10 |
TYPES_IN = {'602': '602', 'abw': 'abw', 'aw': 'aw', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
11 |
'awt': 'awt', 'cwk': 'cwk', 'dbk': 'dbk', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
12 |
'doc': 'doc', 'docm': 'docm', 'docx': 'docx', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
13 |
'dot': 'dot', 'dotm': 'dotm', 'dotx': 'dotx', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
14 |
'fo': 'fo', 'htm': 'htm', 'html': 'html', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
15 |
'hwp': 'hwp', 'isc': 'isc', 'iscii': 'iscii', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
16 |
'kwd': 'kwd', 'mif': 'mif', 'odt': 'odt', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
17 |
'opml': 'opml', 'ott': 'ott', 'pdb': 'pdb', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
18 |
'pdf': 'pdf', 'rtf': 'rtf', 'sdw': 'sdw', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
19 |
'stw': 'stw', 'sxw': 'sxw', 'text': 'text', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
20 |
'txt': 'txt', 'wml': 'wml', 'wp': 'wp', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
21 |
'wpd': 'wpd', 'wri': 'wri', 'xhtml': 'xhtml', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
22 |
'xml': 'xml', 'zabw': 'zabw'} |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
23 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
24 |
TYPES_OUT = {'abw': 'abw', 'aw': 'aw', 'awt': 'awt', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
25 |
'dbk': 'dbk', 'doc': 'doc', 'eml': 'eml', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
26 |
'fo': 'fo', 'html': 'html', 'isc': 'isc', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
27 |
'iscii': 'iscii', 'kwd': 'kwd', 'latex': 'latex', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
28 |
'mht': 'mht', 'mif': 'mif', 'nroff': 'nroff', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
29 |
'nws': 'nws', 'odt': 'odt', 'pdb': 'pdb', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
30 |
'pdf': 'pdf', 'ps': 'ps', 'rtf': 'rtf', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
31 |
'sxw': 'sxw', 'text': 'text', 'txt': 'txt', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
32 |
'wml': 'wml', 'xml': 'xml', 'xml2ps': 'xml2ps', |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
33 |
'zabw': 'zabw'} |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
34 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
35 |
class AbiFileConverter(object): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
36 |
"""This let's you convert between all filetypes supperted by the |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
37 |
AbiWord program. Import type isn't checked, as AbiWord doesn't check |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
38 |
on extension, but on metadata. |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
39 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
40 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
41 |
def __init__(self, timeout=60): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
42 |
self.id = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
43 |
self.timeout = timeout |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
44 |
self._start_abiword() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
45 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
46 |
def _start_abiword(self): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
47 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
48 |
Start abiword with the AbiCommand plugin, if not already started |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
49 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
50 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
51 |
# find the abiword executable |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
52 |
abicommand = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
53 |
for dir in os.environ['PATH'].split(':'): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
54 |
if os.path.isfile(os.path.join(dir, 'abiword')): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
55 |
abicommand = os.path.join(dir, 'abiword') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
56 |
if not abicommand: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
57 |
raise AbiConverterError('Can not find abiword executable') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
58 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
59 |
# start the abiword executable |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
60 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
61 |
self.child = pexpect.spawn(abicommand + ' --plugin AbiCommand') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
62 |
self.child.expect( |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
63 |
'AbiWord command line plugin: Type "quit" to exit', 10) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
64 |
except: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
65 |
raise AbiConverterError('Can not open abiword executable') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
66 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
67 |
def stop_abiword(self): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
68 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
69 |
Stop the running abiword, kill it if necessary |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
70 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
71 |
self.child.sendline('quit') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
72 |
if self._is_running(): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
73 |
os.kill(self.child.pid, 9) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
74 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
75 |
def _is_running(self): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
76 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
77 |
Test to see if abiword is running |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
78 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
79 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
80 |
self.child.sendline('writepid /dev/null') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
81 |
self.child.expect('OK', 1) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
82 |
return True |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
83 |
except: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
84 |
return False |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
85 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
86 |
def convert_file(self, in_file, out_file=None, type=None): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
87 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
88 |
Convert a file. If out_file is not specified, a byte string is |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
89 |
returned. If type is not specified, the file extension from out_file is |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
90 |
used to determine the type. If this fails, the type 'text' is used. |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
91 |
Return value is -1 if an error occurred. |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
92 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
93 |
# is the out_file specified? |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
94 |
return_bytes = False |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
95 |
if out_file is None: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
96 |
out_file = tempfile.mktemp(prefix="abiconvert_") |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
97 |
return_bytes = True |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
98 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
99 |
# is the type specified |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
100 |
type = TYPES_OUT.get( |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
101 |
type or os.path.splitext(out_file)[1][1:], 'txt') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
102 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
103 |
# do the coversion |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
104 |
self._perform_conversion(in_file, out_file, type) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
105 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
106 |
# return a byte string if no out_file is specified |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
107 |
if return_bytes: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
108 |
fp = open(out_file, 'r') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
109 |
bytes = fp.read() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
110 |
fp.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
111 |
os.remove(out_file) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
112 |
return bytes |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
113 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
114 |
def _perform_conversion(self, in_file, out_file, type): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
115 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
116 |
Do the actual conversion |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
117 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
118 |
# make sure we are up and running |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
119 |
if not self._is_running: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
120 |
self._start_abiword() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
121 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
122 |
# convert the file |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
123 |
cmd = 'convert %s %s %s' % (os.path.abspath(in_file), |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
124 |
os.path.abspath(out_file), type) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
125 |
self.child.sendline(cmd) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
126 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
127 |
# Check for errors |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
128 |
i = self.child.expect(['OK', pexpect.TIMEOUT]) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
129 |
if i != 0: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
130 |
raise AbiCommandError('Error performing AbiCommand: %s' %cmd) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
131 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
132 |
def convert_to_html(self, input): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
133 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
134 |
Convert input file to HTML |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
135 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
136 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
137 |
from tempfile import mkstemp,mkdtemp |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
138 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
139 |
THE_OUTDIR = "outdir" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
140 |
THE_OUTFILE = "outfile" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
141 |
THE_INDIR = "indir" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
142 |
THE_INFILE = "infile" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
143 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
144 |
infile = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
145 |
outfile = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
146 |
out_f = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
147 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
148 |
# create in/out files |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
149 |
temp_dir = mkdtemp(prefix="cm_") |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
150 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
151 |
# in |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
152 |
indir_name = os.path.join(temp_dir, THE_INDIR) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
153 |
os.mkdir(indir_name) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
154 |
infile_name = os.path.join(indir_name, THE_INFILE) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
155 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
156 |
# out |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
157 |
outdir_name = os.path.join(temp_dir, THE_OUTDIR) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
158 |
os.mkdir(outdir_name) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
159 |
outfile_name = os.path.join(outdir_name, THE_OUTFILE) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
160 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
161 |
# write infile |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
162 |
infile = open(infile_name,'w') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
163 |
if type(input) == unicode: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
164 |
input = input.encode('utf8') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
165 |
infile.write(input) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
166 |
infile.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
167 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
168 |
# fix perms |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
169 |
# TODO group permission should suffice |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
170 |
os.chmod(temp_dir, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
171 |
os.chmod(indir_name, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
172 |
os.chmod(infile_name, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
173 |
os.chmod(outdir_name, 0777) # read / write |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
174 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
175 |
# Do the job |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
176 |
self.convert_file(infile_name, outfile_name, 'html') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
177 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
178 |
out_f = open(outfile_name,'r') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
179 |
output = out_f.read() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
180 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
181 |
# load other files (useful only for html) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
182 |
img_res = [] |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
183 |
if os.path.isdir(outdir_name + '/' + THE_OUTFILE + '_files'): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
184 |
image_names = [name for name in os.listdir(outdir_name + '/' + THE_OUTFILE + '_files') if name != THE_OUTFILE] |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
185 |
for image_name in image_names: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
186 |
img_res.append(os.path.join(outdir_name + '/' + THE_OUTFILE + '_files', image_name)) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
187 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
188 |
# clean images paths |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
189 |
output = re.sub(r'<img(.+src=")outfile_files/([^"]+")', r'<img\1\2', output); |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
190 |
output = re.sub(r'<img(.+)style="width:[\d\.]+mm"', r'<img\1', output); |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
191 |
return output,img_res |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
192 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
193 |
finally: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
194 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
195 |
if out_f: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
196 |
out_f.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
197 |
if infile: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
198 |
infile.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
199 |
except: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
200 |
pass |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
201 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
202 |
def convert_from_html(self, input, format): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
203 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
204 |
Convert input file from HTML |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
205 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
206 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
207 |
from tempfile import mkstemp,mkdtemp |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
208 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
209 |
THE_OUTDIR = "outdir" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
210 |
THE_OUTFILE = "outfile" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
211 |
THE_INDIR = "indir" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
212 |
THE_INFILE = "infile" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
213 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
214 |
infile = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
215 |
outfile = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
216 |
out_f = None |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
217 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
218 |
# create in/out files |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
219 |
temp_dir = mkdtemp(prefix="cm_") |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
220 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
221 |
# in |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
222 |
indir_name = os.path.join(temp_dir, THE_INDIR) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
223 |
os.mkdir(indir_name) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
224 |
infile_name = os.path.join(indir_name, THE_INFILE + '.html') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
225 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
226 |
# out |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
227 |
outdir_name = os.path.join(temp_dir, THE_OUTDIR) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
228 |
os.mkdir(outdir_name) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
229 |
outfile_name = os.path.join(outdir_name, THE_OUTFILE) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
230 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
231 |
# write infile |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
232 |
infile = open(infile_name,'w') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
233 |
if type(input) == unicode: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
234 |
input = input.encode('utf8') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
235 |
infile.write(input) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
236 |
infile.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
237 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
238 |
# fix perms |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
239 |
# TODO group permission should suffice |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
240 |
os.chmod(temp_dir, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
241 |
os.chmod(indir_name, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
242 |
os.chmod(infile_name, 0755) # read |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
243 |
os.chmod(outdir_name, 0777) # read / write |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
244 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
245 |
# Do the job |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
246 |
self.convert_file(infile_name, outfile_name, format) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
247 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
248 |
out_f = open(outfile_name,'r') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
249 |
output = out_f.read() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
250 |
return output |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
251 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
252 |
finally: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
253 |
try: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
254 |
if out_f: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
255 |
out_f.close() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
256 |
if infile: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
257 |
infile.close() |
| 361 | 258 |
top = temp_dir |
|
365
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
259 |
#for root, dirs, files in os.walk(top, topdown=False): |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
260 |
# for name in files: |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
261 |
# os.remove(os.path.join(root, name)) |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
262 |
# for name in dirs: |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
263 |
# os.rmdir(os.path.join(root, name)) |
|
a478cb9786fd
For some reasons, abiwords can read background style attribute but not background-color
gibus
parents:
361
diff
changeset
|
264 |
#os.rmdir(top) |
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
265 |
except: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
266 |
pass |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
267 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
268 |
def add_html_header(self, body): |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
269 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
270 |
Add an HTML header to an HTML body |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
271 |
""" |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
272 |
|
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
273 |
return """ |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
274 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
275 |
<head> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
276 |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
277 |
</head> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
278 |
<body> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
279 |
%s |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
280 |
</body> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
281 |
</html> |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
282 |
""" %body |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
diff
changeset
|
283 |