diff --git a/manuskript/load_save/version_1.py b/manuskript/load_save/version_1.py index 8536eb9..39f1b7d 100644 --- a/manuskript/load_save/version_1.py +++ b/manuskript/load_save/version_1.py @@ -54,8 +54,6 @@ characterMap = OrderedDict([ (Character.notes, "Notes") ]) -# If true, logs infos while saving and loading. -LOG = False def formatMetaData(name, value, tabLength=10): @@ -95,11 +93,6 @@ def slugify(name): return newName -def log(*args): - if LOG: - print(" ".join(str(a) for a in args)) - - def saveProject(zip=None): """ Saves the project. If zip is False, the project is saved as a multitude of plain-text files for the most parts @@ -113,7 +106,7 @@ def saveProject(zip=None): if zip == None: zip = settings.saveToZip - log("\n\nSaving to:", "zip" if zip else "folder") + LOGGER.info("Saving to: %s", "zip" if zip else "folder") # List of files to be written files = [] @@ -344,7 +337,7 @@ def saveProject(zip=None): folder = os.path.splitext(os.path.basename(project))[0] # Debug - log("\nSaving to folder", folder) + LOGGER.debug("Saving to folder %s", folder) # If cache is empty (meaning we haven't loaded from disk), we wipe folder, just to be sure. if not cache: @@ -361,7 +354,7 @@ def saveProject(zip=None): # Move the old file to the new place try: os.replace(oldPath, newPath) - log("* Renaming/moving {} to {}".format(old, new)) + LOGGER.debug("* Renaming/moving {} to {}".format(old, new)) except FileNotFoundError: # Maybe parent folder has been renamed pass @@ -371,7 +364,7 @@ def saveProject(zip=None): for f in cache: f2 = f.replace(old, new) if f2 != f: - log(" * Updating cache:", f, f2) + LOGGER.debug(" * Updating cache: %s, %s", f, f2) cache2[f2] = cache[f] cache = cache2 @@ -382,7 +375,7 @@ def saveProject(zip=None): # Check if content is in cache, and write if necessary if path not in cache or cache[path] != content: - log("* Writing file {} ({})".format(path, "not in cache" if path not in cache else "different")) + LOGGER.debug("* Writing file {} ({})".format(path, "not in cache" if path not in cache else "different")) # mode = "w" + ("b" if type(content) == bytes else "") if type(content) == bytes: with open(filename, "wb") as f: @@ -396,7 +389,7 @@ def saveProject(zip=None): # Removing phantoms for path in [p for p in cache if p not in [p for p, c in files]]: filename = os.path.join(dir, folder, path) - log("* Removing", path) + LOGGER.debug("* Removing %s", path) if os.path.isdir(filename): shutil.rmtree(filename) @@ -413,7 +406,7 @@ def saveProject(zip=None): newDir = os.path.join(root, dir) try: os.removedirs(newDir) - log("* Removing empty directory:", newDir) + LOGGER.debug("* Removing empty directory: %s", newDir) except: # Directory not empty, we don't remove. pass @@ -539,8 +532,8 @@ def exportOutlineItem(root): lp = child._lastPath if lp and spath != lp: moves.append((lp, spath)) - log(child.title(), "has been renamed (", lp, " → ", spath, ")") - log(" → We mark for moving:", lp) + LOGGER.debug("%s has been renamed (%s → %s)", child.title(), lp, spath) + LOGGER.debug(" → We mark for moving: %s", lp) # Updates item last's path child._lastPath = spath @@ -556,7 +549,7 @@ def exportOutlineItem(root): files.append((spath, content)) else: - log("Unknown type") + LOGGER.debug("Unknown type: %s", child.type()) f, m, r = exportOutlineItem(child) files += f @@ -634,7 +627,7 @@ def loadProject(project, zip=None): #################################################################################################################### # Read and store everything in a dict - log("\nLoading {} ({})".format(project, "ZIP" if zip else "not zip")) + LOGGER.debug("Loading {} ({})".format(project, "zip" if zip else "folder")) if zip: files = loadFilesFromZip(project) @@ -698,7 +691,7 @@ def loadProject(project, zip=None): mdl = mw.mdlLabels mdl.appendRow(QStandardItem("")) # Empty = No labels if "labels.txt" in files: - log("\nReading labels:") + LOGGER.debug("Reading labels:") for s in files["labels.txt"].split("\n"): if not s: continue @@ -706,7 +699,7 @@ def loadProject(project, zip=None): m = re.search(r"^(.*?):\s*(.*)$", s) txt = m.group(1) col = m.group(2) - log("* Add status: {} ({})".format(txt, col)) + LOGGER.debug("* Add status: {} ({})".format(txt, col)) icon = iconFromColorString(col) mdl.appendRow(QStandardItem(icon, txt)) @@ -719,11 +712,11 @@ def loadProject(project, zip=None): mdl = mw.mdlStatus mdl.appendRow(QStandardItem("")) # Empty = No status if "status.txt" in files: - log("\nReading Status:") + LOGGER.debug("Reading status:") for s in files["status.txt"].split("\n"): if not s: continue - log("* Add status:", s) + LOGGER.debug("* Add status: %s", s) mdl.appendRow(QStandardItem(s)) else: errors.append("status.txt") @@ -765,7 +758,7 @@ def loadProject(project, zip=None): mdl = mw.mdlPlots if "plots.xml" in files: - log("\nReading plots:") + LOGGER.debug("Reading plots:") # xml = bytearray(files["plots.xml"], "utf-8") root = ET.fromstring(files["plots.xml"]) @@ -774,7 +767,7 @@ def loadProject(project, zip=None): row = getStandardItemRowFromXMLEnum(plot, Plot) # Log - log("* Add plot: ", row[0].text()) + LOGGER.debug("* Add plot: %s", row[0].text()) # Characters if row[Plot.characters].text(): @@ -801,7 +794,7 @@ def loadProject(project, zip=None): mdl = mw.mdlWorld if "world.opml" in files: - log("\nReading World:") + LOGGER.debug("Reading World:") # xml = bytearray(files["plots.xml"], "utf-8") root = ET.fromstring(files["world.opml"]) body = root.find("body") @@ -817,7 +810,7 @@ def loadProject(project, zip=None): # Characters mdl = mw.mdlCharacter - log("\nReading Characters:") + LOGGER.debug("Reading Characters:") for f in [f for f in files if "characters" in f]: md, body = parseMMDFile(files[f]) c = mdl.addCharacter() @@ -843,7 +836,7 @@ def loadProject(project, zip=None): else: c.infos.append(CharacterInfo(c, desc, val)) - log("* Adds {} ({})".format(c.name(), c.ID())) + LOGGER.debug("* Adds {} ({})".format(c.name(), c.ID())) #################################################################################################################### # Texts @@ -851,14 +844,14 @@ def loadProject(project, zip=None): # everything, but the outline folder takes precedence (in cases it's been edited outside of manuskript. mdl = mw.mdlOutline - log("\nReading outline:") + LOGGER.debug("Reading outline:") paths = [f for f in files if "outline" in f] outline = OrderedDict() # We create a structure of imbricated OrderedDict to store the whole tree. for f in paths: split = f.split(os.path.sep)[1:] - # log("* ", split) + # LOGGER.debug("* %s", split) last = "" parent = outline @@ -913,7 +906,7 @@ def addTextItems(mdl, odict, parent=None): if type(odict[k]) == OrderedDict and "folder.txt" in odict[k]: # Adds folder - log("{}* Adds {} to {} (folder)".format(" " * parent.level(), k, parent.title())) + LOGGER.debug("{}* Adds {} to {} (folder)".format(" " * parent.level(), k, parent.title())) item = outlineFromMMD(odict[k]["folder.txt"], parent=parent) item._lastPath = odict[k + ":lastPath"] @@ -922,7 +915,7 @@ def addTextItems(mdl, odict, parent=None): # k is not a folder elif type(odict[k]) == str and k != "folder.txt" and not ":lastPath" in k: - log("{}* Adds {} to {} (file)".format(" " * parent.level(), k, parent.title())) + LOGGER.debug("{}* Adds {} to {} (file)".format(" " * parent.level(), k, parent.title())) item = outlineFromMMD(odict[k], parent=parent) item._lastPath = odict[k + ":lastPath"] @@ -977,17 +970,19 @@ def appendRevisions(mdl, root): # Get root's ID ID = root.attrib["ID"] if not ID: - log("* Serious problem: no ID!") + LOGGER.debug("* Serious problem: no ID!") + LOGGER.error("Revision has no ID associated!") continue # Find outline item in model item = mdl.getItemByID(ID) if not item: - log("* Error: no item whose ID is", ID) + LOGGER.debug("* Error: no item whose ID is %s", ID) + LOGGER.error("Could not identify the item matching the revision ID.") continue # Store revision - log("* Appends revision ({}) to {}".format(child.attrib["timestamp"], item.title())) + LOGGER.debug("* Appends revision ({}) to {}".format(child.attrib["timestamp"], item.title())) item.appendRevision(child.attrib["timestamp"], child.attrib["text"]) @@ -999,7 +994,7 @@ def getOutlineItem(item, enum): @return: [QStandardItem] """ row = getStandardItemRowFromXMLEnum(item, enum) - log("* Add worldItem:", row[0].text()) + LOGGER.debug("* Add worldItem: %s", row[0].text()) for child in item: sub = getOutlineItem(child, enum) row[0].appendRow(sub)