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 = "."} pymisp = {editable = true,extras = ["fileobjects", "neo", "openioc", "virustotal"],path = "."}
pydeep = {editable = true,git = "https://github.com/kbandla/pydeep.git"} pydeep = {editable = true,git = "https://github.com/kbandla/pydeep.git"}
pymispwarninglists = {editable = true,git = "https://github.com/MISP/PyMISPWarningLists.git"} pymispwarninglists = {editable = true,git = "https://github.com/MISP/PyMISPWarningLists.git"}
reportlab = "*"
[requires] [requires]
python_version = "3.6" python_version = "3.6"

View File

@ -11,22 +11,21 @@ import pymisp
logger = logging.getLogger('pymisp') logger = logging.getLogger('pymisp')
# Potentially not installed imports # Potentially not installed imports
try: #try:
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
from reportlab.pdfbase.pdfmetrics import stringWidth from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.lib import colors from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable from reportlab.platypus import SimpleDocTemplate, Paragraph, PageBreak, Spacer, Table, TableStyle, Flowable
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm, inch from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_RIGHT, TA_CENTER, TA_JUSTIFY, TA_LEFT from reportlab.lib.units import mm
from reportlab.lib.pagesizes import letter, inch from reportlab.lib.enums import TA_RIGHT, TA_CENTER, TA_JUSTIFY, TA_LEFT
HAS_REPORTLAB = True # HAS_REPORTLAB = True
except ImportError: #except ImportError:
HAS_REPORTLAB = False # HAS_REPORTLAB = False
print("ReportLab cannot be imported. Please verify that ReportLab is installed on the system.") # 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): def get_values_from_buffer(pdf_buffer):
return pdf_buffer.value() return pdf_buffer.value()
def get_base64_from_buffer(pdf_buffer): def get_base64_from_buffer(pdf_buffer):
return base64.b64encode(pdf_buffer.value()) return base64.b64encode(pdf_buffer.value())
@ -690,45 +688,5 @@ def register_to_file(pdf_buffer, file_name):
f.write(pdf_buffer.read()) f.write(pdf_buffer.read())
def register_value_to_file(pdf_value, file_name): def register_value_to_file(pdf_value, file_name):
with open(file_name, 'wb') as f: with open(file_name, 'wb') as f:
f.write(pdf_value) 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 endobj
24 0 obj 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 /Subject (\(unspecified\)) /Title (\(anonymous\)) /Trapped /False
>> >>
endobj endobj
@ -379,7 +379,7 @@ xref
trailer trailer
<< <<
/ID /ID
[<ce880b5391302daa2739de9c741801c3><ce880b5391302daa2739de9c741801c3>] [<0f16994cff7b04926980333b4fec6185><0f16994cff7b04926980333b4fec6185>]
% ReportLab generated PDF document -- digest (http://www.reportlab.com) % ReportLab generated PDF document -- digest (http://www.reportlab.com)
/Info 24 0 R /Info 24 0 R

View File

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