From 37a1d86659fab35ee0efd0948ebd481bc596c5bb Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 7 Apr 2014 08:07:52 +0200 Subject: [PATCH] Fixed an error in random_string_from_module that was introduced last update. --- src/utils/batchprocessors.py | 131 +++++++++++++++++++---------------- src/utils/utils.py | 4 +- 2 files changed, 76 insertions(+), 59 deletions(-) diff --git a/src/utils/batchprocessors.py b/src/utils/batchprocessors.py index 770bec0894..aec7033f71 100644 --- a/src/utils/batchprocessors.py +++ b/src/utils/batchprocessors.py @@ -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 + #------------------------------------------------------------ # diff --git a/src/utils/utils.py b/src/utils/utils.py index cf065c7925..97eaf3be37 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -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):