Ada Young avatar Ada Young committed 076f823

Implemented warpdb:export and warpdb:import! Now we can share our warps.

Comments (0)

Files changed (1)

    >
 <description trim="y">
 <![CDATA[
-I'm a serpent and I often wonder which warp to take to reach a given area from a given area. This plugin addresses the problem with a warp database which may be looked up for such assistance. Have a look at the available aliases to interact with it. I'll add the ability to work with ATCP/GMCP information and open some possibility to work with a mapper in later versions.
+I'm a serpent and I often wonder which warp to take to reach a given area from a given area. This plugin addresses the problem with a warp database which may be looked up for such assistance. Have a look at the available aliases to interact with it. I'll add the ability to work with ATCP/GMCP information, warps in the wilderness and open some possibility to work with a mapper in later versions.
 
 This plugin is written in Python, please see this page to setup Python on your computer: http://adayoung.bitbucket.org/pythonsqlite.html
 
     WarpDB:sources - Check for warps in your area and add them to database
     WarpDB:endpoints - Display a list of all areas you can reach from your location
     WarpDB:toarea <area name> - Check for warps leading to the specified area from your location
-    WarpDB:search from:<area name> to:<area name> - Display a list of warps connecting the specified areas
 
-    WarpDB:export <area name> - Export all outgoing warps from a given area
+    WarpDB:export [<area name>] - Export all outgoing warps from a given area
     WarpDB:import - Import a list of warps exported by the above alias
 
     WarpDB:orphans - Display a list of warps who's destination area is not in the database
 if len(areaname) > 0:
     ProcessWarps(areaname)
 else:
-    Note("WarpDB: Will not manage warps in the wilderness. Sorry!")
+    Note("WarpDB: Will not manage warps in the wilderness!")
   </send>
   </trigger>
   <trigger
   </send>
   </alias>
   <alias
+   match="^WarpDB:export (.+)$"
+   enabled="y"
+   group="WarpDB"
+   regexp="y"
+   send_to="12"
+   ignore_case="y"
+   sequence="100"
+  >
+  <send>
+DumpData("%1")
+  </send>
+  </alias>
+  <alias
+   match="^WarpDB:export$"
+   enabled="y"
+   group="WarpDB"
+   regexp="y"
+   send_to="12"
+   ignore_case="y"
+   sequence="100"
+  >
+  <send>
+DumpDataFull()
+  </send>
+  </alias>
+  <alias
+   match="^WarpDB:import$"
+   enabled="y"
+   group="WarpDB"
+   regexp="y"
+   send_to="12"
+   ignore_case="y"
+   sequence="100"
+  >
+  <send>
+LoadData()
+  </send>
+  </alias>
+  <alias
    match="^WarpDB:test (.*?)$"
    enabled="y"
    group="WarpDB"
     if len(warp_list) > 0:
         Note("The following warps lead from here to [%s]:" % areaname)
         line_len = len("The following warps lead to [%s]:" % areaname)
-        Note("-" * line_len)
+        Note("-" * (line_len + 10)) # O_o
         for source, destination in warp_list:
             Note("%s -> %s" % (source, destination))
-        Note("-" * line_len)
+        Note("-" * (line_len + 10)) # O_o
     else:
         Note("WarpDB: No recorded warps lead from here to [%s], try WarpDB:sources here and in the destination area to link the two." % areaname)
+
+def DumpData(areaname):
+    areaname = ValidateArea(areaname)
+    if areaname is None: # bail out
+        return
+
+    filename = asksaveasfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
+    if filename is '': # bail out
+        Note("WarpDB: Export cancelled by user.")
+        return
+
+    dataset = {areaname: []}
+    cursor.execute("SELECT source, destination FROM warplist w JOIN arealist a ON w.source_areaid=a.id WHERE areaname=?", (areaname, ))
+    results = cursor.fetchall()
+    for source, destination in results:
+        dataset[areaname].append((source, destination))
+
+    import cPickle
+    with open(filename, 'w') as dump_file:
+        cPickle.dump(dataset, dump_file)
+    Note("Exported a total of %d objects." % len(dataset[areaname]))
+
+def DumpDataFull():
+    filename = asksaveasfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
+    if filename is '': # bail out
+        Note("WarpDB: Export cancelled by user.")
+        return
+
+    dataset = {}
+    cursor.execute("SELECT areaname FROM arealist")
+    results = cursor.fetchall()
+    for areaname in results:
+        dataset[areaname[0]] = []
+    cursor.execute("SELECT source, destination, areaname FROM warplist w JOIN arealist a ON w.source_areaid=a.id")
+    results = cursor.fetchall()
+    for source, destination, areaname in results:
+        dataset[areaname].append((source, destination))
+
+    import cPickle
+    with open(filename, 'w') as dump_file:
+        cPickle.dump(dataset, dump_file)
+    Note("Exported a total of %d compound objects." % len(dataset))
+
+def LoadData():
+    filename = askopenfilename(defaultextension=".pickle", filetypes=[("WarpDB Pickle", "*.pickle")])
+    if filename is '': # bail out
+        Note("WarpDB: Import cancelled by user.")
+        return
+
+    import cPickle
+    with open(filename, 'r') as load_file:
+        try:
+            dataset = cPickle.load(load_file)
+        except:
+            import sys
+            Note(str(sys.exc_info()))
+            Note("WarpDB: Invalid file or the file does not contain a WarpDB pickle object.")
+            return
+
+    try:
+        for areaname in dataset.keys():
+            cursor.execute("SELECT id FROM arealist WHERE areaname=?", (areaname, ))
+            results = cursor.fetchall()
+            if len(results) > 0:
+                areaid = results[0][0]
+            else:
+                cursor.execute("INSERT INTO arealist (areaname) VALUES (?)", (areaname, ))
+                cursor.execute("SELECT last_insert_rowid()")
+                areaid = cursor.fetchone()[0]
+            import_set = []
+            for source, destination in dataset[areaname]:
+                import_set.append((source, destination, areaid))
+            cursor.executemany("""
+            INSERT INTO warplist (source, destination, source_areaid) VALUES (?, ?, ?)
+            """, import_set)
+        db.commit()
+        Note("WarpDB: Imported a total of %d compound objects." % len(dataset))
+    except sqlite3.Error as e:
+        Note("SQLite error: {0}".format(e.message))
+    except:
+        import sys
+        Note(str(sys.exc_info()))
+        Note("WarpDB: Invalid file or the file does not contain a WarpDB pickle object.")
 ]]>
 </script>
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.