Debian Codegraph @

lib/report/xml_report.py

raw
1 # -*- coding: utf-8 -*-
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15 # MA 02110-1301, USA.
16 #
17 # Author: Mauro Soria
18
19 from xml.etree import ElementTree as ET
20
21 from lib.core.decorators import locked
22 from lib.core.settings import (
23 COMMAND,
24 DEFAULT_ENCODING,
25 START_TIME,
26 )
27 from lib.report.factory import BaseReport, FileReportMixin
28
29
30 class XMLReport(FileReportMixin, BaseReport):
31 __format__ = "xml"
32 __extension__ = "xml"
33
34 def new(self):
35 return ET.Element("dirsearchscan", args=COMMAND, time=START_TIME)
36
37 def parse(self, file):
38 return ET.parse(file).getroot()
39
40 @locked
41 def save(self, file, result):
42 root = self.parse(file)
43 target = ET.SubElement(root, "result", url=result.url)
44 ET.SubElement(target, "status").text = str(result.status)
45 ET.SubElement(target, "contentLength").text = str(result.length)
46 ET.SubElement(target, "contentType").text = result.type
47 ET.SubElement(target, "redirect").text = result.redirect
48 self.write(file, root)
49
50 def write(self, file, root):
51 ET.indent(root)
52 xml_ = ET.tostring(root, encoding=DEFAULT_ENCODING, method="xml").decode()
53 super().write(file, xml_)