{{
import datetime
import malcat
from filetypes.PYC import MAGIC_TO_VERSION

def pval(key, val=None, fmt="{}", pa=False, kalign=30, valign=0, formated=False):
    r = "[normal]{}[/normal]:".format(key)
    lr = len(r) - 17
    if lr < kalign:
        r += (" "*(kalign-lr))
    if isinstance(val, malcat.StructAccess):
        if val.has_enum and val.enum:
            d = "[color6]{}[/color6]([dim]{}[/dim])".format(val.enum, fmt.format(val.value))
            formated = True
            ld = len(d) - len("[color3][/color3][dim][/dim]")
        else:
            d = fmt.format(val.value)
            ld = len(d)
        val = val.value
    else:
        d = fmt.format(val)
        ld = len(d)
    if not formated:
        if type(val) == int: 
            if pa:
                grp = "fa"
            elif val == 0:
                grp = "dim"
            else:
                grp = "normal"
        else:
            grp = "normal"
        d = "[{}]{}[/{}]".format(grp,d,grp)
    if ld < valign:
        d += (" "*(valign-ld))
    write(r+d)  

def bitfieldpp(fa):
    s = []
    for i in range(fa.count - 1, -1, -1):
        bit = fa.at(i)
        if bit.value:
            s.append("[color5]{}[/color5]([dim]{:X}[/dim])".format(bit.name, 1 << i))
    if s:
        return  " + ".join(s)
    else:
        return "[dim]0[/dim]"

def pbitfield(key, fa, kalign=30):    
    r = "[normal]{}[/normal]:".format(key)
    lr = len(r) - 17
    if lr < kalign:
        r += (" "*(kalign-lr))
    write(r + bitfieldpp(fa))

module = analysis.struct.Module
}}
[color1]Python Header[/color1]
  {{pval("Python version", ".".join(map(str, analysis.parser.version)), valign=24)}}{{ if "Flags" in analysis.struct["PythonHeader"]: }} {{pbitfield("Flags", analysis.struct.PythonHeader.Flags)}}{{ :endif}}
{{ if "Timestamp" in analysis.struct["PythonHeader"]: }}
  {{pval("Timestamp", analysis.struct.PythonHeader.Timestamp.value.strftime('%Y-%m-%d %H:%M:%S'))}}
{{ :endif}}
{{ if "Size" in analysis.struct["PythonHeader"]:}}
  {{pval("Size", analysis.struct.PythonHeader.Size, fmt="{:d} bytes", valign=24)}}
{{ :endif}}

[color1]Module[/color1]
  {{pval("Name", analysis.parser.read_object(module["Name"]), valign=24)}} {{pval("File name", analysis.parser.read_object(module["Filename"]))}}

