Index: coalesce.py =================================================================== --- coalesce.py (revisión: 351) +++ coalesce.py (copia de trabajo) @@ -22,20 +22,20 @@ # Alex 20090309 # Coalesces (unifies) all into one file to generate an executable -import sys +import sys, re from io.fileline import * from util.trace import Trace files = list() -def getreader(filename): +def getreader(filename, dir = ''): "Get a reader for lines" if filename in files: # already parsed; skip return None files.append(filename) - return LineReader(filename) + return LineReader(filename, dir) def readargs(args): "Read arguments from the command line" @@ -72,9 +72,20 @@ line = reader.currentline() if line.startswith('from'): self.comments = False - filename = line.split()[1].replace('.', '/') + '.py' - newreader = getreader(filename) - self.coalesce(newreader, writer) + # Add directory so relative imports work + current_dir = reader.dir + orig_filename = line.split()[1].replace('.', '/') + '.py' + filename = current_dir + orig_filename + # Append to dir if we're going deeper + new_dir = current_dir + get_dir(filename) + # First load relative import. If that fails, + # then load the absolute import + try: + newreader = getreader(filename, new_dir) + self.coalesce(newreader, writer) + except: + newreader = getreader(filename, current_dir) + self.coalesce(newreader, writer) elif line.startswith('#'): if self.comments: writer.writeline(line) @@ -83,6 +94,23 @@ reader.nextline() reader.close() + +# FIND IMAGE DIRECTORY +# Look for either backslash (win32) or forslash (everything else) +# to find directory where images reside +def get_dir(in_input_file): + directory = '' + if (sys.platform == 'win32'): + input_exp = re.compile('..{1,}\\\\') # Greedy to catch full folder + else: + input_exp = re.compile('..{1,}/') # Greedy to catch full folder + input_obj = input_exp.match(in_input_file) # Must match at beginning of expression + if (input_obj): + directory = input_obj.group() + return directory + + + reader, writer = readargs(sys.argv) if reader: Coalescer().coalesce(reader, writer) Index: io/fileline.py =================================================================== --- io/fileline.py (revisión: 351) +++ io/fileline.py (copia de trabajo) @@ -30,11 +30,15 @@ class LineReader(object): "Reads a file line by line" - def __init__(self, filename): + def __init__(self, filename, directory = ''): if isinstance(filename, file): - self.file = filename + self.file = directory + filename else: self.file = codecs.open(filename, 'rU', 'utf-8') + if (directory != ''): + #Make sure directory has a trailing GNU/Linux type slash + assert(directory.endswith('/') == True) + self.dir = directory self.linenumber = 1 self.lastline = None self.current = None