def new_element(elemstring): """Return a new Disc, Menu, or Video created from the given string. Example: elemstring = 'Menu "Main menu" -format dvd -tvsys ntsc'.""" tokens = tokenize(elemstring) type = tokens.pop(0) name = tokens.pop(0) opts = tokens + ['out', name] newelem = element_classes[type](opts) newelem.indent = indent_level(elemstring) return newelem
def _parse(self, options): """Parse a string or list of options, returning a dictionary of those that match self.defdict.""" custom = {} # If options is a string, tokenize it before proceeding if options.__class__ == str: options = tokenize(options) while len(options) > 0: opt = options.pop(0).lstrip('-') if opt not in self.defdict: log.error("Unrecognized option: %s. Ignoring." % opt) else: expected_args = self.defdict[opt].num_args() # Is this option an alias (short-form) of another option? # (i.e., -vcd == -format vcd) if self.defdict[opt].alias: key, value = self.defdict[opt].alias custom[key] = value # No args elif expected_args == 0: custom[opt] = True # One arg elif expected_args == 1: arg = options.pop(0) custom[opt] = arg # Comma-separated list of args elif expected_args < 0: arglist = [] next = options.pop(0) # If next is a keyword in defs, print error if next in self.defdict: log.error('"%s" option expects one or more arguments ' \ '(got keyword "%s")' % (opt, next)) # Until the next keyword is reached, add to list while next and next.lstrip('-') not in self.defdict: # Ignore any surrounding [ , , ] arglist.append(next.lstrip('[').rstrip(',]')) next = options.pop(0) # Put the last token back options.insert(0, next) custom[opt] = arglist return custom
def get_elements(filename): """Get a list of elements from the given filename.""" lines = get_code_lines(filename) # Condense lines so there's only one element definition per line condlines = [] lastline = '' while len(lines) > 0: curline = lines.pop(0) tokens = tokenize(curline) if tokens[0] in element_classes: if lastline: condlines.append(lastline) lastline = curline.rstrip(' \r\n') else: lastline += ' ' + curline.lstrip().rstrip(' \r\n') condlines.append(lastline) # Create and return a list of elements elems = [] for line in condlines: elems.append(new_element(line)) return elems