Coverage for d7a/alp/operations/responses.py: 97%

32 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-24 08:03 +0200

1# 

2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV. 

3# 

4# This file is part of pyd7a. 

5# See https://github.com/Sub-IoT/pyd7a for further info. 

6# 

7# Licensed under the Apache License, Version 2.0 (the "License"); 

8# you may not use this file except in compliance with the License. 

9# You may obtain a copy of the License at 

10# 

11# http://www.apache.org/licenses/LICENSE-2.0 

12# 

13# Unless required by applicable law or agreed to in writing, software 

14# distributed under the License is distributed on an "AS IS" BASIS, 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18# 

19 

20# author: Christophe VG <contact@christophe.vg> 

21# class implementation of responses 

22from bitstring import ConstBitStream 

23 

24from d7a.alp.operands.file_header import FileHeaderOperand 

25from d7a.alp.operations.operation import Operation 

26 

27from d7a.alp.operands.file import Data 

28from d7a.system_files.system_files import SystemFiles 

29 

30 

31class ReturnFileData(Operation): 

32 def __init__(self, custom_files_class=None, *args, **kwargs): 

33 self.file_type = None 

34 self.file_data_parsed = None 

35 self.op = 32 

36 self.operand_class = Data 

37 super(ReturnFileData, self).__init__(*args, **kwargs) 

38 self.try_parse_file(SystemFiles) 

39 if custom_files_class is not None: 

40 self.try_parse_file(custom_files_class) 

41 

42 def try_parse_file(self, files_class): 

43 try: 

44 file_type = files_class().files[files_class.enum_class(int(self.operand.offset.id))] 

45 except: 

46 return 

47 if (file_type is not None) and (file_type.length >= self.operand.length.value): 

48 self.file_type = file_type 

49 try: 

50 self.file_data_parsed = file_type.parse(ConstBitStream(bytearray(self.operand.data)), self.operand.offset.offset.value, self.operand.length.value) 

51 except: 

52 self.file_type = None 

53 self.file_data_parsed = None 

54 

55class ReturnFileHeader(Operation): 

56 def __init__(self, *args, **kwargs): 

57 self.op = 33 

58 self.operand_class = FileHeaderOperand 

59 super(ReturnFileHeader, self).__init__(*args, **kwargs)