Author: esr
Date: Tue Apr 24 17:58:27 2007
New Revision: 17036
URL: http://svn.gna.org/viewcvs/wesnoth?rev=17036&view=rev
Log:
More refactoring. This version of map_convert.py continues to pass all its
regression tests with the Perl version.
Modified:
trunk/data/tools/map_convert.py
Modified: trunk/data/tools/map_convert.py
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/data/tools/map_convert.py?rev=17036&r1=17035&r2=17036&view=diff
==============================================================================
--- trunk/data/tools/map_convert.py (original)
+++ trunk/data/tools/map_convert.py Tue Apr 24 17:58:27 2007
@@ -102,14 +102,25 @@
width = max_len+2
+class maptransform_error:
+ "Error object to be thrown by maptransform."
+ def __init__(self, infile, inline, imap, x, y, type):
+ self.infile = infile
+ self.inline = inline
+ self.x = x
+ self.y = y
+ self.type = type
+ def __str__(self):
+ return '"%s", line %d: %s at (%d, %d)\n' % \
+ (self.input, self.inline, self.type, self.x, self.y)
+
def maptransform(input, baseline, inmap, y):
"Transform a map line from 1.2.x to 1.3.x format."
format = "%%%d.%ds" % (width, max_len)
x = 0
if "," in inmap[y]:
- sys.stderr.write("mapconvert map file %s appears "
- "to be converted already\n" % input)
- sys.exit(1)
+ raise maptransform_error(input, baseline, x, y,
+ "map file appears to be converted already")
line = ''
for char in inmap[y]:
ohex = ''
@@ -118,8 +129,8 @@
elif char in conversion:
ohex = format % conversion[char] + ','
else:
- sys.stderr.write('map_convert: "%s", line %d: unrecognized
character %s (%d) at (%d, %d)\n' % \
- (input, baseline+y+1, `char`, ord(char), x, y))
+ raise maptransform_error(input, baseline+y+1, x, y,
+ "unrecognized character %s (%d)" %
(`char`, ord(char)))
# ohex = format % char
sys.exit(1)
if "_K" in ohex:
@@ -131,8 +142,8 @@
# Intentionally skipping 0 as it is original hex
a = adj[i];
if not a in conversion:
- sys.stderr.write('map_convert: "%s", line %d: error in
adjacent hexes at (%d, %d).\n' % \
- (input, baseline+y+1, x, y))
+ raise maptransform_error(input, baseline, x, y,
+ "error in adjacent hexes")
sys.exit(1)
ca = conversion[a]
if ca.startswith("C"): #this is a castle hex
@@ -256,13 +267,17 @@
sys.stderr.write("can not read map file: %s\n" % map_file)
sys.exit(1)
- changed = translator(map_file, new_map_file, maptransform, texttransform)
- if changed == None:
- shutil.copy(map_file, new_map_file)
- else:
- ofp = open(new_map_file, "w");
- ofp.write(changed)
- ofp.close()
+ try:
+ changed=translator(map_file, new_map_file, maptransform, texttransform)
+ if changed == None:
+ shutil.copy(map_file, new_map_file)
+ else:
+ ofp = open(new_map_file, "w");
+ ofp.write(changed)
+ ofp.close()
+ except maptransform_error, e:
+ sys.stderr.write("map_convert.py: " + `e` + "\n")
+ sys.exit(1)
# map_convert ends here.
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits