def getArguments(self, menuitem): # Returns list of args and dictionary of kwargs args = [] kwargs = {} if self.state is not AsciiMenuParser.state_arg: return args, kwargs while 1: token0 = self.nextToken() if token0 is None: raise ooferror.ErrDataFileError("Premature EOF in data file?") if token0 in endSequence: # Does no checking for matching () or [] pairs! self.parendepth -= 1 if self.parendepth == 0: self.state = AsciiMenuParser.state_idle return args, kwargs if token0 == ARGSEP: continue token1 = self.nextToken() if token1 != ASSIGN: # not a keyword argument self.pushbackToken(token1) # to be read again args.append(self.getArgumentValue(token0)) else: # key word argument if not legalname(token0): raise ooferror.ErrDataFileError( "Illegal argument name: '%s'" % token0) token2 = self.nextToken() kwargs[token0] = self.getArgumentValue(token2)
def getBytes(self, n): if self.progress.stopped(): self._error = True raise ooferror.ErrDataFileError("Interrupted!") b = FileInput.getBytes(self, n) self.reportProgress() return b
def getLine(self): if self.progress.stopped(): self._error = True raise ooferror.ErrDataFileError("Interrupted!") line = FileInput.getLine(self) self.reportProgress() return line
def getBytes(self, n): b = self.file.read(n) self.bytecount += len(b) if len(b) != n: raise ooferror.ErrDataFileError( "Premature EOF at byte %d! (%d missing)" % (self.bytecount, n - len(b))) return b
def readfile(self, filename, prog, z=0): tslfile = file(filename, "r") prog.setMessage("Reading " + filename) count = 1 # line counter lines = tslfile.readlines() nlines = len(lines) data = utils.ReservableList(nlines) angletype = None for line in lines: if line[0] == '#': if line.startswith("Column 1-3", 2): if "radians" in line: angletype = "radians" else: angletype = "degrees" debug.fmsg("Angles are in %s." % angletype) else: # line[0] != '#' substrings = line.split() if len(substrings) < 5: raise ooferror.ErrUserError( "Too few numbers in line %d of %s" % (count, filename)) values = map(float, substrings[:5]) if angletype == "radians": angles = values[:3] elif angletype == "degrees": angles = map(math.radians, values[:3]) else: raise ooferror.ErrDataFileError( "Angle type not specified in TSL data file") orientation = corientation.COrientBunge(*angles) if config.dimension() == 2: point = primitives.Point(values[3], values[4]) elif config.dimension() == 3: point = primitives.Point(values[3], values[4], z) data.append(DataPoint(point, # position angles, ' '.join(substrings[10:]))) # phase name count += 1 # count actual file lines, comments and all prog.setMessage("read %d/%d lines" % (count, nlines)) prog.setFraction(float(count)/nlines) npts = len(data) debug.fmsg("read %d lines, %d data points" % (count, npts)) # We don't yet know if the points are on a rectangular or a # hexagonal lattice, so split the data up into rows. # getrows() is a generator, but we need an actual list so that # we can index into it. rows = list(getrows(data)) if len(rows) < 2: raise ooferror.ErrUserError( "Orientation map data has too few rows.") if rows[0][0].position[0] != rows[1][0].position[0]: # Must be a hexagonal lattice. Throw out every other row. reporter.warn("Converting hexagonal lattice to rectangular by discarding alternate rows.") rows = rows[::2] # discard odd numbered rows return rows, npts
def run1(self): menuitem = self.mode.getMenuItem(self.menu) if menuitem is None: return 0 args, kwargs = self.mode.getArguments(menuitem) if args: raise ooferror.ErrDataFileError( "All arguments to menu commands must be keyword arguments!") menuitem.parser = self menuitem(**kwargs) menuitem.parser = None return 1
def getArgumentValue(self, token): if token[0] in quotechars: # it's a string return token[1:-1] # strip the quotation marks if token == BGNLIST: self.parendepth += 1 return list(self.getArguments(None)[0]) if token == BGNTUPLE: self.parendepth += 1 return tuple(self.getArguments(None)[0]) try: # is it a number? val = string2number(token) except ValueError: # no, it's not pass else: return val # yes, it's a number # Is it None? if token == 'None': return None if token == 'True': return True if token == 'False': return False # Is it a function or variable defined in the OOF namespace? try: argval = utils.OOFeval_r(token) except KeyError: raise ooferror.ErrDataFileError("Incomprehensible argument: %s" % token) # If it's a function, the next token is an open paren. nexttoken = self.nextToken() if nexttoken == BGNARG: self.parendepth += 1 args, kwargs = self.getArguments(argval) return argval(*args, **kwargs) if nexttoken == BGNINDEX: self.parendepth += 1 args, kwargs = self.getArguments(argval) return argval[args[0]] self.pushbackToken(nexttoken) # to be read again return argval # arg was an OOF namespace variable
def getIdentifier(self): token = self.nextToken() if not token: return None # EOF if self.state is AsciiMenuParser.state_idle: if not legalname(token): raise ooferror.ErrDataFileError("Illegal command: '%s'" % token) self.state = AsciiMenuParser.state_cmd return token if self.state is AsciiMenuParser.state_cmd: if token[0] == CMDSEP: self.state = AsciiMenuParser.state_idle return self.getIdentifier() if token[0] == BGNARG: self.parendepth += 1 self.state = AsciiMenuParser.state_arg return None
def processQuote(self): quotechar = self.buffer[self.bufpos] quote = "" while 1: # look for closing quote end = self.bufpos + 1 while end < self.buflen and self.buffer[end] != quotechar: end += 1 if end == self.buflen: # keep looking! quote += self.buffer[self.bufpos:end] self.fetchLine() # look at next line if not self.buffer: raise ooferror.ErrDataFileError("unmatched quotation marks") else: # found closing quote quote += self.buffer[self.bufpos:end+1] self.bufpos = end + 1 if quote[-2] != ESCAPE: return quote else: quote = quote[:-2] + quote[-1] # remove ESCAPE
def readData(self, tslfile, prog): count = 1 # line counter lines = tslfile.readlines() nlines = len(lines) data = utils.ReservableList(nlines) angletype = None for line in lines: if line[0] == '#': if line.startswith("Column 1-3", 2): if "radians" in line: angletype = "radians" else: angletype = "degrees" else: # line[0] != '#' substrings = line.split() if len(substrings) < 5: raise ooferror.ErrUserError( "Too few numbers in line %d of %s" % (count, tslfile.name)) values = map(float, substrings[:5]) if angletype == "radians": angles = values[:3] angles[0] = angles[0] - math.radians(self.angle_offset) elif angletype == "degrees": angles[0] = angles[0] - self.angle_offset angles = map(math.radians, values[:3]) else: raise ooferror.ErrDataFileError( "Angle type not specified in TSL data file") data.append( DataPoint( primitives.Point(values[3], values[4]), # position angles, ' '.join(substrings[10:]))) # phase name count += 1 # count actual file lines, comments and all prog.setMessage("read %d/%d lines" % (count, nlines)) prog.setFraction(float(count) / nlines) npts = len(data) debug.fmsg("read %d lines, %d data points" % (count, npts)) return data, None # None ==> hexgrid not detected yet