]>
Commit | Line | Data |
---|---|---|
a64b9449 CC |
1 | ## @file\r |
2 | # This file is used to define the printer for Bios layout.\r | |
3 | #\r | |
4 | # Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>\r | |
5 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | ##\r | |
7 | from utils.FmmtLogger import FmmtLogger as logger\r | |
8 | \r | |
9 | def GetFormatter(layout_format: str):\r | |
10 | if layout_format == 'json':\r | |
11 | return JsonFormatter()\r | |
12 | elif layout_format == 'yaml':\r | |
13 | return YamlFormatter()\r | |
14 | elif layout_format == 'html':\r | |
15 | return HtmlFormatter()\r | |
16 | else:\r | |
17 | return TxtFormatter()\r | |
18 | \r | |
19 | class Formatter(object):\r | |
20 | def dump(self, layoutdict, layoutlist, outputfile: str=None) -> None:\r | |
21 | raise NotImplemented\r | |
22 | \r | |
23 | class JsonFormatter(Formatter):\r | |
24 | def dump(self,layoutdict: dict, layoutlist: list, outputfile: str=None) -> None:\r | |
25 | try:\r | |
26 | import json\r | |
27 | except:\r | |
28 | TxtFormatter().dump(layoutdict, layoutlist, outputfile)\r | |
29 | return\r | |
30 | print(outputfile)\r | |
31 | if outputfile:\r | |
32 | with open(outputfile,"w") as fw:\r | |
33 | json.dump(layoutdict, fw, indent=2)\r | |
34 | else:\r | |
35 | print(json.dumps(layoutdict,indent=2))\r | |
36 | \r | |
37 | class TxtFormatter(Formatter):\r | |
38 | def LogPrint(self,layoutlist: list) -> None:\r | |
39 | for item in layoutlist:\r | |
40 | print(item)\r | |
41 | print('\n')\r | |
42 | \r | |
43 | def dump(self,layoutdict: dict, layoutlist: list, outputfile: str=None) -> None:\r | |
44 | logger.info('Binary Layout Info is saved in {} file.'.format(outputfile))\r | |
45 | with open(outputfile, "w") as f:\r | |
46 | for item in layoutlist:\r | |
47 | f.writelines(item + '\n')\r | |
48 | \r | |
49 | class YamlFormatter(Formatter):\r | |
50 | def dump(self,layoutdict, layoutlist, outputfile = None):\r | |
51 | TxtFormatter().dump(layoutdict, layoutlist, outputfile)\r | |
52 | \r | |
53 | class HtmlFormatter(Formatter):\r | |
54 | def dump(self,layoutdict, layoutlist, outputfile = None):\r | |
55 | TxtFormatter().dump(layoutdict, layoutlist, outputfile)\r |