def __init__(self, args): Menu.__init__(self) self.database = os.path.expanduser(args.database) self.spells = Spells(self.database) self.spellbook = self.spells.getSpellbook(args.spellbook) self.spells.session.add(self.spellbook) self.template = __path__[0] + "templates/plain/"
class MkSpellbook(Menu): def __init__(self, args): Menu.__init__(self) self.database = os.path.expanduser(args.database) self.spells = Spells(self.database) self.spellbook = self.spells.getSpellbook(args.spellbook) self.spells.session.add(self.spellbook) self.template = __path__[0] + "templates/plain/" def start(self): menuselection = [ ("New Spellbook", self.newMenu), ("Select Spellbook", self.loadMenu), ("Delete a Spellbook", self.deleteMenu), ("Select Spells", self.spellMenu), ("Clear Spells", self.clearSpells), ("Rename Spellbook", self.renameMenu), ("Set an author", self.setAuthor), ("Set a logo", self.setLogo), ("Select Template", self.templateMenu), ("Export to pdf", self.pdflatex), ("Add Spells", self.addMenu)] mrc, mch = self.d.menu("Main Menu", self.mkSelection([c[0] for c in menuselection])) if mch and not mrc: return menuselection[int(mch)-1][1] return None def newMenu(self): src, spellbookname = self.d.inputbox("Name your spellbook", init="My Spellbook") if not src: spellbook = self.spells.session.query(Spellbook).filter(Spellbook.name == spellbookname).first() if spellbook: yrc, ych = self.d.yesno("A Spellbook called "+ spellbookname + " already exists. Overwrite it?") if not yrc: self.spells.session.delete(spellbook) self.spells.session.commit() self.spellbook = Spellbook(name=spellbookname) self.spells.session.add(self.spellbook) self.spells.session.commit() else: return self.newMenu else: self.spellbook = Spellbook(name=spellbookname) self.spells.session.add(self.spellbook) self.spells.session.commit() return self.start def loadMenu(self): availspellbooks = self.spells.listSpellbooks() if availspellbooks == []: self.d.msgbox("No spellbooks saved") return self.start lrc, lch = self.d.menu("Select Spellbook", self.mkSelection([sb.name for sb in availspellbooks])) if lch: self.spellbook = availspellbooks[int(lch)-1] return self.start def deleteMenu(self): availspellbooks = self.spells.listSpellbooks() if availspellbooks: lrc, lch = self.d.menu("Deleta a Spellbook", self.mkSelection([sb.name for sb in availspellbooks])) if lch: if not availspellbooks[int(lch)-1] == self.spellbook: return lambda: self.askYesNoDelete(availspellbooks[int(lch)-1]) else: mrc, mch = self.d.msgbox("You cannot delete your active spellbook") return self.start def askYesNoDelete(self, spellbook): yrc, ych = self.d.yesno("Do you really want to delete " + spellbook.name + "? It cannot be recovered.") if not yrc: self.spells.session.delete(spellbook) return self.start def spellMenu(self): menu = Spellselection(self.spellbook, self.spells) menu.caller(menu.askruleset) return self.start def clearSpells(self): yrc, ych = self.d.yesno("Are you sure to clear your spellbook? All unsaved data will be lost") if not yrc: self.spellbook.spells = [] self.spells.session.commit() return self.start def renameMenu(self): src, spellbookname = self.d.inputbox("Rename Spellbook", init=self.spellbook.name) if not src: spellbook = self.spells.session.query(Spellbook).filter(Spellbook.name == spellbookname).first() if spellbook and spellbook != self.spellbook: yrc, ych = self.d.yesno("A Spellbook called "+ spellbookname + " already exists. Overwrite it?") if not yrc: self.spells.session.delete(spellbook) self.spells.session.commit() self.spellbook.name = spellbookname self.spells.session.add(self.spellbook) self.spells.session.commit() else: return self.saveMenu else: self.spellbook.name = spellbookname self.spells.session.commit() return self.start def setAuthor(self): arc, author = self.d.inputbox("Set an author:", init=self.spellbook.author) if author: self.spellbook.author = author self.spells.session.commit() return self.start return self.start def setLogo(self): lrc, logo = self.d.fselect("Select a logo for the Spellbook", os.path.expanduser("~")) if not os.path.isfile(logo): erc, rch = self.d.msgbox("No such file: " + logo) return self.start if logo: with open(logo, "rb") as logofile: self.spellbook.logo = logofile.read() self.spellbook.logoext = os.path.splitext(logo)[1] self.spells.session.commit() return self.start def templateMenu(self): templates = [] for template in os.listdir(__path__[0] + "/templates"): if os.path.isdir(__path__[0] + "/templates/" + template): templates.append((template, __path__[0] + "/templates/" + template + "/")) if os.path.isdir(os.path.expanduser("~/.mkspellbook/templates/")): for template in os.listdir(os.path.expanduser("~/.mkspellbook/templates/")): templates.append((template + " (User)", os.path.expanduser("~/.mkspellbook/templates/" + template + "/"))) if templates: trc, tch = self.d.menu("Templates", self.mkSelection([template[0] for template in templates])) if tch: self.template = templates[int(tch)-1] return self.start def pdflatex(self): orc, output = self.d.inputbox("Export pdf to", init=os.path.expanduser("~/" + self.spellbook.name + ".pdf")) if output and not orc: g = Genlatex() self.spellbook.spells = sorted(self.spellbook.spells, key=lambda spell: (spell.classlevel.level, spell.spell.name)) g.genlatex(self.spellbook, self.template[1], output) yrc, ych = self.d.yesno("Do you want to open your Spellbook?") if not yrc: vrc, viewer = self.d.inputbox("Which viewer do you want to use?", init="evince") if viewer and not vrc: subprocess.Popen([viewer, output]) return self.start def addMenu(self): importscripts = [] for importscript in os.listdir(__path__[0] + "/importscripts"): if importscript.startswith("script_") and importscript.endswith(".py"): importscripts.append((importscript,"System")) if os.path.isdir(os.path.expanduser("~/.mkspellbook/importscripts/")): for importscript in os.listdir(os.path.expanduser("~/.mkspellbook/importscripts/")): if importscript.startswith("script_") and importscript.endswith(".py"): importscripts.append((importscript, "User")) irc, ich = self.d.menu("Select import script", self.mkSelection([script[0] + " (" + script[1] + ")" for script in importscripts])) if ich and not irc: script = importscripts[int(ich)-1] if script[1] == "System": mscript = importlib.import_module(__package__ + ".importscripts." + os.path.splitext(script[0])[0]) mscript.runimport(self.database) else: sys.path.insert(0,os.path.expanduser("~/.mkspellbook/importscripts")) mscript = __import__(os.path.splitext(script[0])[0]) sys.path.remove(os.path.expanduser("~/.mkspellbook/importscripts")) mscript.runimport(self.database) return self.start