flipperzero-firmware/scripts/flipper/utils/fff.py
Albert Kharisov 84410c83b5
[FL-2183] [FL-2209] Dolphin Deeds, Level up, assets generation, refactoring (#965)
* Desktop: cleanup headers
* Get loader pubsub via record
* [FL-2183] Dolphin refactoring 2022.01
* Restruct animations assets structure
* Rename assets
* Cleanup headers
* Update Recording animation
* Add BadBattery animation
* Provide loader's pubsub via record
* Fix load/unload animations
* Scripts: add flipper format support, initial dolphin packager rework. Assets: internal and external dolphin.
* Sync internal meta.txt and manifest.txt
* Reorder, rename dolphin assets
* Split essential generated assets
* Add ReadMe for dolphin assets
* Separate essential blocking animations
* Scripts: full dolphin validation before packaging
* Assets, Scripts: dolphin external resources packer
* Github: update codeowners
* Scripts: proper slots handling in dolphin animation meta
* Scripts: correct frames enumeration and fix compiled assets.
* [FL-2209] Add Dolphin Deeds points and many more
* Remove excess frame_rate
* Change dolphin assets directory
* Scripts: add internal resource support to dolphin compiler
* Scripts: add internal assets generation, renaming
* Scripts: correct assert, renaming
* Code cleanup, documentation, fixes
* Update Levelup animations
* Rename essential -> blocking
* Fix Unlocked hint
* Scripts: rewrite Templite compiller, replace regexps with token parser, split block types into code and variable blocks. Update dolphin templates.
* Documentation: add key combos description and use information
* Scripts: cleanup templit, more debug info and add dev comment

Co-authored-by: あく <alleteam@gmail.com>
2022-01-29 12:20:41 +03:00

104 lines
3.0 KiB
Python

import logging
class FlipperFormatFile:
def __init__(self):
# Storage
self.lines = []
self.cursor = 0
# Logger
self.logger = logging.getLogger("FlipperFormatFile")
def _resetCursor(self):
self.cursor = 0
def nextLine(self):
line = None
while self.cursor < len(self.lines):
temp_line = self.lines[self.cursor].strip()
self.cursor += 1
if len(temp_line) > 0 and not temp_line.startswith("#"):
line = temp_line
break
if line is None:
raise EOFError()
return line
def readKeyValue(self):
line = self.nextLine()
data = line.split(":", 1)
if len(data) != 2:
self.logger.error(f"Incorrectly formated line {self.cursor}: `{line}`")
raise Exception("Unexpected line: not `key:value`")
return data[0].strip(), data[1].strip()
def readKey(self, key: str):
k, v = self.readKeyValue()
if k != key:
raise KeyError(f"Unexpected key {k} != {key}")
return v
def readKeyInt(self, key: str):
value = self.readKey(key)
return int(value) if value else None
def readKeyIntArray(self, key: str):
value = self.readKey(key)
return [int(i) for i in value.split(" ")] if value else None
def readKeyFloat(self, key: str):
value = self.readKey(key)
return float(value) if value else None
def writeLine(self, line: str):
self.lines.insert(self.cursor, line)
self.cursor += 1
def writeKey(self, key: str, value):
if isinstance(value, (str, int, float)):
pass
elif isinstance(value, (list, set)):
value = " ".join(map(str, value))
else:
raise Exception("Unknown value type")
self.writeLine(f"{key}: {value}")
def writeEmptyLine(self):
self.writeLine("")
def writeComment(self, text: str):
self.writeLine(f"# {text}")
def getHeader(self):
if self.cursor != 0 and len(self.lines) == 0:
raise Exception("Can't read header data: cursor not at 0 or file is empty")
# Read Filetype
key, value = self.readKeyValue()
if key != "Filetype":
raise Exception("Invalid Header: missing `Filetype`")
filetype = value
# Read Version
key, value = self.readKeyValue()
if key != "Version":
raise Exception("Invalid Header: missing `Version`")
version = int(value)
return filetype, version
def setHeader(self, filetype: str, version: int):
if self.cursor != 0 and len(self.lines) != 0:
raise Exception("Can't set header data: file is not empty")
self.writeKey("Filetype", filetype)
self.writeKey("Version", version)
def load(self, filename: str):
file = open(filename, "r")
self.lines = file.readlines()
def save(self, filename: str):
file = open(filename, "w")
file.write("\n".join(self.lines))