Completed
Pull Request — master (#58)
by
unknown
01:10
created

FileReportBackend._next_num()   A

Complexity

Conditions 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 11
rs 9.2
1
2
from .base_report_backend import BaseReportBackend
3
4
from ..storage import Storage
5
from ..utils import safe_dumps
6
7
8
class FileReportBackend(BaseReportBackend):
9
    def __init__(self, config):
10
        super(FileReportBackend, self).__init__(config)
11
        self.storage = Storage(config.getoption("benchmark_storage"),
12
                               default_machine_id=self.machine_id,
13
                               logger=self.logger)
14
15
    @property
16
    def _next_num(self):
17
        files = self.storage.query("[0-9][0-9][0-9][0-9]_*")
18
        files.sort(reverse=True)
19
        if not files:
20
            return "0001"
21
        for f in files:
22
            try:
23
                return "%04i" % (int(str(f.name).split('_')[0]) + 1)
24
            except ValueError:
25
                raise
26
27
    def _save(self, output_json, save):
28
        output_file = self.storage.get("%s_%s.json" % (self._next_num, save))
29
        self.logger.info("output_file " + str(output_file))
30
        self.logger.info("save " + str(save))
31
        assert not output_file.exists()
32
        with output_file.open('wb') as fh:
33
            fh.write(safe_dumps(output_json, ensure_ascii=True, indent=4).encode())
34
        self.logger.info("Saved benchmark data in: %s" % output_file)
35
36
    def _load(self, id_prefix=None):
37
        if id_prefix is None:
38
            return self.storage.load('[0-9][0-9][0-9][0-9]_')
39
        else:
40
            return self.storage.load(id_prefix)
41
42