Fixed an error in random_string_from_module that was introduced last update.

This commit is contained in:
Griatch 2014-04-07 08:07:52 +02:00
parent 803af4307b
commit 37a1d86659
2 changed files with 76 additions and 59 deletions

View file

@ -280,70 +280,85 @@ class BatchCommandProcessor(object):
"""
#helper function
def identify_line(line):
"""
Identifies the line type (comment, commanddef or empty)
"""
try:
if line.strip().startswith("#INSERT"):
return "insert"
elif line.strip()[0] == '#':
return "comment"
else:
return "commanddef"
except IndexError:
return "empty"
text = "".join(read_batchfile(pythonpath, file_ending='.ev'))
#read the indata, if possible.
lines = read_batchfile(pythonpath, file_ending='.ev')
#line = utils.to_unicode(line)
if not lines:
return None
commands = []
curr_cmd = ""
#purge all superfluous whitespace and newlines from lines
reg1 = re.compile(r"\s+")
lines = [reg1.sub(" ", l) for l in lines]
#parse all command definitions into a list.
for line in lines:
typ = identify_line(line)
if typ == "commanddef":
curr_cmd += line
elif typ == "empty" and curr_cmd:
curr_cmd += "\r\n"
elif typ == "insert":
# note that we are not safeguarding for
# cyclic imports here!
if curr_cmd:
commands.append(curr_cmd.strip())
curr_cmd = ""
filename = line.lstrip("#INSERT").strip()
insert_commands = self.parse_file(filename)
if insert_commands is None:
insert_commands = ["{rINSERT ERROR: %s{n" % filename]
commands.extend(insert_commands)
else: #comment
if curr_cmd:
commands.append(curr_cmd.strip())
curr_cmd = ""
if curr_cmd:
commands.append(curr_cmd.strip())
#second round to clean up now merged line edges etc.
reg2 = re.compile(r"[ \t\f\v]+")
commands = [reg2.sub(" ", c) for c in commands]
def replace_insert(match):
"Map replace entries"
return "\#\n".join(self.parse_file(match.group()))
# insert commands from inserted files
text = re.sub(r"^\#INSERT (.*?)", replace_insert, text, flags=re.MULTILINE)
# get all commands
commands = re.split(r"^\#.*?$", text, flags=re.MULTILINE)
#remove eventual newline at the end of commands
commands = [c.strip('\r\n') for c in commands]
commands = [c for c in commands if c]
return commands
# #helper function
# def identify_line(line):
# """
# Identifies the line type (comment, commanddef or empty)
# """
# try:
# if line.strip().startswith("#INSERT"):
# return "insert"
# elif line.strip()[0] == '#':
# return "comment"
# else:
# return "commanddef"
# except IndexError:
# return "empty"
#
# #read the indata, if possible.
# lines = read_batchfile(pythonpath, file_ending='.ev')
#
# #line = utils.to_unicode(line)
# if not lines:
# return None
#
# commands = []
# curr_cmd = ""
#
# #purge all superfluous whitespace and newlines from lines
# reg1 = re.compile(r"\s+")
# lines = [reg1.sub(" ", l) for l in lines]
#
# #parse all command definitions into a list.
# for line in lines:
#
# typ = identify_line(line)
#
# if typ == "commanddef":
# curr_cmd += line
# elif typ == "empty" and curr_cmd:
# curr_cmd += "\r\n"
# elif typ == "insert":
# # note that we are not safeguarding for
# # cyclic imports here!
# if curr_cmd:
# commands.append(curr_cmd.strip())
# curr_cmd = ""
# filename = line.lstrip("#INSERT").strip()
# insert_commands = self.parse_file(filename)
# if insert_commands is None:
# insert_commands = ["{rINSERT ERROR: %s{n" % filename]
# commands.extend(insert_commands)
# else: #comment
# if curr_cmd:
# commands.append(curr_cmd.strip())
# curr_cmd = ""
# if curr_cmd:
# commands.append(curr_cmd.strip())
#
# #second round to clean up now merged line edges etc.
# reg2 = re.compile(r"[ \t\f\v]+")
# commands = [reg2.sub(" ", c) for c in commands]
#
# #remove eventual newline at the end of commands
# commands = [c.strip('\r\n') for c in commands]
# return commands
#------------------------------------------------------------
#

View file

@ -843,7 +843,9 @@ def random_string_from_module(module):
"""
Returns a random global string from a module
"""
string = random.choice(string_from_module(module))
string = string_from_module(module)
if is_iter(string):
string = random.choice(string)
return string
def init_new_player(player):