Checkpoint initial implementation

This commit is contained in:
Andrew Miner
2025-10-06 17:41:18 -06:00
parent 773e0a3d85
commit a8b6e6b15e
106 changed files with 3012 additions and 0 deletions
@@ -0,0 +1,19 @@
from tungston.core.world import World
from tungston.generator.markdown.report import Report
# Class ############################################################################################
class MineralReport(Report):
def build(self):
for mineral in self.world.minerals.all():
self.line(f"# Mineral: {mineral.name}")
self.line()
self.indent()
for replacement in mineral.replacements:
self.line(str(replacement))
self.outdent()
self.line()
@@ -0,0 +1,55 @@
from pytest import fixture
from tungston.core.geology.mineral import Mineral
from tungston.core.geology.mineralcatalog import MineralCatalog
from tungston.core.geology.replacement import Replacement
from tungston.core.world import World
from tungston.generator.markdown.mineralreport import MineralReport
import textwrap
# Fixtures #########################################################################################
@fixture(name="copper")
def createCopper():
yield Mineral("copper", [
Replacement("#minecraft:stone_replaceables", "minecraft:copper_ore"),
Replacement("#minecraft:deepslate_replaceables", "minecraft:deepslate_copper_ore")
])
@fixture(name="iron")
def createIron():
yield Mineral("iron", [
Replacement("#minecraft:stone_replaceables", "minecraft:iron_ore"),
Replacement("#minecraft:deepslate_replaceables", "minecraft:deepslate_iron_ore")
])
@fixture(name="minerals")
def createMineralCatalog(copper, iron):
yield MineralCatalog([copper, iron])
@fixture(name="world")
def createWorld(minerals):
yield World(None, None, minerals, None)
@fixture(name="report")
def createReport(world):
report = MineralReport(world)
report.build()
yield report
# Tests ############################################################################################
def test_report(report):
assert str(report) == textwrap.dedent("""
# Mineral: copper
#minecraft:stone_replaceables => minecraft:copper_ore
#minecraft:deepslate_replaceables => minecraft:deepslate_copper_ore
# Mineral: iron
#minecraft:stone_replaceables => minecraft:iron_ore
#minecraft:deepslate_replaceables => minecraft:deepslate_iron_ore
""").strip()
+70
View File
@@ -0,0 +1,70 @@
from collections.abc import Iterator
from tungston.core.world import World
# Constants ########################################################################################
INDENT = " "
# Class ############################################################################################
class Report:
def __init__(self, world:World):
self.world = world
self.reset()
def __str__(self) -> str:
return "\n".join(self._lines).strip()
def __repr__(self) -> str:
return (
"Report{" +
f"lines: {len(self._lines)} lines, " +
f"lineBuffer: {len(self._lineBuffer)} chunks, " +
f"indent: {self._indent}" +
"}"
)
def build(self):
raise NotImplementedError()
def reset(self) -> "Report":
self._lineBuffer = []
self._lines = []
self._indent = 0
return self
def asLines(self) -> Iterator[str]:
for line in self._lines:
yield line
# Helper Methods ###########################################################
def indent(self) -> "Report":
self._indent += 1
self._lineBuffer.insert(0, INDENT)
return self
def line(self, text:str="") -> "Report":
self.text(text)
self._lines.append("".join(self._lineBuffer))
self._lineBuffer = []
while len(self._lineBuffer) < self._indent:
self._lineBuffer.append(INDENT)
return self
def outdent(self) -> "Report":
self._indent = max(0, self._indent - 1)
if self._lineBuffer:
if self._lineBuffer[0] == INDENT:
self._lineBuffer.pop()
return self
def text(self, text:str) -> "Report":
self._lineBuffer.append(str(text))
return self
@@ -0,0 +1,31 @@
from pytest import fixture
from tungston.generator.markdown.report import Report
# Fixtures #########################################################################################
@fixture(name="emptyReport")
def createEmptyReport():
return Report()
@fixture(name="report")
def createReport():
return (
Report(None)
.text("alpha")
.line("bravo")
.indent()
.line("charlie")
.indent()
.line("delta")
.outdent()
.line("echo")
)
# Tests ############################################################################################
def test_repr(report):
assert repr(report) == "Report{lines: 4 lines, lineBuffer: 1 chunks, indent: 1}"
def test_str(report):
assert str(report) == "alphabravo\n charlie\n delta\n echo"