Source

AdaCode / descdb.xml

Full commit
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Monday, December 03, 2012, 12:00 PM -->
<!-- MuClient version 4.77 -->

<!-- Plugin "DescDB" generated by Plugin Wizard -->

<muclient>
<plugin
   name="DescDB"
   author="Ada"
   id="0f3133a287ed8aedfbb82718"
   language="Python"
   purpose="Log Database"
   date_written="2013-01-05 12:00:00"
   requires="4.62"
   version="1.0"
   >
<description trim="y">
<![CDATA[
A database of descriptions! ... I don't know what I'd do with it yet.
]]>
</description>

</plugin>

<!--  Get our standard constants -->

<include name="constants.pys"/>

<!--  Triggers  -->

<triggers>
  <trigger
   group="DescDB"
   enabled="y"
   keep_evaluating="y"
   match="^(He|She) is a (graceful tsol\'aa|stout dwarf|human|powerful troll|mhun|winged atavian|tiger\-like rajamala|insectoid horkval|frog\-like grook|lizard\-like xoran|beautiful siren|horned satyr)(.+)$"
   name="DescriptionStart"
   omit_from_output="n"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
desc_text = []
desc_text.append("%0")
world.EnableTrigger("CaptureAll", True)
world.EnableTrigger("DescriptionEnd", True)
name = world.GetCommandList(1)[0].split(" ")[1].lower()
  </send>
  </trigger>
  <trigger
   group="DescDB"
   enabled="n"
   keep_evaluating="y"
   match="^(.+)$"
   name="CaptureAll"
   omit_from_output="n"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
desc_text.append("%0")
  </send>
  </trigger>
  <trigger
   group="DescDB"
   enabled="n"
   keep_evaluating="y"
   match="^(He|She) is wearing.+$"
   name="DescriptionEnd"
   omit_from_output="n"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
desc_text = " ".join(desc_text[:-1])
world.EnableTrigger("CaptureAll", False)
world.EnableTrigger("DescriptionEnd", False)
ProcessDescription(desc_text)
  </send>
  </trigger>
  <trigger
   group="DescDB"
   enabled="n"
   keep_evaluating="y"
   match="^(He|She) is completely naked\.$"
   name="DescriptionEndNaked"
   omit_from_output="n"
   regexp="y"
   send_to="12"
   sequence="900"
  >
  <send>
desc_text = " ".join(desc_text[:-1])
desc_text = re.split("(?:He|She) is completely naked\.")
desc_text = " ".join(desc_text)
world.EnableTrigger("CaptureAll", False)
world.EnableTrigger("DescriptionEnd", False)
ProcessDescription(desc_text)
  </send>
  </trigger>
</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   match="^DescDB:test (.*?)$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>exec("%1")</send>
  </alias>
</aliases>

<!--  Script  -->

<script>
<![CDATA[
import re
import sqlite3

import Tkinter
root = Tkinter.Tk()
root.withdraw() # to hide the Tk GUI window as per http://stackoverflow.com/a/1796726

from tkFileDialog import asksaveasfilename, askopenfilename

def Note(message): # not writing world.Note everywhere ^_^
    world.Note(message)

DBFile = world.GetVariable("dbpath")
if not DBFile:
    while not (DBFile and len(DBFile) > 0):
        DBFile = asksaveasfilename(defaultextension=".db", filetypes=[("SQLite3 database file", "*.db")], title="Specify the location of your new database.")
    world.SetVariable("dbpath", DBFile)
    world.SaveState

db = sqlite3.connect(DBFile)
cursor = db.cursor()

def createdb():
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.execute("""
    CREATE TABLE IF NOT EXISTS descriptions (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        description TEXT,
        name TEXT
    )""")
    cursor.execute("""
    CREATE VIRTUAL TABLE IF NOT EXISTS desc_fts USING fts4(content="descriptions", f_content)
    """)
    cursor.execute("""
    CREATE TRIGGER IF NOT EXISTS descriptions_ai AFTER INSERT ON descriptions BEGIN
        INSERT INTO desc_fts(docid, f_content) VALUES(new.rowid, new.description);
    END;
    """)
    cursor.execute("""
    CREATE TRIGGER IF NOT EXISTS descriptions_bd BEFORE DELETE ON descriptions BEGIN
        DELETE FROM desc_fts WHERE docid=old.rowid;
    END;
    """)
    db.commit()

createdb()
desc_text = []
name = ""

def ProcessDescription(description):
    split_desc = re.split("A rune.+?\.", description)
    description = "".join(split_desc)
    split_desc = re.split("(?:He|She) walks with the .+?favour.+?\.", description)
    description = "".join(split_desc)

    description = description.strip()

    cursor.execute("SELECT name, description FROM descriptions WHERE name=? AND description=?", (name, description))
    results = cursor.fetchall()
    if not len(results) > 0:
        cursor.execute("INSERT INTO descriptions (name, description) VALUES (?, ?)", (name, description))
        db.commit()
]]>
</script>

<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="DescDB:help"
   ignore_case="y"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
def OnHelp (a, b, c):
    world.note (world.GetPluginInfo (world.GetPluginID, 3))
]]>
</script>

</muclient>