def parsevalue(self, value): """Parses a value set in a config file, and returns the correct object type""" if not isinstance(value, (str, unicode)): return value # If it's a string, try to parse it if (value.isdigit()): return int(value) elif (value[0] in ['"',"'"] and value[-1] == value[0]): return sparse.stringeval(value) elif (value[0] == 'u' and value[1] in ['"',"'"] and value[-1] == value[1]): return sparse.stringeval(value[1:]).decode("utf-8") else: return self.resolveconfigobject(value)
def parsevalue(self, value): """Parses a value set in a config file, and returns the correct object type""" if not isinstance(value, (str, unicode)): return value # If it's a string, try to parse it if (value.isdigit()): return int(value) elif (value[0] in ['"', "'"] and value[-1] == value[0]): return sparse.stringeval(value) elif (value[0] == 'u' and value[1] in ['"', "'"] and value[-1] == value[1]): return sparse.stringeval(value[1:]).decode("utf-8") else: return self.resolveconfigobject(value)
def parseassignments(self): """parses all the assignments from the tokenized preferences""" self.__dict__.setdefault('removals', {}) self.__dict__.setdefault('valuepos', {}) self.__dict__.setdefault('commentpos', {}) self.__dict__.setdefault('sectionstart', {}) self.__dict__.setdefault('sectionend', {}) assignvar = None operation = None lastcomment = None lastvalue = None lastsection = None lastindent = indent("") context = {} indentlevels = {} self.refreshposcache() for tokennum, token in enumerate(self.tokens): if isinstance(token, indent): if token.level < lastindent.level: parentcontext = ".".join( [context[level] for level in range(token.level)]) for level in range(token.level, lastindent.level): if level in context: if not parentcontext: childcontext = context[level] else: childcontext = parentcontext + "." + context[ level] self.sectionend[childcontext] = ( tokennum, indentlevels[level + 1]) parentcontext = childcontext del context[level] elif token.level > lastindent.level: if operation == ':': operation = None else: self.raiseerror("indent without preceding :", tokennum) lastindent = token indentlevels[lastindent.level] = token elif self.iscommenttoken(token): # if the last value or section found is on the same line # as this comment then this comment refers to that pref lastcomment = (tokennum, token) if (lastvalue is not None) and (lastsection is not None): commentline, commentcharpos = self.getlinepos( self.findtokenpos(tokennum)) valuenum, value = lastvalue vline, vpos = self.getlinepos(self.findtokenpos(valuenum)) sectionnum, section = lastsection sectionline, sectionpos = self.getlinepos( self.findtokenpos(sectionnum)) if commentline == vline: self.commentpos[value] = tokennum lastcomment = None elif commentline == sectionline: self.commentpos[section] = tokennum lastcomment = None elif token == '=': operation = token elif token == ':': context[lastindent.level] = assignvar operation = token prefixes = [ context[level] for level in range(0, lastindent.level) ] key = ".".join(prefixes + [assignvar]) self.sectionstart[key] = tokennum lastsection = (tokennum, key) elif operation == '=': prefixes = [ context[level] for level in range(0, lastindent.level) ] key = ".".join(prefixes + [assignvar]) realvalue = self.parsevalue(token) self.setvalue(key, realvalue) self.valuepos[key] = tokennum operation = None lastvalue = (tokennum, key) elif operation is None: if self.isstringtoken(token): if token.startswith(self.unicodeprefix): assignvar = sparse.stringeval( token.replace(self.unicodeprefix, "", 1)).decode("utf-8") else: assignvar = sparse.stringeval(token) else: assignvar = token else: self.raiseerror("I don't know how to parse that here", tokennum) # handle comments if operation == '=' or token == ':': # if the last comment found is on the line before this one then it refers to this pref/section if lastcomment is not None: commentnum, comment = lastcomment commentline, commentcharpos = self.getlinepos( self.findtokenpos(commentnum)) myline, mycharpos = self.getlinepos( self.findtokenpos(tokennum)) if myline == (commentline + 1): if token == ':' and lastsection is not None: self.commentpos[lastsection[1]] = commentnum elif operation == '=' and lastvalue is not None: self.commentpos[lastvalue[1]] = commentnum
def parseassignments(self): """parses all the assignments from the tokenized preferences""" self.__dict__.setdefault('removals',{}) self.__dict__.setdefault('valuepos',{}) self.__dict__.setdefault('commentpos',{}) self.__dict__.setdefault('sectionstart',{}) self.__dict__.setdefault('sectionend',{}) assignvar = None operation = None lastcomment = None lastvalue = None lastsection = None lastindent = indent("") context = {} indentlevels = {} self.refreshposcache() for tokennum, token in enumerate(self.tokens): if isinstance(token, indent): if token.level < lastindent.level: parentcontext = ".".join([context[level] for level in range(token.level)]) for level in range(token.level, lastindent.level): if level in context: if not parentcontext: childcontext = context[level] else: childcontext = parentcontext + "." + context[level] self.sectionend[childcontext] = (tokennum, indentlevels[level+1]) parentcontext = childcontext del context[level] elif token.level > lastindent.level: if operation == ':': operation = None else: self.raiseerror("indent without preceding :", tokennum) lastindent = token indentlevels[lastindent.level] = token elif self.iscommenttoken(token): # if the last value or section found is on the same line # as this comment then this comment refers to that pref lastcomment = (tokennum,token) if (lastvalue is not None) and (lastsection is not None): commentline,commentcharpos = self.getlinepos(self.findtokenpos(tokennum)) valuenum,value = lastvalue vline,vpos = self.getlinepos(self.findtokenpos(valuenum)) sectionnum,section = lastsection sectionline,sectionpos = self.getlinepos(self.findtokenpos(sectionnum)) if commentline == vline: self.commentpos[value] = tokennum lastcomment = None elif commentline == sectionline: self.commentpos[section] = tokennum lastcomment = None elif token == '=': operation = token elif token == ':': context[lastindent.level] = assignvar operation = token prefixes = [context[level] for level in range(0, lastindent.level)] key = ".".join(prefixes+[assignvar]) self.sectionstart[key] = tokennum lastsection = (tokennum,key) elif operation == '=': prefixes = [context[level] for level in range(0, lastindent.level)] key = ".".join(prefixes+[assignvar]) realvalue = self.parsevalue(token) self.setvalue(key, realvalue) self.valuepos[key] = tokennum operation = None lastvalue = (tokennum,key) elif operation is None: if self.isstringtoken(token): if token.startswith(self.unicodeprefix): assignvar = sparse.stringeval(token.replace(self.unicodeprefix, "", 1)).decode("utf-8") else: assignvar = sparse.stringeval(token) else: assignvar = token else: self.raiseerror("I don't know how to parse that here", tokennum) # handle comments if operation == '=' or token == ':': # if the last comment found is on the line before this one then it refers to this pref/section if lastcomment is not None: commentnum,comment = lastcomment commentline,commentcharpos = self.getlinepos(self.findtokenpos(commentnum)) myline,mycharpos = self.getlinepos(self.findtokenpos(tokennum)) if myline == (commentline+1): if token == ':' and lastsection is not None: self.commentpos[lastsection[1]] = commentnum elif operation == '=' and lastvalue is not None: self.commentpos[lastvalue[1]] = commentnum