Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
juga
sbws
Commits
f6d48ae0
Commit
f6d48ae0
authored
May 23, 2018
by
juga
Committed by
Matt Traudt
Jun 05, 2018
Browse files
Simplify and make more clear header generation
parent
5c055574
Changes
2
Hide whitespace changes
Inline
Side-by-side
sbws/lib/v3bwfile.py
View file @
f6d48ae0
...
@@ -10,15 +10,15 @@ from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
...
@@ -10,15 +10,15 @@ from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
LINE_SEP
=
'
\n
'
LINE_SEP
=
'
\n
'
K_SEP_V110
=
'='
K
EYVALUE
_SEP_V110
=
'='
K
V
_SEP_V
11
0
=
'
\n
'
K
EYVALUE
_SEP_V
20
0
=
'
'
K_SEP_V200
=
' '
# List of the extra KeyValues accepted by the class
KV_SEP_V200
=
' '
EXTRA_ARG_KEYVALUES
=
[
'software'
,
'software_version'
,
'file_created'
,
ORDERED_KV
=
[
'version
'
]
'earliest_bandwidth'
,
'generator_started
'
]
ORDERED_K
=
[
'timestamp'
,
'version'
]
# List of all unordered KeyValues currently being used to generate the file
ALLOWED_K
=
ORDERED_KV
+
[
'software'
,
'software_version'
,
'lastest_bandwidth'
,
UNORDERED_KEYVALUES
=
EXTRA_ARG_KEYVALUES
+
[
'lastest_bandwidth'
]
'file_created'
,
'earliest_bandwidth'
,
# List of all the KeyValues currently being used to generate the file
'generator_started'
]
ALL_KEYVALUES
=
[
'version'
]
+
UNORDERED_KEYVALUES
TERMINATOR
=
'===='
TERMINATOR
=
'===='
LINE_TERMINATOR
=
TERMINATOR
+
LINE_SEP
LINE_TERMINATOR
=
TERMINATOR
+
LINE_SEP
...
@@ -39,83 +39,70 @@ class V3BwHeader(object):
...
@@ -39,83 +39,70 @@ class V3BwHeader(object):
- generator_started: str, ISO 8601 timestamp in UTC time zone
- generator_started: str, ISO 8601 timestamp in UTC time zone
when the generator started
when the generator started
"""
"""
def
__init__
(
self
,
timestamp
,
version
=
SPEC_VERSION
,
software
=
'sbws'
,
def
__init__
(
self
,
timestamp
,
**
kwargs
):
software_version
=
__version__
,
**
kwargs
):
self
.
timestamp
=
str
(
timestamp
)
# FIXME: which value should timestamp have when is not given?
# KeyValues with default value when not given by kwargs
self
.
timestamp
=
timestamp
self
.
version
=
kwargs
.
get
(
'version'
,
SPEC_VERSION
)
self
.
version
=
version
self
.
software
=
kwargs
.
get
(
'software'
,
'sbws'
)
self
.
software
=
software
self
.
software_version
=
kwargs
.
get
(
'software_version'
,
__version__
)
self
.
software_version
=
software_version
self
.
file_created
=
kwargs
.
get
(
'file_created'
,
now_isodt_str
())
self
.
file_created
=
kwargs
.
get
(
'file_created'
,
now_isodt_str
())
# lastest_bandwidth should not be in kwargs, since it MUST be the
# same as timestamp
self
.
lastest_bandwidth
=
unixts_to_isodt_str
(
timestamp
)
self
.
lastest_bandwidth
=
unixts_to_isodt_str
(
timestamp
)
if
kwargs
.
get
(
'earliest_bandwidth'
):
[
setattr
(
self
,
k
,
v
)
for
k
,
v
in
kwargs
.
items
()
self
.
earliest_bandwidth
=
kwargs
[
'earliest_bandwidth'
]
if
k
in
EXTRA_ARG_KEYVALUES
]
if
kwargs
.
get
(
'generator_started'
):
self
.
generator_started
=
kwargs
[
'generator_started'
]
@
property
@
property
def
kv_ordered_ls
(
self
):
def
keyvalue_unordered_tuple_ls
(
self
):
"""Return list of headers KeyValue tuples for the KeyValues
"""Return list of KeyValue tuples that do not have specific order."""
that have specific order.
# sort the list to generate determinist headers
"""
keyvalue_tuple_ls
=
sorted
([(
k
,
v
)
for
k
,
v
in
self
.
__dict__
.
items
()
kv_ls
=
[(
k
,
str
(
getattr
(
self
,
k
,
''
)))
for
k
in
ORDERED_KV
]
if
k
in
UNORDERED_KEYVALUES
])
log
.
debug
(
'kv_ls %s'
,
kv_ls
)
log
.
debug
(
'keyvalue_tuple_ls %s'
,
keyvalue_tuple_ls
)
return
kv_ls
return
keyvalue_tuple_ls
@
property
def
k_extra_ls
(
self
):
"""Return list of headers Keywords that do not have specific order."""
k_extra
=
list
(
set
(
self
.
__dict__
.
keys
()).
difference
(
ORDERED_K
)
.
intersection
(
ALLOWED_K
))
log
.
debug
(
'k_extra %s'
,
k_extra
)
return
k_extra
@
property
@
property
def
kv_extra_ls
(
self
):
def
keyvalue_tuple_ls
(
self
):
"""Return list of headers KeyValue tuples for the KeyValues
"""Return list of all KeyValue tuples"""
that do not have specific order.
return
[(
'version'
,
self
.
version
)]
+
self
.
keyvalue_unordered_tuple_ls
"""
# sorting the list to generate determinist headers
kv_extra
=
sorted
([(
k
,
str
(
getattr
(
self
,
k
)))
for
k
in
self
.
k_extra_ls
])
log
.
debug
(
'kv_extra %s'
,
kv_extra
)
return
kv_extra
@
property
@
property
def
kv_ls
(
self
):
def
keyvalue_v110str_ls
(
self
):
return
self
.
kv_ordered_ls
+
self
.
kv_extra_ls
"""Return KeyValue list of strings following spec v1.1.0."""
keyvalues
=
[
self
.
timestamp
]
+
[
KEYVALUE_SEP_V110
.
join
([
k
,
v
])
for
k
,
v
in
self
.
keyvalue_tuple_ls
]
log
.
debug
(
'keyvalue %s'
,
keyvalues
)
return
keyvalues
@
property
@
property
def
kv_v110_ls
(
self
):
"""Return header kv list of strings following spec v1.1.0."""
kv
=
[
str
(
self
.
timestamp
)]
+
[
K_SEP_V110
.
join
([
k
,
v
])
for
k
,
v
in
self
.
kv_ls
]
log
.
debug
(
'kv %s'
,
kv
)
return
kv
def
strv110
(
self
):
def
strv110
(
self
):
"""Return header string following spec v1.1.0."""
"""Return header string following spec v1.1.0."""
header_str
=
LINE_SEP
.
join
(
self
.
k
v
_v110_ls
)
+
LINE_SEP
+
\
header_str
=
LINE_SEP
.
join
(
self
.
k
eyvalue
_v110
str
_ls
)
+
LINE_SEP
+
\
LINE_TERMINATOR
LINE_TERMINATOR
log
.
debug
(
'header_str %s'
,
header_str
)
log
.
debug
(
'header_str %s'
,
header_str
)
return
header_str
return
header_str
@
property
@
property
def
k
v
_v200_ls
(
self
):
def
k
eyvalue
_v200_ls
(
self
):
"""Return
header kv
following spec v2.0.0."""
"""Return
KeyValue list of strings
following spec v2.0.0."""
k
v
=
[
str
(
self
.
timestamp
)
]
+
[
K_SEP_V200
.
join
([
k
,
v
])
k
eyvalue
=
[
self
.
timestamp
]
+
[
K
EYVALUE
_SEP_V200
.
join
([
k
,
v
])
for
k
,
v
in
self
.
k
v
_ls
]
for
k
,
v
in
self
.
k
eyvalue_tuple
_ls
]
log
.
debug
(
'k
v %s'
,
kv
)
log
.
debug
(
'k
eyvalue %s'
,
keyvalue
)
return
k
v
return
k
eyvalue
@
property
@
property
def
strv200
(
self
):
def
strv200
(
self
):
"""Return header string following spec v2.0.0."""
"""Return header string following spec v2.0.0."""
header_str
=
LINE_SEP
.
join
(
self
.
k
v
_v200_ls
)
+
LINE_SEP
+
\
header_str
=
LINE_SEP
.
join
(
self
.
k
eyvalue
_v200_ls
)
+
LINE_SEP
+
\
LINE_TERMINATOR
LINE_TERMINATOR
log
.
debug
(
'header_str %s'
,
header_str
)
log
.
debug
(
'header_str %s'
,
header_str
)
return
header_str
return
header_str
def
__str__
(
self
):
if
self
.
version
==
'1.1.0'
:
return
self
.
strv110
return
self
.
strv200
@
classmethod
@
classmethod
def
from_lines_v110
(
cls
,
lines
):
def
from_lines_v110
(
cls
,
lines
):
"""
"""
...
@@ -131,22 +118,17 @@ class V3BwHeader(object):
...
@@ -131,22 +118,17 @@ class V3BwHeader(object):
return
None
return
None
ts
=
lines
[
0
]
ts
=
lines
[
0
]
# not checking order
# not checking order
kwargs
=
dict
([
l
.
split
(
K_SEP_V110
)
kwargs
=
dict
([
l
.
split
(
K
EYVALUE
_SEP_V110
)
for
l
in
lines
[:
index_terminator
]
for
l
in
lines
[:
index_terminator
]
if
l
.
split
(
K_SEP_V110
)[
0
]
in
ALL
OWED_K
])
if
l
.
split
(
K
EYVALUE
_SEP_V110
)[
0
]
in
ALL
_KEYVALUES
])
h
=
cls
(
ts
,
**
kwargs
)
h
=
cls
(
ts
,
**
kwargs
)
return
h
,
lines
[
index_terminator
+
1
:]
return
h
,
lines
[
index_terminator
+
1
:]
@
classmethod
@
classmethod
def
from_text_v110
(
self
,
text
):
def
from_text_v110
(
self
,
text
):
"""
"""
:param
list lines
: text to parse
:param
str text
: text to parse
:returns: tuple of V3BwHeader object and non-header lines
:returns: tuple of V3BwHeader object and non-header lines
"""
"""
assert
isinstance
(
text
,
str
)
assert
isinstance
(
text
,
str
)
return
self
.
from_lines_v110
(
text
.
split
(
LINE_SEP
))
return
self
.
from_lines_v110
(
text
.
split
(
LINE_SEP
))
def
__str__
(
self
):
if
self
.
version
==
'1.1.0'
:
return
self
.
strv110
()
return
self
.
strv200
tests/unit/lib/test_v3bwfile.py
View file @
f6d48ae0
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""Test generation of bandwidth measurements document (v3bw)"""
"""Test generation of bandwidth measurements document (v3bw)"""
from
sbws.globals
import
SPEC_VERSION
from
sbws.globals
import
SPEC_VERSION
from
sbws.lib.v3bwfile
import
V3BwHeader
,
TERMINATOR
,
LINE_SEP
,
K_SEP_V110
from
sbws.lib.v3bwfile
import
V3BwHeader
,
TERMINATOR
,
LINE_SEP
,
K
EYVALUE
_SEP_V110
from
sbws
import
__version__
as
version
from
sbws
import
__version__
as
version
timestamp
=
1523974147
timestamp
=
1523974147
timestamp_l
=
str
(
timestamp
)
timestamp_l
=
str
(
timestamp
)
version_l
=
K_SEP_V110
.
join
([
'version'
,
SPEC_VERSION
])
version_l
=
K
EYVALUE
_SEP_V110
.
join
([
'version'
,
SPEC_VERSION
])
software_l
=
K_SEP_V110
.
join
([
'software'
,
'sbws'
])
software_l
=
K
EYVALUE
_SEP_V110
.
join
([
'software'
,
'sbws'
])
software_version_l
=
K_SEP_V110
.
join
([
'software_version'
,
version
])
software_version_l
=
K
EYVALUE
_SEP_V110
.
join
([
'software_version'
,
version
])
file_created
=
'2018-04-25T13:10:57'
file_created
=
'2018-04-25T13:10:57'
file_created_l
=
K_SEP_V110
.
join
([
'file_created'
,
file_created
])
file_created_l
=
K
EYVALUE
_SEP_V110
.
join
([
'file_created'
,
file_created
])
lastest_bandwidth
=
'2018-04-17T14:09:07'
lastest_bandwidth
=
'2018-04-17T14:09:07'
lastest_bandwidth_l
=
K_SEP_V110
.
join
([
'lastest_bandwidth'
,
lastest_bandwidth
])
lastest_bandwidth_l
=
KEYVALUE_SEP_V110
.
join
([
'lastest_bandwidth'
,
lastest_bandwidth
])
header_ls
=
[
timestamp_l
,
version_l
,
file_created_l
,
lastest_bandwidth_l
,
header_ls
=
[
timestamp_l
,
version_l
,
file_created_l
,
lastest_bandwidth_l
,
software_l
,
software_version_l
,
TERMINATOR
]
software_l
,
software_version_l
,
TERMINATOR
]
header_str
=
LINE_SEP
.
join
(
header_ls
)
+
LINE_SEP
header_str
=
LINE_SEP
.
join
(
header_ls
)
+
LINE_SEP
earliest_bandwidth
=
'2018-04-16T14:09:07'
earliest_bandwidth
=
'2018-04-16T14:09:07'
earliest_bandwidth_l
=
K_SEP_V110
.
join
([
'earliest_bandwidth'
,
earliest_bandwidth_l
=
K
EYVALUE
_SEP_V110
.
join
([
'earliest_bandwidth'
,
earliest_bandwidth
])
earliest_bandwidth
])
generator_started
=
'2018-04-16T14:09:05'
generator_started
=
'2018-04-16T14:09:05'
generator_started_l
=
K_SEP_V110
.
join
([
'generator_started'
,
generator_started_l
=
K
EYVALUE
_SEP_V110
.
join
([
'generator_started'
,
generator_started
])
generator_started
])
header_extra_ls
=
[
timestamp_l
,
version_l
,
header_extra_ls
=
[
timestamp_l
,
version_l
,
earliest_bandwidth_l
,
file_created_l
,
generator_started_l
,
earliest_bandwidth_l
,
file_created_l
,
generator_started_l
,
lastest_bandwidth_l
,
lastest_bandwidth_l
,
...
...
Write
Preview
Supports
Markdown
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