-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
map_file_loader.py
60 lines (49 loc) · 2.04 KB
/
map_file_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import sys
import yaml
from logger import logger
from section import Section
from gnu_linker_map_parser import GNULinkerMapParser
class MapFileLoader:
"""
Takes input file provided by user and loads it in memory for further processing.
Depending on the type of file (.map or .yaml) will include an additional conversion step and
create a temporary .yaml file
"""
def __init__(self, file, convert):
self.input_filename = file
self.convert = convert
def parse(self):
_, file_extension = os.path.splitext(self.input_filename)
if file_extension == '.map':
self.parse_map(self.input_filename)
if self.convert:
logger.info(".map file converted and saved as map.yaml")
exit(0)
return self.parse_yaml('map.yaml')
if file_extension in ['.yaml', '.yml']:
if self.convert:
logger.error("--convert flag requires a .map file")
exit(-1)
return self.parse_yaml(self.input_filename)
logger.error(f"Wrong map file extension: '{file_extension}'. Use .map or .yaml files")
sys.exit(-1)
@staticmethod
def parse_yaml(filename):
sections = []
with open(filename, 'r', encoding='utf-8') as file:
y = yaml.safe_load(file)
for element in y['map']:
sections.append(Section(address=element['address'],
size=element['size'],
id=element['id'],
name=element.get('name'),
parent=element.get('parent', 'none'),
_type=element.get('type', 'area'),
flags=element.get('flags', '')
)
)
return sections
@staticmethod
def parse_map(input_filename):
GNULinkerMapParser(input_filename=input_filename, output_filename='map.yaml').parse()