-
Notifications
You must be signed in to change notification settings - Fork 0
/
_importable.py
171 lines (139 loc) · 5.6 KB
/
_importable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
################################################################################
# _importable.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
from App.Common import package_home
from App.special_dtml import HTMLFile
from cStringIO import StringIO
import ZPublisher.HTTPRequest
import os
import sys
import tempfile
import time
import transaction
import urllib
import zExceptions
# Product Imports.
import _fileutil
import _filtermanager
import _globals
from _blobfields import recurse_uploadRessources, uploadRessources
# ------------------------------------------------------------------------------
# _importable.recurse_importContent:
#
# Process objects after import.
# ------------------------------------------------------------------------------
def recurse_importContent(self, folder):
message = ''
# Cleanup.
for key in ['oRoot','oRootNode','oCurrNode','oParent','dTagStack','dValueStack']:
try: delattr(self,key)
except: pass
# Upload ressources.
uploadRessources(self,folder)
# Commit object.
self.onChangeObj( self.REQUEST, forced=1)
transaction.commit()
# Process children.
for ob in self.getChildNodes():
recurse_importContent(ob,folder)
# Return with message.
return message
# ------------------------------------------------------------------------------
# _importable.importContent
# ------------------------------------------------------------------------------
def importContent(self, file):
message = ''
# Setup.
catalog_awareness = self.getConfProperty('ZMS.CatalogAwareness.active',1)
self.setConfProperty('ZMS.CatalogAwareness.active',0)
self.dTagStack = _globals.MyStack()
self.dValueStack = _globals.MyStack()
self.oParent = self.getParentNode()
# Parse XML-file.
ob = self.parse(StringIO(file.read()),self,1)
# Process objects after import
message += recurse_importContent(ob,_fileutil.getFilePath(file.name))
# Cleanup.
self.setConfProperty('ZMS.CatalogAwareness.active',catalog_awareness)
# Return with message.
return message
# ------------------------------------------------------------------------------
# _importable.importFile
# ------------------------------------------------------------------------------
def importFile(self, file, REQUEST, handler):
message = ''
# Get filename.
if isinstance(file,ZPublisher.HTTPRequest.FileUpload):
filename = file.filename
else:
filename = file.name
_globals.writeBlock( self, '[importFile]: filename='+filename)
# Create temporary folder.
folder = tempfile.mktemp()
os.mkdir(folder)
# Save to temporary file.
filename = _fileutil.getOSPath('%s/%s'%(folder,_fileutil.extractFilename(filename)))
_fileutil.exportObj(file,filename)
# Find XML-file.
if _fileutil.extractFileExt(filename) == 'zip':
_fileutil.extractZipArchive(filename)
filename = None
for deep in [0,1]:
for ext in ['xml', 'htm', 'html' ]:
if filename is None:
filename = _fileutil.findExtension(ext, folder, deep)
break
if filename is None:
raise zExceptions.InternalError('XML-File not found!')
# Import Filter.
if REQUEST.get('filter','') in self.getFilterIds():
filename = _filtermanager.importFilter(self, filename, REQUEST.get('filter',''), REQUEST)
# Import XML-file.
_globals.writeBlock( self, '[importFile]: filename='+filename)
f = open(filename, 'r')
message += handler(self, f)
f.close()
# Remove temporary files.
_fileutil.remove(folder, deep=1)
# Return with message.
message += self.getZMILangStr('MSG_IMPORTED')%('<i>%s</i>'%_fileutil.extractFilename(filename))
return message
################################################################################
################################################################################
###
### class Importable
###
################################################################################
################################################################################
class Importable:
##############################################################################
# Importable.manage_import:
#
# Import XML-file.
##############################################################################
def manage_import(self, file, lang, REQUEST, RESPONSE=None):
""" Importable.manage_import """
message = ''
t0 = time.time()
# Import XML.
message += importFile(self, file, REQUEST, importContent)
# Return with message.
if RESPONSE:
message += ' (in '+str(int((time.time()-t0)*100.0)/100.0)+' secs.)'
return REQUEST.RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s'%(lang,urllib.quote(message)))
################################################################################