Coverage for d7a/alp/operands/file.py: 84%

38 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 implementations of File {*} Operands 

22from d7a.alp.operands.length import Length 

23from d7a.alp.operands.offset import Offset 

24from d7a.support.schema import Validatable, Types 

25 

26 

27class Data(Validatable): 

28 

29 SCHEMA = [{ 

30 "offset" : Types.OBJECT(Offset), 

31 "length" : Types.OBJECT(Length), 

32 "data" : Types.BYTES() 

33 }] 

34 

35 def __init__(self, data=[], offset=Offset()): 

36 self.offset = offset 

37 self.data = data 

38 self.length = Length(len(data)) 

39 super(Data, self).__init__() 

40 

41 # for consistency with schema, e.g. if using generic attribute conversion, etc 

42 # @property 

43 # def length(self): 

44 # return len(self.data) 

45 

46 # the Python way ;-) 

47 def __len__(self): 

48 return self.length.value 

49 

50 def __iter__(self): 

51 for byte in self.offset: yield byte 

52 for byte in self.length: yield byte 

53 for byte in self.data: yield byte 

54 

55 def __str__(self): 

56 return "{}, length={}, data={}".format(self.offset, self.length, self.data) 

57 

58 

59class DataRequest(Validatable): 

60 

61 SCHEMA = [{ 

62 "offset" : Types.OBJECT(Offset), 

63 "length" : Types.OBJECT(Length) 

64 }] 

65 

66 def __init__(self, length=Length(), offset=Offset()): 

67 self.offset = offset 

68 self.length = length 

69 super(DataRequest, self).__init__() 

70 

71 def __iter__(self): 

72 for byte in self.offset: yield byte 

73 for byte in self.length: yield byte 

74 

75 def __str__(self): 

76 return "{}, length={}".format(self.offset, self.length) 

77 

78 

79class FileIdOperand(Validatable): 

80 

81 SCHEMA = [{ 

82 "file_id": Types.BYTE() 

83 }] 

84 

85 def __init__(self, file_id): 

86 self.file_id = file_id 

87 super(FileIdOperand, self).__init__() 

88 

89 def __iter__(self): 

90 yield self.file_id 

91 

92 def __str__(self): 

93 return "file-id={}".format(self.file_id)