]> git.lizzy.rs Git - cheatdb.git/blob - app/tasks/minetestcheck/__init__.py
dbc57adb4e08445cec4755e8b1f36fcd4571e014
[cheatdb.git] / app / tasks / minetestcheck / __init__.py
1 from enum import Enum
2
3 class MinetestCheckError(Exception):
4         def __init__(self, value):
5                 self.value = value
6         def __str__(self):
7                 return repr("Error validating package: " + self.value)
8
9 class ContentType(Enum):
10         UNKNOWN = "unknown"
11         MOD = "mod"
12         MODPACK = "modpack"
13         GAME = "game"
14         TXP = "texture pack"
15
16         def isModLike(self):
17                 return self == ContentType.MOD or self == ContentType.MODPACK
18
19         def validate_same(self, other):
20                 """
21                 Whether or not `other` is an acceptable type for this
22                 """
23                 assert(other)
24
25                 if self == ContentType.MOD:
26                         if not other.isModLike():
27                                 raise MinetestCheckError("expected a mod or modpack, found " + other.value)
28
29                 elif self == ContentType.TXP:
30                         if other != ContentType.UNKNOWN and other != ContentType.TXP:
31                                 raise MinetestCheckError("expected a " + self.value + ", found a " + other.value)
32
33                 elif other != self:
34                         raise MinetestCheckError("expected a " + self.value + ", found a " + other.value)
35
36
37 from .tree import PackageTreeNode, get_base_dir
38
39 def build_tree(path, expected_type=None, author=None, repo=None, name=None):
40         path = get_base_dir(path)
41
42         root = PackageTreeNode(path, "/", author=author, repo=repo, name=name)
43         assert(root)
44
45         if expected_type:
46                 expected_type.validate_same(root.type)
47
48         return root