2 from . import MinetestCheckError, ContentType
3 from .config import parse_conf
5 def get_base_dir(path):
6 if not os.path.isdir(path):
7 raise IOError("Expected dir")
9 root, subdirs, files = next(os.walk(path))
10 if len(subdirs) == 1 and len(files) == 0:
11 return get_base_dir(path + "/" + subdirs[0])
16 def detect_type(path):
17 if os.path.isfile(path + "/game.conf"):
18 return ContentType.GAME
19 elif os.path.isfile(path + "/init.lua"):
20 return ContentType.MOD
21 elif os.path.isfile(path + "/modpack.txt") or \
22 os.path.isfile(path + "/modpack.conf"):
23 return ContentType.MODPACK
24 elif os.path.isdir(path + "/mods"):
25 return ContentType.GAME
26 elif os.path.isfile(path + "/texture_pack.conf"):
27 return ContentType.TXP
29 return ContentType.UNKNOWN
32 class PackageTreeNode:
33 def __init__(self, baseDir, relative, author=None, repo=None, name=None):
35 self.baseDir = baseDir
36 self.relative = relative
44 self.type = detect_type(baseDir)
47 if self.type == ContentType.GAME:
48 if not os.path.isdir(baseDir + "/mods"):
49 raise MinetestCheckError(("game at {} does not have a mods/ folder").format(self.relative))
50 self.add_children_from_mod_dir(baseDir + "/mods")
51 elif self.type == ContentType.MODPACK:
52 self.add_children_from_mod_dir(baseDir)
54 def getMetaFilePath(self):
56 if self.type == ContentType.GAME:
57 filename = "game.conf"
58 elif self.type == ContentType.MOD:
60 elif self.type == ContentType.MODPACK:
61 filename = "modpack.conf"
62 elif self.type == ContentType.TXP:
63 filename = "texture_pack.conf"
67 return self.baseDir + "/" + filename
75 with open(self.getMetaFilePath(), "r") as myfile:
76 conf = parse_conf(myfile.read())
77 for key, value in conf.items():
83 if not "description" in result:
85 with open(self.baseDir + "/description.txt", "r") as myfile:
86 result["description"] = myfile.read()
92 pattern = re.compile("^([a-z0-9_]+)\??$")
93 if not "depends" in result and not "optional_depends" in result:
95 with open(self.baseDir + "/depends.txt", "r") as myfile:
96 contents = myfile.read()
99 for line in contents.split("\n"):
101 if pattern.match(line):
102 if line[len(line) - 1] == "?":
103 soft.append( line[:-1])
107 result["depends"] = hard
108 result["optional_depends"] = soft
114 if "depends" in result:
115 result["depends"] = [x.strip() for x in result["depends"].split(",")]
116 if "optional_depends" in result:
117 result["optional_depends"] = [x.strip() for x in result["optional_depends"].split(",")]
120 if "name" in result and not "title" in result:
121 result["title"] = result["name"].replace("_", " ").title()
123 # Calculate short description
124 if "description" in result:
125 desc = result["description"]
126 idx = desc.find(".") + 1
127 cutIdx = min(len(desc), 200 if idx < 5 else idx)
128 result["short_description"] = desc[:cutIdx]
131 self.name = result["name"]
136 def add_children_from_mod_dir(self, dir):
137 for entry in next(os.walk(dir))[1]:
138 path = os.path.join(dir, entry)
139 if not entry.startswith('.') and os.path.isdir(path):
140 child = PackageTreeNode(path, self.relative + entry + "/", name=entry)
141 if not child.type.isModLike():
142 raise MinetestCheckError(("Expecting mod or modpack, found {} at {} inside {}") \
143 .format(child.type.value, child.relative, self.type.value))
145 self.children.append(child)
147 def getModNames(self):
148 return self.fold("name", type=ContentType.MOD)
150 # attr: Attribute name
151 # key: Key in attribute
152 # retval: Accumulator
153 # type: Filter to type
154 def fold(self, attr, key=None, retval=None, type=None):
158 # Iterate through children
159 for child in self.children:
160 child.fold(attr, key, retval, type)
163 if type and type != self.type:
167 at = getattr(self, attr)
172 value = at if key is None else at.get(key)
173 if isinstance(value, list):
181 return self.meta.get(key)
184 for child in self.children: