Skip to content

Commit 5daaff5

Browse files
author
zml2008
committed
Made the homes converter have read-write support for all formats
(except MyHomes since it doesn't seem to be maintained anymore) This allows conversion to and from CommandBook's format
1 parent a03552e commit 5daaff5

File tree

1 file changed

+89
-34
lines changed

1 file changed

+89
-34
lines changed

homeconvert.py

100644100755
Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import sqlite3, csv, sys, os, time
1+
#!/usr/bin/env python
2+
import sqlite3
3+
import csv
4+
import sys
5+
import os
6+
import logging
7+
28

39
class NamedLocation(object):
410
def __init__(self, name, owner, world, x, y, z, pitch, yaw):
@@ -10,51 +16,100 @@ def __init__(self, name, owner, world, x, y, z, pitch, yaw):
1016
self.z = z
1117
self.pitch = pitch
1218
self.yaw = yaw
13-
14-
def dump_commandbook_csv(warps, dest):
15-
with open(dest, 'a') as raw:
16-
locwriter = csv.writer(raw, quoting=csv.QUOTE_ALL)
17-
for warp in warps:
18-
locwriter.writerow([warp.name, warp.world, warp.owner, warp.x, warp.y, warp.z, warp.pitch, warp.yaw])
19-
20-
def importer_myhomes(args):
21-
if (len(args) < 1):
22-
raise Exception("No MyHomes path specified!")
23-
dbpath = os.path.abspath(os.path.expanduser(args[0]))
24-
conn = sqlite3.connect(dbpath)
25-
26-
c = conn.cursor();
27-
c.execute("SELECT `name`, `world`, `x`, `y`,`z`,`pitch`,`yaw` FROM `homeTable`")
28-
warps = []
29-
for res in c:
30-
warps.append(NamedLocation(res[0], res[0], res[1], res[2], res[3], res[4], res[5], res[6]))
31-
c.close()
32-
return warps
33-
19+
20+
21+
class CommandBookLocationsDatabase(object):
22+
def read(self, src):
23+
with open(src, 'r') as raw:
24+
locreader = csv.reader(raw)
25+
rownum = 0
26+
warps = []
27+
for row in locreader:
28+
rownum += 1
29+
if len(row) < 8:
30+
logging.warning("Line %d has < 8 rows: %s" % (rownum, row))
31+
continue
32+
warps.append(NamedLocation(row[0], row[2], row[1], row[3],
33+
row[4], row[5], row[6], row[7]))
34+
return warps
35+
36+
def write(self, warps, dest):
37+
with open(dest, 'a') as raw:
38+
locwriter = csv.writer(raw, quoting=csv.QUOTE_ALL)
39+
for warp in warps:
40+
locwriter.writerow([warp.name, warp.world, warp.owner,
41+
warp.x, warp.y, warp.z, warp.pitch,
42+
warp.yaw])
43+
44+
45+
class MyHomesLocationsDatabase(object):
46+
def read(self, src):
47+
conn = sqlite3.connect(src)
48+
c = conn.cursor()
49+
c.execute("SELECT `name`, `world`, `x`, `y`,`z`,`pitch`,`yaw` FROM `homeTable`")
50+
51+
warps = []
52+
for res in c:
53+
warps.append(NamedLocation(res[0], res[0], res[1], res[2], res[3], res[4], res[5], res[6]))
54+
c.close()
55+
return warps
56+
57+
def write(self, warps, dest):
58+
raise Exception("MyHomes does not support exporting!")
59+
60+
61+
class Warpz0rLocationsDatabase(object):
62+
def read(self, src):
63+
with open(src) as db:
64+
warps = []
65+
linenum = 0
66+
for line in db:
67+
linenum += 1
68+
split = line.split(":")
69+
if len(split) < 6:
70+
logging.warning("Less than required 6 entries on line %d: %s" % (linenum, line))
71+
continue
72+
warps.append(NamedLocation(split[0], split[0], split[5], split[1], split[2], split[3], 0, split[4]))
73+
return warps
74+
75+
def write(self, warps, dest):
76+
with open(dest, 'a') as db:
77+
for warp in warps:
78+
db.write(":".join([warp.name, warp.x, warp.y, warp.z, warp.yaw, warp.world, '-1']) + '\n')
79+
3480
importers = {
35-
"myhomes": importer_myhomes
81+
"myhomes": MyHomesLocationsDatabase,
82+
"warpz0r": Warpz0rLocationsDatabase,
83+
"cmdbook": CommandBookLocationsDatabase
3684
}
3785

3886
if __name__ == "__main__":
39-
if len(sys.argv) < 3:
40-
print "Not enough arguments. Usage: %s <converter> <destination file> [converter args]" % __file__
87+
if len(sys.argv) < 5:
88+
print "Not enough arguments. Usage: %s <source format> <source file> <destination format> <destination file>" % __file__
4189
exit(1)
4290
if not sys.argv[1] in importers:
4391
print "Unknown converter '%s' specified! Available converters: %s" % (sys.argv[1], importers.keys().__str__()[1:-1])
4492
exit(1)
93+
if not sys.argv[3] in importers:
94+
print "Unknown converter '%s' specified! Available converters: %s" % (sys.argv[3], importers.keys().__str__()[1:-1])
95+
exit(1)
96+
97+
logging.basicConfig(format='[%(asctime)s] [HomeConverter] %(message)s', datefmt='%H:%M:%S')
4598

46-
destpath = os.path.abspath(os.path.expanduser(sys.argv[2]))
99+
destpath = os.path.abspath(os.path.expanduser(sys.argv[4]))
100+
srcpath = os.path.abspath(os.path.expanduser(sys.argv[2]))
101+
if not os.path.isdir(os.path.dirname(srcpath)):
102+
logging.error("Source file (%s) does not exist!")
47103
if not os.path.isdir(os.path.dirname(destpath)):
48104
os.makedirs(os.path.dirname(destpath))
49-
50-
importer = importers[sys.argv[1]]
105+
importer = importers[sys.argv[1]]()
106+
output = importers[sys.argv[3]]()
51107
warps = None
52108

53109
try:
54-
warps = importer(sys.argv[3:])
55-
except Exception as e:
56-
print e
110+
warps = importer.read(srcpath)
111+
output.write(warps, destpath)
112+
print "%d homes successfully converted!" % len(warps)
113+
except Exception, e:
114+
logging.error(e)
57115
exit(1)
58-
59-
dump_commandbook_csv(warps, destpath)
60-
print "%d homes successfully converted!" % len(warps)

0 commit comments

Comments
 (0)