File: FvLayoutPrint.py

package info (click to toggle)
edk2 2022.11-6%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 165,180 kB
  • sloc: ansic: 1,628,399; perl: 160,190; python: 135,478; asm: 49,448; cpp: 17,566; sh: 3,078; makefile: 2,986; pascal: 472; xml: 318; lisp: 35; sed: 5
file content (55 lines) | stat: -rw-r--r-- 1,923 bytes parent folder | download | duplicates (4)
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
## @file
# This file is used to define the printer for Bios layout.
#
# Copyright (c) 2021-, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
from utils.FmmtLogger import FmmtLogger as logger

def GetFormatter(layout_format: str):
    if layout_format == 'json':
        return JsonFormatter()
    elif layout_format == 'yaml':
        return YamlFormatter()
    elif layout_format == 'html':
        return HtmlFormatter()
    else:
        return TxtFormatter()

class Formatter(object):
    def dump(self, layoutdict, layoutlist, outputfile: str=None) -> None:
        raise NotImplemented

class JsonFormatter(Formatter):
    def dump(self,layoutdict: dict, layoutlist: list, outputfile: str=None) -> None:
        try:
            import json
        except:
            TxtFormatter().dump(layoutdict, layoutlist, outputfile)
            return
        print(outputfile)
        if outputfile:
            with open(outputfile,"w") as fw:
                json.dump(layoutdict, fw, indent=2)
        else:
            print(json.dumps(layoutdict,indent=2))

class TxtFormatter(Formatter):
    def LogPrint(self,layoutlist: list) -> None:
        for item in layoutlist:
            print(item)
        print('\n')

    def dump(self,layoutdict: dict, layoutlist: list, outputfile: str=None) -> None:
        logger.info('Binary Layout Info is saved in {} file.'.format(outputfile))
        with open(outputfile, "w") as f:
            for item in layoutlist:
                f.writelines(item + '\n')

class YamlFormatter(Formatter):
    def dump(self,layoutdict, layoutlist, outputfile = None):
        TxtFormatter().dump(layoutdict, layoutlist, outputfile)

class HtmlFormatter(Formatter):
    def dump(self,layoutdict, layoutlist, outputfile = None):
        TxtFormatter().dump(layoutdict, layoutlist, outputfile)