Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fact_pdf_report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
fact_pdf_report
Commits
e280210d
Commit
e280210d
authored
Jul 17, 2019
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uncle Bobbed the filter plus other requested changes
parent
653701f3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
72 deletions
+66
-72
template_engine.py
pdf_generator/tex_generation/template_engine.py
+33
-43
test_generator.py
test/unit/test_generator.py
+8
-4
test_template_engine.py
test/unit/tex_generation/test_template_engine.py
+25
-25
No files found.
pdf_generator/tex_generation/template_engine.py
View file @
e280210d
import
logging
from
base64
import
decodebytes
from
collections
import
OrderedDict
from
contextlib
import
suppress
from
pathlib
import
Path
from
time
import
localtime
,
strftime
...
...
@@ -10,39 +11,32 @@ from common_helper_files import human_readable_file_size
GENERIC_TEMPLATE
=
'generic.tex'
def
byte_number_filter
(
number
,
verbose
=
True
):
if
isinstance
(
number
,
(
int
,
float
)):
def
render_number_as_size
(
number
,
verbose
=
True
):
if
not
isinstance
(
number
,
(
int
,
float
)):
return
'not available'
if
verbose
:
return
'{} ({})'
.
format
(
human_readable_file_size
(
int
(
number
)),
format
(
number
,
',d'
)
+
' Byte'
)
return
human_readable_file_size
(
int
(
number
))
return
'not available'
def
nice_unix_time
(
unix_time_stamp
):
'''
input unix_time_stamp
output string 'YYYY-MM-DD HH:MM:SS'
'''
if
isinstance
(
unix_time_stamp
,
(
int
,
float
)):
tmp
=
localtime
(
unix_time_stamp
)
return
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S'
,
tmp
)
def
render_unix_time
(
unix_time_stamp
):
if
not
isinstance
(
unix_time_stamp
,
(
int
,
float
)):
return
'not available'
return
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S'
,
localtime
(
unix_time_stamp
))
def
nice_number_filter
(
number
):
def
render_number_as_string
(
number
):
if
isinstance
(
number
,
int
):
return
'{:,}'
.
format
(
number
)
if
isinstance
(
number
,
float
):
return
'{:,.2f}'
.
format
(
number
)
if
isinstance
(
number
,
str
):
try
:
with
suppress
(
ValueError
)
:
return
str
(
int
(
number
))
except
ValueError
:
pass
return
'not available'
def
filter_latex_special_cha
rs
(
data
):
def
replace_special_characte
rs
(
data
):
latex_character_escapes
=
OrderedDict
()
latex_character_escapes
[
'
\\
'
]
=
''
latex_character_escapes
[
'
\'
'
]
=
''
...
...
@@ -69,34 +63,31 @@ def filter_latex_special_chars(data):
return
data
def
convert_base64_to_png_filter
(
base64_string
,
filename
,
directory
):
file_path
=
Path
(
directory
,
filename
+
'.png'
)
def
decode_base64_to_file
(
base64_string
,
filename
,
directory
,
suffix
=
'png'
):
file_path
=
Path
(
directory
,
'{}.{}'
.
format
(
filename
,
suffix
)
)
file_path
.
write_bytes
(
decodebytes
(
base64_string
.
encode
(
'utf-8'
)))
return
str
(
file_path
)
def
filter_cha
rs_in_list
(
list_of_strings
):
def
replace_characte
rs_in_list
(
list_of_strings
):
return
[
filter_latex_special_cha
rs
(
item
)
for
item
in
list_of_strings
replace_special_characte
rs
(
item
)
for
item
in
list_of_strings
]
def
split_hash
(
hash_value
,
max_length
=
61
):
if
len
(
hash_
value
)
>
max_length
:
hash_
value
=
'{} {}'
.
format
(
hash_value
[:
max_length
],
hash_value
[
max_length
:])
return
hash_
value
def
split_hash
_string
(
hash_string
,
max_length
=
61
):
if
len
(
hash_
string
)
>
max_length
:
hash_
string
=
'{}
\n
{}'
.
format
(
hash_string
[:
max_length
],
hash_string
[
max_length
:])
return
hash_
string
def
split_
output_lines
(
output_value
,
max_length
=
92
):
lines
=
output_value
.
splitlines
(
keepends
=
True
)
output
=
''
def
split_
long_lines
(
multiline_string
,
max_length
=
92
):
def
evaluate_split
(
line
):
return
line
if
len
(
line
)
<=
max_length
else
'{}
\n
{}'
.
format
(
line
[:
max_length
],
line
[
max_length
:])
for
line
in
lines
:
if
len
(
line
)
>
max_length
:
line
=
'{} {}'
.
format
(
line
[:
max_length
],
line
[
max_length
:])
output
+=
line
return
output
return
''
.
join
(
evaluate_split
(
line
)
for
line
in
multiline_string
.
splitlines
(
keepends
=
True
)
)
def
item_contains_string
(
item
,
string
):
...
...
@@ -125,22 +116,21 @@ def create_jinja_environment(templates_to_use='default'):
def
plugin_name
(
name
):
parts
=
name
.
split
(
'_'
)
return
' '
.
join
((
'{}{}'
.
format
(
part
[
0
:
1
]
.
upper
(),
part
[
1
:])
for
part
in
parts
))
return
' '
.
join
((
part
.
title
()
for
part
in
name
.
split
(
'_'
)))
def
_add_filters_to_jinja
(
environment
):
environment
.
filters
[
'number_format'
]
=
byte_number_filter
environment
.
filters
[
'nice_unix_time'
]
=
nice
_unix_time
environment
.
filters
[
'nice_number'
]
=
nice_number_filter
environment
.
filters
[
'filter_chars'
]
=
filter_latex_special_cha
rs
environment
.
filters
[
'number_format'
]
=
render_number_as_size
environment
.
filters
[
'nice_unix_time'
]
=
render
_unix_time
environment
.
filters
[
'nice_number'
]
=
render_number_as_string
environment
.
filters
[
'filter_chars'
]
=
replace_special_characte
rs
environment
.
filters
[
'elements_count'
]
=
len
environment
.
filters
[
'base64_to_png'
]
=
convert_base64_to_png_filter
environment
.
filters
[
'base64_to_png'
]
=
decode_base64_to_file
environment
.
filters
[
'check_list'
]
=
lambda
x
:
x
if
x
else
[
'list is empty'
]
environment
.
filters
[
'plugin_name'
]
=
plugin_name
environment
.
filters
[
'filter_list'
]
=
filter_cha
rs_in_list
environment
.
filters
[
'split_hash'
]
=
split_hash
environment
.
filters
[
'split_output_lines'
]
=
split_
output
_lines
environment
.
filters
[
'filter_list'
]
=
replace_characte
rs_in_list
environment
.
filters
[
'split_hash'
]
=
split_hash
_string
environment
.
filters
[
'split_output_lines'
]
=
split_
long
_lines
environment
.
filters
[
'contains'
]
=
item_contains_string
...
...
test/unit/test_generator.py
View file @
e280210d
import
json
from
pathlib
import
Path
import
pytest
from
pdf_generator.generator
import
(
copy_fact_image
,
create_report_filename
,
create_templates
,
execute_latex
,
render_analysis_templates
)
...
...
@@ -40,10 +41,13 @@ def test_copy_fact_image(tmpdir):
assert
Path
(
str
(
tmpdir
),
'fact_logo.png'
)
.
exists
()
def
test_create_report_filename
():
assert
create_report_filename
({
'device_name'
:
'simple'
})
==
'simple_analysis_report.pdf'
assert
create_report_filename
({
'device_name'
:
'harder name'
})
==
'harder_name_analysis_report.pdf'
assert
create_report_filename
({
'device_name'
:
'dangerous/name'
})
==
'dangerous__name_analysis_report.pdf'
@pytest.mark.parametrize
(
'device_name, pdf_name'
,
[
(
'simple'
,
'simple_analysis_report.pdf'
),
(
'harder name'
,
'harder_name_analysis_report.pdf'
),
(
'dangerous/name'
,
'dangerous__name_analysis_report.pdf'
)
])
def
test_create_report_filename
(
device_name
,
pdf_name
):
assert
create_report_filename
({
'device_name'
:
device_name
})
==
pdf_name
def
test_create_analysis_templates
():
...
...
test/unit/tex_generation/test_template_engine.py
View file @
e280210d
...
...
@@ -2,8 +2,8 @@ from pathlib import Path
import
pytest
from
pdf_generator.tex_generation.template_engine
import
(
TemplateEngine
,
byte_number_filter
,
convert_base64_to_png_filter
,
filter_chars_in_list
,
filter_latex_special_chars
,
nice_number_filter
,
nice_unix_time
,
split_hash
,
split_output
_lines
TemplateEngine
,
decode_base64_to_file
,
render_number_as_size
,
render_number_as_string
,
render_unix_time
,
replace_characters_in_list
,
replace_special_characters
,
split_hash_string
,
split_long
_lines
)
# pylint: disable=redefined-outer-name
...
...
@@ -15,57 +15,57 @@ def stub_engine(tmpdir):
def
test_byte_number_filter
():
assert
byte_number_filter
(
None
)
==
'not available'
assert
render_number_as_size
(
None
)
==
'not available'
assert
byte_number_filter
(
12
,
verbose
=
False
)
==
'12.00 Byte'
assert
byte_number_filter
(
128000
)
==
'125.00 KiB (128,000 Byte)'
assert
byte_number_filter
(
128000
,
verbose
=
False
)
==
'125.00 KiB'
assert
render_number_as_size
(
12
,
verbose
=
False
)
==
'12.00 Byte'
assert
render_number_as_size
(
128000
)
==
'125.00 KiB (128,000 Byte)'
assert
render_number_as_size
(
128000
,
verbose
=
False
)
==
'125.00 KiB'
def
test_nice_number_filter
():
assert
nice_number_filter
(
None
)
==
'not available'
assert
nice_number_filter
(
'no int'
)
==
'not available'
assert
render_number_as_string
(
None
)
==
'not available'
assert
render_number_as_string
(
'no int'
)
==
'not available'
assert
nice_number_filter
(
12
)
==
'12'
assert
nice_number_filter
(
12.1
)
==
'12.10'
assert
nice_number_filter
(
12.101
)
==
'12.10'
assert
nice_number_filter
(
12.109
)
==
'12.11'
assert
nice_number_filter
(
'12'
)
==
'12'
assert
render_number_as_string
(
12
)
==
'12'
assert
render_number_as_string
(
12.1
)
==
'12.10'
assert
render_number_as_string
(
12.101
)
==
'12.10'
assert
render_number_as_string
(
12.109
)
==
'12.11'
assert
render_number_as_string
(
'12'
)
==
'12'
@pytest.mark.skip
(
reason
=
'Since local time used, result is not stable'
)
def
test_nice_unix_time
():
assert
nice
_unix_time
(
None
)
==
'not available'
assert
render
_unix_time
(
None
)
==
'not available'
assert
nice
_unix_time
(
10
)
==
'1970-01-01 01:00:10'
assert
render
_unix_time
(
10
)
==
'1970-01-01 01:00:10'
def
test_split_hash
():
assert
split_hash
(
'X'
*
62
)
==
'{}
X'
.
format
(
'X'
*
61
)
assert
split_hash
(
'X'
*
61
)
==
'X'
*
61
assert
split_hash
_string
(
'X'
*
62
)
==
'{}
\n
X'
.
format
(
'X'
*
61
)
assert
split_hash
_string
(
'X'
*
61
)
==
'X'
*
61
def
test_split_output_lines
():
assert
split_
output
_lines
(
'X
\n
X'
)
==
'X
\n
X'
assert
split_
output_lines
(
'{}
\n
X'
.
format
(
'X'
*
93
))
==
'{}
X
\n
X'
.
format
(
'X'
*
92
)
assert
split_
long
_lines
(
'X
\n
X'
)
==
'X
\n
X'
assert
split_
long_lines
(
'{}
\n
X'
.
format
(
'X'
*
93
))
==
'{}
\n
X
\n
X'
.
format
(
'X'
*
92
)
def
test_convert_base64_to_png_filter
(
tmpdir
):
convert_base64_to_png_filter
(
'0000'
,
'testfile'
,
str
(
tmpdir
))
decode_base64_to_file
(
'0000'
,
'testfile'
,
str
(
tmpdir
))
assert
Path
(
str
(
tmpdir
),
'testfile.png'
)
.
read_bytes
()
==
b
'
\xd3\x4d\x34
'
def
test_filter_latex_special_chars
():
assert
filter_latex_special_cha
rs
(
'safe'
)
==
'safe'
assert
replace_special_characte
rs
(
'safe'
)
==
'safe'
assert
filter_latex_special_cha
rs
(
r'C:\Windows'
)
==
r'C:Windows'
assert
filter_latex_special_cha
rs
(
r'100 $'
)
==
r'100 \$'
assert
replace_special_characte
rs
(
r'C:\Windows'
)
==
r'C:Windows'
assert
replace_special_characte
rs
(
r'100 $'
)
==
r'100 \$'
def
test_filter_chars_in_list
():
assert
filter_cha
rs_in_list
([])
==
[]
assert
replace_characte
rs_in_list
([])
==
[]
assert
filter_cha
rs_in_list
([
r'safe'
,
r'un\safe'
])
==
[
'safe'
,
'unsafe'
]
assert
replace_characte
rs_in_list
([
r'safe'
,
r'un\safe'
])
==
[
'safe'
,
'unsafe'
]
def
test_render_meta_template
(
stub_engine
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment