Refactoring step.

This commit is contained in:
Eric S. Raymond 2007-05-03 07:21:46 +00:00
parent 7e194d593b
commit f410430429

View file

@ -26,6 +26,10 @@
import sys, os, re, getopt, curses.ascii
def vcdir(filename):
"Predicate that tells us to ignore a file if it's part of the VCS state."
return ".svn" in filename # Change if we move away from Subversion
filemoves = {
# Older includes all previous to 1.3.1.
"older" : (
@ -288,32 +292,30 @@ conversion1 = {
"~" : "_f",
}
max_len = max(*map(len, conversion1.values()))
def get_adjacent(x, y, map):
"Returns string of original location+adjacent locations on hex 1-char map"
odd = (x) % 2
adj = map[y][x];
if x > 0:
adj += map[y][x-1]
if x < len(map[y])-1:
adj += map[y][x+1]
if y > 0:
adj += map[y-1][x]
if y < len(map)-1:
adj += map[y+1][x]
if x > 0 and y > 0 and not odd:
adj += map[y-1][x-1]
if x < len(map[y])-1 and y > 0 and not odd:
adj += map[y-1][x+1];
if x > 0 and y < len(map)-1 and odd:
adj += map[y+1][x-1]
if x < len(map[y])-1 and y < len(map)-1 and odd:
adj += map[y+1][x+1]
adj = adj.replace("\n", "").replace("\r", "")
return adj
width = max_len+2
def neighborhood(x, y, map):
"Returns list of original location+adjacent locations from a hex map"
odd = (x) % 2
adj = [map[y][x]];
if x > 0:
adj.append(map[y][x-1])
if x < len(map[y])-1:
adj.append(map[y][x+1])
if y > 0:
adj.append(map[y-1][x])
if y < len(map)-1:
adj.append(map[y+1][x])
if x > 0 and y > 0 and not odd:
adj.append(map[y-1][x-1])
if x < len(map[y])-1 and y > 0 and not odd:
adj.append(map[y-1][x+1])
if x > 0 and y < len(map)-1 and odd:
adj.append(map[y+1][x-1])
if x < len(map[y])-1 and y < len(map)-1 and odd:
adj.append(map[y+1][x+1])
return adj
def maptransform1(input, baseline, inmap, y):
"Transform a map line from 1.2.x to 1.3.x format."
format = "%%%d.%ds" % (width, max_len)
@ -335,7 +337,9 @@ def maptransform1(input, baseline, inmap, y):
sys.exit(1)
if "_K" in ohex:
# Convert keeps according to adjacent hexes
adj = get_adjacent(x, y, inmap)
adj = neighborhood(x, y, inmap)
adj = "".join(adj).replace("\n", "").replace("\r", "")
# print "adjacent: %s" % adj
hexcount = {}
for i in range(1, len(adj)):
@ -509,18 +513,28 @@ def translator(filename, mapxform, textxform):
else:
return None
ignore = (".tgz", ".png", ".jpg")
ignore = (".tgz", ".png", ".jpg", "-bak")
def interesting(fn):
"Is a file interesting for conversion purposes?"
return fn.endswith(".cfg") or fn.endswith(".map") \
or ("maps" in fn and fn[-4:] not in ignore)
def allcfgfiles(dir):
"Get the names of all .cfg and map files under dir, ignoring .svn directories."
"Get the names of all interesting files under dir."
datafiles = []
os.path.walk(dir,
lambda arg, dir, names: datafiles.extend(map(lambda x: os.path.normpath(os.path.join(dir, x)), names)),
None)
datafiles = filter(lambda x: ".svn" not in x, datafiles)
datafiles = filter(lambda x: x.endswith(".cfg") or ('maps' in x and os.path.isfile(x) and x[-4:] not in ignore), datafiles)
datafiles = filter(lambda x: not x.endswith("-bak"), datafiles)
datafiles.sort() # So changes and diffs for the same campaigns will cluster in the report
if not os.path.isdir(dir):
if interesting(dir):
datafiles.append(dir)
else:
for root, dirs, files in os.walk(dir):
if vcdir(dirs):
dirs.remove(dirs)
for name in files:
if interesting(os.path.join(root, name)):
datafiles.append(os.path.join(root, name))
print datafiles
datafiles.sort() # So diffs for same campaigns will cluster in reports
return datafiles
def help():