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
+
2
8
3
9
class NamedLocation (object ):
4
10
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):
10
16
self .z = z
11
17
self .pitch = pitch
12
18
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
+
34
80
importers = {
35
- "myhomes" : importer_myhomes
81
+ "myhomes" : MyHomesLocationsDatabase ,
82
+ "warpz0r" : Warpz0rLocationsDatabase ,
83
+ "cmdbook" : CommandBookLocationsDatabase
36
84
}
37
85
38
86
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__
41
89
exit (1 )
42
90
if not sys .argv [1 ] in importers :
43
91
print "Unknown converter '%s' specified! Available converters: %s" % (sys .argv [1 ], importers .keys ().__str__ ()[1 :- 1 ])
44
92
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' )
45
98
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!" )
47
103
if not os .path .isdir (os .path .dirname (destpath )):
48
104
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 ]]()
51
107
warps = None
52
108
53
109
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 )
57
115
exit (1 )
58
-
59
- dump_commandbook_csv (warps , destpath )
60
- print "%d homes successfully converted!" % len (warps )
0 commit comments