from enum import Enum class MinetestCheckError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr("Error validating package: " + self.value) class ContentType(Enum): UNKNOWN = "unknown" TOOL = "tool" MODPACK = "modpack" GAME = "game" ASSETPACK = "texture pack" def isModLike(self): return self == ContentType.TOOL or self == ContentType.MODPACK def validate_same(self, other): """ Whether or not `other` is an acceptable type for this """ assert other if self == ContentType.TOOL: if not other.isModLike(): raise MinetestCheckError("Expected a tool or modpack, found " + other.value) elif self == ContentType.ASSETPACK: if other != ContentType.UNKNOWN and other != ContentType.ASSETPACK: raise MinetestCheckError("expected a " + self.value + ", found a " + other.value) elif other != self: raise MinetestCheckError("Expected a " + self.value + ", found a " + other.value) from .tree import PackageTreeNode, get_base_dir def build_tree(path, expected_type=None, author=None, repo=None, name=None): path = get_base_dir(path) root = PackageTreeNode(path, "/", author=author, repo=repo, name=name) assert root if expected_type: expected_type.validate_same(root.type) return root