3939from ietf .submit .parsers .plain_parser import PlainParser
4040from ietf .submit .parsers .ps_parser import PSParser
4141from ietf .submit .parsers .xml_parser import XMLParser
42+ from ietf .utils import log
4243from ietf .utils .draft import Draft
4344
4445class SubmissionBaseUploadForm (forms .Form ):
@@ -145,6 +146,9 @@ def clean(self):
145146 xml_file = self .cleaned_data .get ('xml' )
146147 name , ext = os .path .splitext (os .path .basename (xml_file .name ))
147148 tfh , tfn = tempfile .mkstemp (prefix = name + '-' , suffix = '.xml' )
149+ file_name = {}
150+ xml2rfc .log .write_out = open (os .devnull , "w" )
151+ xml2rfc .log .write_err = io .StringIO () # open(os.devnull, "w")
148152 try :
149153 # We need to write the xml file to disk in order to hand it
150154 # over to the xml parser. XXX FIXME: investigate updating
@@ -154,33 +158,15 @@ def clean(self):
154158 for chunk in xml_file .chunks ():
155159 tf .write (chunk )
156160 os .environ ["XML_LIBRARY" ] = settings .XML_LIBRARY
161+ # --- Parse the xml ---
157162 try :
158163 parser = xml2rfc .XmlRfcParser (str (tfn ), quiet = True )
159164 self .xmltree = parser .parse (normalize = True )
160- root = self .xmltree .getroot ()
161- ver = root .get ('version' , '2' )
162- if ver == '2' :
163- ok , errors = self .xmltree .validate ()
164- else :
165- # XXX TODO: Add v3 validation
166- ok , errors = True , ''
167- except Exception as exc :
168- raise forms .ValidationError ("An exception occurred when trying to process the XML file: %s" % exc )
169- if not ok :
170- # Each error has properties:
171- #
172- # message: the message text
173- # domain: the domain ID (see lxml.etree.ErrorDomains)
174- # type: the message type ID (see lxml.etree.ErrorTypes)
175- # level: the log level ID (see lxml.etree.ErrorLevels)
176- # line: the line at which the message originated (if applicable)
177- # column: the character column at which the message originated (if applicable)
178- # filename: the name of the file in which the message originated (if applicable)
179- raise forms .ValidationError (
180- [ forms .ValidationError ("One or more XML validation errors occurred when processing the XML file:" ) ] +
181- [ forms .ValidationError ("%s: Line %s: %s" % (xml_file .name , e .line , e .message ), code = "%s" % e .type ) for e in errors ]
182- )
183- self .xmlroot = self .xmltree .getroot ()
165+ self .xmlroot = self .xmltree .getroot ()
166+ xml_version = self .xmlroot .get ('version' , '2' )
167+ except Exception as e :
168+ raise forms .ValidationError ("An exception occurred when trying to [arse the XML file: %s" % e )
169+
184170 draftname = self .xmlroot .attrib .get ('docName' )
185171 if draftname is None :
186172 raise forms .ValidationError ("No docName attribute found in the xml root element" )
@@ -212,8 +198,78 @@ def clean(self):
212198 if info [item ]:
213199 info [item ] = info [item ].strip ()
214200 self .authors .append (info )
215- except forms .ValidationError :
216- raise
201+
202+ # --- Prep the xml ---
203+ file_name ['xml' ] = os .path .join (settings .IDSUBMIT_STAGING_PATH , '%s-%s.%s' % (self .filename , self .revision , ext ))
204+ try :
205+ if xml_version == '3' :
206+ prep = xml2rfc .PrepToolWriter (self .xmltree , quiet = True )
207+ self .xmltree .tree = prep .prep ()
208+ if self .xmltree .tree == None :
209+ raise forms .ValidationError ("Error from xml2rfc (prep): %s" % prep .errors )
210+ except Exception as e :
211+ raise forms .ValidationError ("Error from xml2rfc (prep): %s" % e )
212+
213+ # --- Convert to txt ---
214+ if not ('txt' in self .cleaned_data and self .cleaned_data ['txt' ]):
215+ file_name ['txt' ] = os .path .join (settings .IDSUBMIT_STAGING_PATH , '%s-%s.txt' % (self .filename , self .revision ))
216+ debug .show ("file_name['txt']" )
217+ try :
218+ if xml_version != '3' :
219+ pagedwriter = xml2rfc .PaginatedTextRfcWriter (self .xmltree , quiet = True )
220+ debug .show ('pagedwriter' )
221+ pagedwriter .write (file_name ['txt' ])
222+ else :
223+ writer = xml2rfc .TextWriter (self .xmltree , quiet = True )
224+ writer .write (file_name ['txt' ])
225+ log .log ("In %s: xml2rfc %s generated %s from %s (version %s)" %
226+ ( os .path .dirname (file_name ['xml' ]),
227+ xml2rfc .__version__ ,
228+ os .path .basename (file_name ['txt' ]),
229+ os .path .basename (file_name ['xml' ]),
230+ xml_version ))
231+ except Exception as e :
232+ raise
233+ debug .show ('xml2rfc.log.write_err.getvalue()' )
234+ debug .show ('e.args' )
235+ raise forms .ValidationError ("Error from xml2rfc (text): %s" % e )
236+
237+ # --- Convert to xml ---
238+ if xml_version == '3' :
239+ try :
240+ file_name ['html' ] = os .path .join (settings .IDSUBMIT_STAGING_PATH , '%s-%s.html' % (self .filename , self .revision ))
241+ writer = xml2rfc .HtmlWriter (self .xmltree , quiet = True )
242+ writer .write (file_name ['html' ])
243+ self .file_types .append ('.html' )
244+ log .log ("In %s: xml2rfc %s generated %s from %s (version %s)" %
245+ ( os .path .dirname (file_name ['xml' ]),
246+ xml2rfc .__version__ ,
247+ os .path .basename (file_name ['html' ]),
248+ os .path .basename (file_name ['xml' ]),
249+ xml_version ))
250+ except Exception as e :
251+ raise forms .ValidationError ("Error from xml2rfc (html): %s" % e )
252+
253+ if xml_version == '2' :
254+ ok , errors = self .xmltree .validate ()
255+ else :
256+ ok , errors = True , ''
257+
258+ if not ok :
259+ # Each error has properties:
260+ #
261+ # message: the message text
262+ # domain: the domain ID (see lxml.etree.ErrorDomains)
263+ # type: the message type ID (see lxml.etree.ErrorTypes)
264+ # level: the log level ID (see lxml.etree.ErrorLevels)
265+ # line: the line at which the message originated (if applicable)
266+ # column: the character column at which the message originated (if applicable)
267+ # filename: the name of the file in which the message originated (if applicable)
268+ raise forms .ValidationError (
269+ [ forms .ValidationError ("One or more XML validation errors occurred when processing the XML file:" ) ] +
270+ [ forms .ValidationError ("%s: Line %s: %s" % (xml_file .name , e .line , e .message ), code = "%s" % e .type ) for e in errors ]
271+ )
272+
217273 finally :
218274 os .close (tfh )
219275 os .unlink (tfn )
0 commit comments