v3bwfile.py 1.5 KB
Newer Older
juga's avatar
juga committed
1
2
3
4
# -*- coding: utf-8 -*-
"""Classes and functions that create the bandwidth measurements document
(v3bw) used by bandwidth authorities."""

5
import time
juga's avatar
juga committed
6
import logging
7
8
from sbws import __version__
from sbws.globals import SPEC_VERSION
juga's avatar
juga committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

log = logging.getLogger(__name__)


class V3BwHeader(object):
    """
    Create a bandwidth measurements (V3bw) header
    following bandwidth measurements document spec version 1.1.0.

    :param int timestamp: timestamp in Unix Epoch seconds when the document
                          is created
    :param str version: the spec version
    :param str software: the name of the software that generates this
    :param str software_version: the version of the software
    """
    def __init__(self, timestamp=None, version=SPEC_VERSION, software='sbws',
juga's avatar
juga committed
25
                 software_version=__version__, **kwargs):
26
        self.timestamp = timestamp or int(time.time())
juga's avatar
juga committed
27
28
29
        self.version = version
        self.software = software
        self.software_version = software_version
juga's avatar
juga committed
30
31
        if kwargs.get('earlier_result_ts'):
            self.earlier_result = kwargs['earlier_result_ts']
juga's avatar
juga committed
32
33
34

    def __str__(self):
        """Return header string following spec version 1.1.0."""
juga's avatar
juga committed
35
36
37
38
39
40
        # sorting the list to generate determinist headers
        kv_headers = ' '.join(sorted(['='.join([k, str(v)])
                                      for k, v in self.__dict__.items()]))
        header = '\n'.join([str(self.timestamp), kv_headers, ''])
        log.debug('header %s', header)
        return header