Skip to content

Commit

Permalink
Option to generate non human readable json
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Aug 30, 2023
1 parent 29a243f commit 8d9d247
Show file tree
Hide file tree
Showing 11 changed files with 236,943 additions and 236,920 deletions.
8 changes: 5 additions & 3 deletions src/mapfile_parser/frontends/jsonify.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from .. import mapfile


def doJsonify(mapPath: Path, outputPath: Path|None) -> int:
def doJsonify(mapPath: Path, outputPath: Path|None, humanReadable: bool=True) -> int:
mapFile = mapfile.MapFile()
mapFile.readMapFile(mapPath)

jsonStr = json.dumps(mapFile.toJson(), indent=4)
jsonStr = json.dumps(mapFile.toJson(humanReadable=humanReadable), indent=4)

if outputPath is None:
print(jsonStr)
Expand All @@ -30,13 +30,15 @@ def doJsonify(mapPath: Path, outputPath: Path|None) -> int:
def processArguments(args: argparse.Namespace):
mapPath: Path = args.mapfile
outputPath: Path|None = Path(args.output) if args.output is not None else None
machine: bool = args.machine

exit(doJsonify(mapPath, outputPath))
exit(doJsonify(mapPath, outputPath, humanReadable=not machine))

def addSubparser(subparser: argparse._SubParsersAction[argparse.ArgumentParser]):
parser = subparser.add_parser("jsonify", help="Converts a mapfile into a json format.")

parser.add_argument("mapfile", help="Path to a map file", type=Path)
parser.add_argument("-o", "--output", help="Output path of for the generated json. If omitted then stdout is used instead.")
parser.add_argument("-m", "--machine", help="Emit numbers as numbers instead of outputting them as pretty strings.", action="store_true")

parser.set_defaults(func=processArguments)
85 changes: 53 additions & 32 deletions src/mapfile_parser/mapfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,24 @@ def getVromStr(self) -> str:
return "None"
return f"0x{self.vrom:06X}"

def serializeSize(self) -> str|None:
def serializeVram(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.vram:08X}"
return self.vram

def serializeSize(self, humanReadable: bool=True) -> str|int|None:
if self.size is None:
return None
return f"0x{self.size:X}"
if humanReadable:
return f"0x{self.size:X}"
return self.size

def serializeVrom(self) -> str|None:
def serializeVrom(self, humanReadable: bool=True) -> str|int|None:
if self.vrom is None:
return None
return f"0x{self.size:X}"
if humanReadable:
return f"0x{self.vrom:06X}"
return self.vrom

@staticmethod
def printCsvHeader():
Expand All @@ -103,12 +112,12 @@ def toCsvHeader() -> str:
def toCsv(self) -> str:
return f"{self.name},{self.vram:08X},{self.size}"

def toJson(self) -> dict[str, Any]:
def toJson(self, humanReadable: bool=True) -> dict[str, Any]:
result: dict[str, Any] = {
"name": self.name,
"vram": self.getVramStr(),
"size": self.serializeSize(),
"vrom": self.serializeVrom(),
"vram": self.serializeVram(humanReadable=humanReadable),
"size": self.serializeSize(humanReadable=humanReadable),
"vrom": self.serializeVrom(humanReadable=humanReadable),
}

return result
Expand Down Expand Up @@ -138,16 +147,22 @@ def isNoloadSection(self) -> bool:
return self.sectionType == ".bss"


def serializeVram(self) -> str|None:
return f"0x{self.vram:08X}"
def serializeVram(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.vram:08X}"
return self.vram

def serializeSize(self) -> str|None:
return f"0x{self.size:X}"
def serializeSize(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.size:X}"
return self.size

def serializeVrom(self) -> str|None:
def serializeVrom(self, humanReadable: bool=True) -> str|int|None:
if self.vrom is None:
return None
return f"0x{self.vrom:06X}"
if humanReadable:
return f"0x{self.vrom:06X}"
return self.vrom


def getName(self) -> Path:
Expand Down Expand Up @@ -232,18 +247,18 @@ def toCsv(self, printVram: bool=True) -> str:
ret += f"{self.filepath},{self.sectionType},{symCount},{maxSize},{self.size},{averageSize:0.2f}"
return ret

def toJson(self) -> dict[str, Any]:
def toJson(self, humanReadable: bool=True) -> dict[str, Any]:
fileDict: dict[str, Any] = {
"filepath": str(self.filepath),
"sectionType": self.sectionType,
"vram": self.serializeVram(),
"size": self.serializeSize(),
"vrom": self.serializeVrom(),
"vram": self.serializeVram(humanReadable=humanReadable),
"size": self.serializeSize(humanReadable=humanReadable),
"vrom": self.serializeVrom(humanReadable=humanReadable),
}

symbolsList = []
for symbol in self._symbols:
symbolsList.append(symbol.toJson())
symbolsList.append(symbol.toJson(humanReadable=humanReadable))

fileDict["symbols"] = symbolsList
return fileDict
Expand Down Expand Up @@ -280,14 +295,20 @@ class Segment:
vrom: int
_filesList: list[File] = dataclasses.field(default_factory=list)

def serializeVram(self) -> str|None:
return f"0x{self.vram:08X}"
def serializeVram(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.vram:08X}"
return self.vram

def serializeSize(self) -> str|None:
return f"0x{self.size:X}"
def serializeSize(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.size:X}"
return self.size

def serializeVrom(self) -> str|None:
return f"0x{self.vrom:06X}"
def serializeVrom(self, humanReadable: bool=True) -> str|int|None:
if humanReadable:
return f"0x{self.vrom:06X}"
return self.vrom


def filterBySectionType(self, sectionType: str) -> Segment:
Expand Down Expand Up @@ -384,17 +405,17 @@ def toCsvSymbols(self) -> str:
ret += "\n"
return ret

def toJson(self) -> dict[str, Any]:
def toJson(self, humanReadable: bool=True) -> dict[str, Any]:
segmentDict: dict[str, Any] = {
"name": self.name,
"vram": self.serializeVram(),
"size": self.serializeSize(),
"vrom": self.serializeVrom(),
"vram": self.serializeVram(humanReadable=humanReadable),
"size": self.serializeSize(humanReadable=humanReadable),
"vrom": self.serializeVrom(humanReadable=humanReadable),
}

filesList = []
for file in self._filesList:
filesList.append(file.toJson())
filesList.append(file.toJson(humanReadable=humanReadable))

segmentDict["files"] = filesList

Expand Down Expand Up @@ -758,10 +779,10 @@ def toCsvSymbols(self) -> str:
ret += segment.toCsvSymbols()
return ret

def toJson(self) -> dict[str, Any]:
def toJson(self, humanReadable: bool=True) -> dict[str, Any]:
segmentsList = []
for segment in self._segmentsList:
segmentsList.append(segment.toJson())
segmentsList.append(segment.toJson(humanReadable=humanReadable))

result: dict[str, Any] = {
"segments": segmentsList
Expand Down
Loading

0 comments on commit 8d9d247

Please sign in to comment.