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
8cf69d6f
Commit
8cf69d6f
authored
5 years ago
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added bunch of unit tests
parent
20910cc8
master
…
stable-1
stable-0
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
26 deletions
+65
-26
template_engine.py
pdf_generator/tex_generation/template_engine.py
+20
-26
__init__.py
test/unit/tex_generation/__init__.py
+0
-0
test_template_engine.py
test/unit/tex_generation/test_template_engine.py
+45
-0
No files found.
pdf_generator/tex_generation/template_engine.py
View file @
8cf69d6f
...
...
@@ -12,7 +12,7 @@ GENERIC_TEMPLATE = 'generic.tex'
def
byte_number_filter
(
number
,
verbose
=
True
):
if
isinstance
(
number
,
(
int
,
float
)):
if
verbose
:
return
'{} ({})'
.
format
(
human_readable_file_size
(
int
(
number
)),
format
(
number
,
',d'
)
+
'
bytes
'
)
return
'{} ({})'
.
format
(
human_readable_file_size
(
int
(
number
)),
format
(
number
,
',d'
)
+
'
Byte
'
)
return
human_readable_file_size
(
int
(
number
))
return
'not available'
...
...
@@ -25,7 +25,7 @@ def nice_unix_time(unix_time_stamp):
if
isinstance
(
unix_time_stamp
,
(
int
,
float
)):
tmp
=
localtime
(
unix_time_stamp
)
return
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S'
,
tmp
)
return
unix_time_stamp
return
'not available'
def
nice_number_filter
(
number
):
...
...
@@ -33,9 +33,12 @@ def nice_number_filter(number):
return
'{:,}'
.
format
(
number
)
if
isinstance
(
number
,
float
):
return
'{:,.2f}'
.
format
(
number
)
if
number
is
None
:
return
'not available'
return
number
if
isinstance
(
number
,
str
):
try
:
return
str
(
int
(
number
))
except
ValueError
:
pass
return
'not available'
def
filter_latex_special_chars
(
data
):
...
...
@@ -65,42 +68,33 @@ def filter_latex_special_chars(data):
return
data
def
count_elements_in_list
(
ls
):
return
len
(
ls
)
def
convert_base64_to_png_filter
(
base64_string
,
filename
,
directory
):
file_path
=
Path
(
directory
,
filename
+
'.png'
)
file_path
.
write_bytes
(
decodebytes
(
base64_string
.
encode
(
'utf-8'
)))
return
str
(
file_path
)
def
check_if_list_empty
(
list_of_strings
):
return
list_of_strings
if
list_of_strings
else
[
'list is empty'
]
def
filter_chars_in_list
(
list_of_strings
):
return
[
filter_latex_special_chars
(
item
)
for
item
in
list_of_strings
]
def
split_hash
(
hash_value
):
if
len
(
hash_value
)
>
61
:
hash_value
=
hash_value
[:
61
]
+
' '
+
hash_value
[
61
:]
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_output_lines
(
output_value
):
splited_lines
=
output_value
.
splitlines
(
)
def
split_output_lines
(
output_value
,
max_length
=
92
):
lines
=
output_value
.
splitlines
(
keepends
=
True
)
output
=
''
for
line
in
splited_lines
:
line_length
=
len
(
line
)
# word_lengths.append(list(map(len, line.split(' '))))
if
line_length
>
92
:
line
=
line
[:
92
]
+
' '
+
line
[
92
:]
output
+=
line
+
'
\n
'
for
line
in
lines
:
if
len
(
line
)
>
max_length
:
line
=
'{} {}'
.
format
(
line
[:
max_length
],
line
[
max_length
:])
output
+=
line
return
output
...
...
@@ -128,9 +122,9 @@ def _add_filters_to_jinja(environment):
environment
.
filters
[
'nice_unix_time'
]
=
nice_unix_time
environment
.
filters
[
'nice_number'
]
=
nice_number_filter
environment
.
filters
[
'filter_chars'
]
=
filter_latex_special_chars
environment
.
filters
[
'elements_count'
]
=
count_elements_in_list
environment
.
filters
[
'elements_count'
]
=
len
environment
.
filters
[
'base64_to_png'
]
=
convert_base64_to_png_filter
environment
.
filters
[
'check_list'
]
=
check_if_list_empty
environment
.
filters
[
'check_list'
]
=
lambda
x
:
x
if
x
else
[
'list is empty'
]
environment
.
filters
[
'filter_list'
]
=
filter_chars_in_list
environment
.
filters
[
'split_hash'
]
=
split_hash
environment
.
filters
[
'split_output_lines'
]
=
split_output_lines
...
...
This diff is collapsed.
Click to expand it.
test/unit/tex_generation/__init__.py
0 → 100644
View file @
8cf69d6f
This diff is collapsed.
Click to expand it.
test/unit/tex_generation/test_template_engine.py
0 → 100644
View file @
8cf69d6f
from
pathlib
import
Path
from
pdf_generator.tex_generation.template_engine
import
(
byte_number_filter
,
convert_base64_to_png_filter
,
nice_number_filter
,
nice_unix_time
,
split_hash
,
split_output_lines
)
def
test_byte_number_filter
():
assert
byte_number_filter
(
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'
def
test_nice_number_filter
():
assert
nice_number_filter
(
None
)
==
'not available'
assert
nice_number_filter
(
'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'
def
test_nice_unix_time
():
assert
nice_unix_time
(
None
)
==
'not available'
assert
nice_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
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
)
def
test_convert_base64_to_png_filter
(
tmpdir
):
convert_base64_to_png_filter
(
'0000'
,
'testfile'
,
str
(
tmpdir
))
assert
Path
(
str
(
tmpdir
),
'testfile.png'
)
.
read_bytes
()
==
b
'
\xd3\x4d\x34
'
This diff is collapsed.
Click to expand it.
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