def write_report(self): if self.report_obj is None: if self.format == "simple": report = SimpleReport(self.output, self.reports) elif self.format == "json": report = JSONReport(self.output, self.reports) elif self.format == "xml": report = XMLReport(self.output, self.reports) elif self.format == "md": report = MarkdownReport(self.output, self.reports) elif self.format == "csv": report = CSVReport(self.output, self.reports) elif self.format == "html": report = HTMLReport(self.output, self.reports) else: report = PlainTextReport(self.output, self.reports) self.report_obj = report with self.lock: self.report_obj.save()
def setupReports(self, requester): if self.arguments.autoSave: basePath = requester.basePath basePath = basePath.replace(os.path.sep, ".")[:-1] fileName = None directoryPath = None if self.batch: fileName = requester.host directoryPath = self.batchDirectoryPath else: fileName = ('{}_'.format(basePath)) fileName += time.strftime('%y-%m-%d_%H-%M-%S') fileName += ".{0}".format(self.arguments.autoSaveFormat) directoryPath = FileUtils.build_path(self.savePath, 'reports', requester.host) outputFile = FileUtils.build_path(directoryPath, fileName) self.output.outputFile(outputFile) if FileUtils.exists(outputFile): i = 2 while FileUtils.exists(outputFile + "_" + str(i)): i += 1 outputFile += "_" + str(i) if not FileUtils.exists(directoryPath): FileUtils.create_directory(directoryPath) if not FileUtils.exists(directoryPath): self.output.error( "Couldn't create the reports folder at {}".format( directoryPath)) sys.exit(1) if FileUtils.can_write(directoryPath): report = None if self.arguments.autoSaveFormat == "simple": report = SimpleReport( requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch, ) elif self.arguments.autoSaveFormat == "json": report = JSONReport( requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch, ) elif self.arguments.autoSaveFormat == "xml": report = XMLReport( requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch, ) elif self.arguments.autoSaveFormat == "md": report = MarkdownReport( requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch, ) elif self.arguments.autoSaveFormat == "csv": report = CSVReport( requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch, ) else: report = PlainTextReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile, self.batch) self.reportManager.addOutput(report) else: self.output.error( "Can't write reports to {}".format(directoryPath)) sys.exit(1) # TODO: format, refactor code if self.arguments.simpleOutputFile: self.reportManager.addOutput( SimpleReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.simpleOutputFile, self.batch)) if self.arguments.plainTextOutputFile: self.reportManager.addOutput( PlainTextReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.plainTextOutputFile, self.batch)) if self.arguments.jsonOutputFile: self.reportManager.addOutput( JSONReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.jsonOutputFile, self.batch)) if self.arguments.xmlOutputFile: self.reportManager.addOutput( XMLReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.xmlOutputFile, self.batch)) if self.arguments.markdownOutputFile: self.reportManager.addOutput( MarkdownReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.markdownOutputFile, self.batch)) if self.arguments.csvOutputFile: self.reportManager.addOutput( CSVReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.csvOutputFile, self.batch))
def setupReports(self, requester): if self.arguments.autoSave: basePath = ('/' if requester.basePath is '' else requester.basePath) basePath = basePath.replace(os.path.sep, '.')[1:-1] fileName = None directoryPath = None if self.batch: fileName = requester.host directoryPath = self.batchDirectoryPath else: fileName = ('{}_'.format(basePath) if basePath is not '' else '') fileName += time.strftime('%y-%m-%d_%H-%M-%S') directoryPath = FileUtils.buildPath(self.savePath, 'reports', requester.host) outputFile = FileUtils.buildPath(directoryPath, fileName) if FileUtils.exists(outputFile): i = 2 while FileUtils.exists(outputFile + "_" + str(i)): i += 1 outputFile += "_" + str(i) if not FileUtils.exists(directoryPath): FileUtils.createDirectory(directoryPath) if not FileUtils.exists(directoryPath): self.output.error( "Couldn't create reports folder {}".format( directoryPath)) sys.exit(1) if FileUtils.canWrite(directoryPath): # I assume this section checks the default.config # In this section #[reports] #autosave-report = True #autosave-report-format = simple report = None if self.arguments.autoSaveFormat == 'simple': report = SimpleReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile) if self.arguments.autoSaveFormat == 'json': report = JSONReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile) if self.arguments.autoSaveFormat == 'csv': report = CSVReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile) if self.arguments.autoSaveFormat == 'sqlite': report = SQLiteReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile) # The default autosave format is plainText hence: # autosave-report-format = plain else: report = PlainTextReport(requester.host, requester.port, requester.protocol, requester.basePath, outputFile) self.reportManager.addOutput(report) else: self.output.error( "Can't write reports to {}".format(directoryPath)) sys.exit(1) if self.arguments.simpleOutputFile is not None: self.reportManager.addOutput( SimpleReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.simpleOutputFile)) if self.arguments.plainTextOutputFile is not None: self.reportManager.addOutput( PlainTextReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.plainTextOutputFile)) # sqliteOutputFile if self.arguments.sqliteOutputFile is not None: self.reportManager.addOutput( SQLiteReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.sqliteOutputFile)) # csvOutputFile if self.arguments.csvOutputFile is not None: self.reportManager.addOutput( CSVReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.csvOutputFile)) if self.arguments.jsonOutputFile is not None: self.reportManager.addOutput( JSONReport(requester.host, requester.port, requester.protocol, requester.basePath, self.arguments.jsonOutputFile))