Coverage for d7a/fs/file_header.py: 93%
42 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
« 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#
19import struct
21from d7a.fs.file_permissions import FilePermissions
22from d7a.fs.file_properties import FileProperties
23from d7a.support.schema import Validatable, Types
26class FileHeader(Validatable):
27 SCHEMA = [{
28 "permissions": Types.OBJECT(FilePermissions),
29 "properties": Types.OBJECT(FileProperties),
30 "alp_command_file_id": Types.BYTE(),
31 "interface_file_id": Types.BYTE(),
32 "file_size": Types.INTEGER(min=0, max=0xFFFFFFFF),
33 "allocated_size": Types.INTEGER(min=0, max=0xFFFFFFFF)
34 }]
36 def __init__(self, permissions, properties, alp_command_file_id, interface_file_id, file_size, allocated_size):
37 self.permissions = permissions
38 self.properties = properties
39 self.alp_command_file_id = alp_command_file_id
40 self.interface_file_id = interface_file_id
41 self.file_size = file_size
42 self.allocated_size = allocated_size
43 Validatable.__init__(self)
45 @staticmethod
46 def parse(s):
47 permissions = FilePermissions.parse(s)
48 properties = FileProperties.parse(s)
49 alp_command_file_id = s.read("uint:8")
50 interface_file_id = s.read("uint:8")
51 file_size = s.read("uint:32")
52 allocated_size = s.read("uint:32")
53 return FileHeader(permissions, properties, alp_command_file_id, interface_file_id, file_size, allocated_size)
55 def __iter__(self):
56 for byte in self.permissions:
57 yield byte
59 for byte in self.properties:
60 yield byte
62 yield self.alp_command_file_id
63 yield self.interface_file_id
64 for byte in bytearray(struct.pack(">I", self.file_size)):
65 yield byte
67 for byte in bytearray(struct.pack(">I", self.allocated_size)):
68 yield byte
70 def __eq__(self, other):
71 if isinstance(other, FileHeader):
72 return self.__dict__ == other.__dict__
74 return False
76 def __ne__(self, other):
77 return not self.__eq__(other)
79 def __str__(self):
80 return "permissions={}, properties=({}), alp_command_file_id={}, interface_file_id={}, file_size={}, allocated_size={}".format(
81 self.permissions, self.properties, self.alp_command_file_id, self.interface_file_id, self.file_size, self.allocated_size
82 )