fix: [pdfexport] Fix tests paths, dependency in pipfile, imports, and 'file' name overwrite in test function

pull/348/head
Falconieri 2019-02-22 11:34:41 +01:00
parent 57eccfe7dc
commit 76ef3d058f
4 changed files with 20 additions and 61 deletions

View File

@ -13,6 +13,7 @@ requests-mock = "*"
pymisp = {editable = true,extras = ["fileobjects", "neo", "openioc", "virustotal"],path = "."}
pydeep = {editable = true,git = "https://github.com/kbandla/pydeep.git"}
pymispwarninglists = {editable = true,git = "https://github.com/MISP/PyMISPWarningLists.git"}
reportlab = "*"
[requires]
python_version = "3.6"

View File

@ -11,22 +11,21 @@ import pymisp
logger = logging.getLogger('pymisp')
# Potentially not installed imports
try:
from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.lib import colors
#try:
from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable
from reportlab.lib.units import mm, inch
from reportlab.lib.enums import TA_RIGHT, TA_CENTER, TA_JUSTIFY, TA_LEFT
from reportlab.lib.pagesizes import letter, inch
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm
from reportlab.lib.enums import TA_RIGHT, TA_CENTER, TA_JUSTIFY, TA_LEFT
HAS_REPORTLAB = True
except ImportError:
HAS_REPORTLAB = False
print("ReportLab cannot be imported. Please verify that ReportLab is installed on the system.")
# HAS_REPORTLAB = True
#except ImportError:
# HAS_REPORTLAB = False
# print("ReportLab cannot be imported. Please verify that ReportLab is installed on the system.")
########################################################################
@ -675,7 +674,6 @@ def convert_event_in_pdf_buffer(misp_event: pymisp.MISPEvent):
def get_values_from_buffer(pdf_buffer):
return pdf_buffer.value()
def get_base64_from_buffer(pdf_buffer):
return base64.b64encode(pdf_buffer.value())
@ -690,45 +688,5 @@ def register_to_file(pdf_buffer, file_name):
f.write(pdf_buffer.read())
def register_value_to_file(pdf_value, file_name):
with open(file_name, 'wb') as f:
f.write(pdf_value)
if __name__ == "__main__":
# pdf_buffer = convert_event_in_pdf_buffer(None)
# register_to_file(pdf_buffer, 'test.pdf')
get_sample_fonts()
get_sample_styles()
# get_values_from_buffer(pdf_buffer)
# get_base64_from_buffer(pdf_buffer)
''' In the future ?
try:
from pymispgalaxies import Clusters
has_pymispgalaxies = True
except ImportError:
has_pymispgalaxies = False
try:
from pytaxonomies import Taxonomies
has_pymispgalaxies = True
except ImportError:
has_pymispgalaxies = False
'''
'''
class ReportLabObject():
def __init__(self, parameters, strict=True, standalone=True, **kwargs):
super(ReportLabObject, self).__init__('reportlab', strict=strict, standalone=standalone, **kwargs)
self._parameters = parameters
self.generate_attributes()
def generate_attributes(self):
first = self._sanitize_timestamp(self._parameters.pop('first-seen', None))
self._parameters['first-seen'] = first
last = self._sanitize_timestamp(self._parameters.pop('last-seen', None))
self._parameters['last-seen'] = last
return super(ReportLabObject, self).generate_attributes()
'''

View File

@ -202,7 +202,7 @@ endobj
endobj
24 0 obj
<<
/Author (\(anonymous\)) /CreationDate (D:20190221153022-01'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20190221153022-01'00') /Producer (ReportLab PDF Library - www.reportlab.com)
/Author (\(anonymous\)) /CreationDate (D:20190222095950-01'00') /Creator (\(unspecified\)) /Keywords () /ModDate (D:20190222095950-01'00') /Producer (ReportLab PDF Library - www.reportlab.com)
/Subject (\(unspecified\)) /Title (\(anonymous\)) /Trapped /False
>>
endobj
@ -379,7 +379,7 @@ xref
trailer
<<
/ID
[<ce880b5391302daa2739de9c741801c3><ce880b5391302daa2739de9c741801c3>]
[<0f16994cff7b04926980333b4fec6185><0f16994cff7b04926980333b4fec6185>]
% ReportLab generated PDF document -- digest (http://www.reportlab.com)
/Info 24 0 R

View File

@ -14,8 +14,8 @@ class TestMISPEvent(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.mispevent = MISPEvent()
self.test_folder = "reportlab_testfiles/"
self.storage_folder = "reportlab_testoutputs/"
self.test_folder = "tests/reportlab_testfiles/"
self.storage_folder = "tests/reportlab_testoutputs/"
def init_event(self):
self.mispevent.info = 'This is a test'
@ -40,9 +40,9 @@ class TestMISPEvent(unittest.TestCase):
file_nb = str(len(os.listdir(self.test_folder)))
i = 0
for file in os.listdir(self.test_folder):
for curr_file in os.listdir(self.test_folder):
self.mispevent = MISPEvent()
file_path = self.test_folder + file
file_path = self.test_folder + curr_file
print("Current file : " + file_path + " " + str(i) + " over " + file_nb)
i += 1
@ -50,4 +50,4 @@ class TestMISPEvent(unittest.TestCase):
self.mispevent.load_file(file_path)
reportlab_generator.register_value_to_file(reportlab_generator.convert_event_in_pdf_buffer(self.mispevent),
self.storage_folder + file + ".pdf")
self.storage_folder + curr_file + ".pdf")