def testInitFromUrl(self): """Checks that XML and data files are present and previews generated.""" with makeTestRepoEnv("clone"): self.assertTrue(exists(join(repoPath(), "tagCategories.xml"))) for filename in ("foobar1.xml", "example.cpp", "solution_EN.png", "solution_DE.png", "exercise_DE.png"): self.assertTrue(exists(join(repoPath(), "exercises", "foobar1", filename)), "{} missing".format(filename))
def initTagsTable(conn): """Initialize the *tags* table in the SQLite database from the tagCategories.xml file. If the file does not exist, a barebone tree containing only the "uncategorized" category is created and used instead. Returns the root of the XML tree. """ cursor = conn.cursor() cursor.execute("DELETE FROM tags;") import exdb from exdb.repo import repoPath initialized = False if exdb.instancePath is not None: tagFile = join(repoPath(), "tagCategories.xml") initialized = True tree = etree.parse(tagFile).getroot() if initialized and exists(tagFile) else initialTree() uncat = tree.find("category[@name='uncategorized']") if uncat is None: uncat = E.category(name='uncategorized') tree.append(uncat) for node in tree.iterdescendants(): parents = list(reversed([el.get("id") for el in node.iterancestors()][:-1])) matpath = '.'.join(map(str, parents)) + '.' if matpath[0] != '.': matpath = "." + matpath cursor.execute("INSERT INTO tags(name, is_tag, mat_path) VALUES (?, ?, ?)", (node.get("name"), node.tag == "tag", matpath)) node.set("id", str(cursor.lastrowid)) node.set("matpath", matpath) conn.commit() return tree
def initTagsTable(conn): """Initialize the *tags* table in the SQLite database from the tagCategories.xml file. If the file does not exist, a barebone tree containing only the "uncategorized" category is created and used instead. Returns the root of the XML tree. """ cursor = conn.cursor() cursor.execute("DELETE FROM tags;") import exdb from exdb.repo import repoPath initialized = False if exdb.instancePath is not None: tagFile = join(repoPath(), "tagCategories.xml") initialized = True tree = etree.parse(tagFile).getroot( ) if initialized and exists(tagFile) else initialTree() uncat = tree.find("category[@name='uncategorized']") if uncat is None: uncat = E.category(name='uncategorized') tree.append(uncat) for node in tree.iterdescendants(): parents = list( reversed([el.get("id") for el in node.iterancestors()][:-1])) matpath = '.'.join(map(str, parents)) + '.' if matpath[0] != '.': matpath = "." + matpath cursor.execute( "INSERT INTO tags(name, is_tag, mat_path) VALUES (?, ?, ?)", (node.get("name"), node.tag == "tag", matpath)) node.set("id", str(cursor.lastrowid)) node.set("matpath", matpath) conn.commit() return tree
def storeTree(tree): """Store the XML *tree* to the appropriate tagCategories.xml file.""" from exdb.repo import repoPath from copy import deepcopy tree = deepcopy(tree) for element in tree.iter(): try: del element.attrib["id"] except KeyError: pass try: del element.attrib["mat_path"] except KeyError: pass with open(join(repoPath(), "tagCategories.xml"), "wt") as f: f.write(etree.tostring(tree, pretty_print=True, encoding=str))