def build_message(): global message message = Group(Optional(Suppress(Literal(":")) + prefix + space)) + Group(command) + Group(Optional(params)) if config.getboolean("parser", "trailing_spaces"): message += ZeroOrMore(space) if config.getboolean("parser", "soft_eol"): message += cr ^ lf ^ crlf else: message += crlf message.leaveWhitespace()
def build_message(): global message message = Group(Optional(Suppress(Literal(':')) + prefix + space)) + \ Group(command) + \ Group(Optional(params)) if config.getboolean('parser', 'trailing_spaces'): message += ZeroOrMore(space) if config.getboolean('parser', 'soft_eol'): message += cr ^ lf ^ crlf else: message += crlf message.leaveWhitespace()
def __init__(self): if not ParserElement: return with warnings.catch_warnings(): # In Python 2.6, pyparsing throws warnings on its own code. warnings.simplefilter("ignore") orOperator = Suppress( CaselessLiteral("OR")).setResultsName("OR_OPERATOR") quoteContents = Group(Word(ALLCHARS.replace("\"", ""))) quoteContents.leaveWhitespace() quotedWord = Group(Suppress('"') + quoteContents + Suppress('"')).setResultsName("QUOTES") plainWord = Group( NotAny(CaselessLiteral("OR")) + Word(WORDCHARS.replace("-", ""), WORDCHARS)).setResultsName( "PLAINWORD") anyWord = Group( NotAny('(') + ~FollowedBy(')') + Word(ALLWORDCHARS)).setResultsName("ANYWORD") keyWord = Group( Combine( Optional("-") + Word(string.ascii_letters) + Literal(":") + (Word(WORDCHARS) | quotedWord))).setResultsName("KEYWORD") notExpr = Group( Suppress("-") + NotAny(string.whitespace) + (quotedWord | plainWord)).setResultsName("NOT") word = Group(keyWord | notExpr | quotedWord | plainWord).setResultsName("WORD") grammar = Forward() parens = Forward() orOperand = Group(word | parens | notExpr | anyWord).setResultsName("OR_OPERAND") orExpr = Group( FollowedBy(orOperand + orOperator + orOperand) + Group(orOperand + OneOrMore(orOperator + orOperand)) ).setResultsName("OR_EXPRESSION") oneExpr = Group(orExpr | parens | word | anyWord).setResultsName("ONE EXPRESSION") parens <<= Group( Group(Optional("-")).setResultsName("NOT_PARENTHESIS") + Suppress("(") + ZeroOrMore(parens | grammar) + Suppress(")")).setResultsName("PARENTHESIS") grammar <<= ((oneExpr + grammar) | oneExpr).setResultsName("GRAMMAR") self._grammar = grammar
# Parser definition # A little more complicated than is needed today, but needed to preserve # comments in future versions. identifier = Word(alphas, alphanums + '_-') blank = White(' \t\n\r\f') #.leaveWhitespace() comment = Combine(Optional(White(' \t')) + '#' + restOfLine) comment_header = Optional(Combine(ZeroOrMore(comment | blank)), default='') comment_inline = Optional(comment, default= '') item = Group(comment_header + identifier + comment_inline) section_header = comment_header + Suppress('[') + identifier + Suppress(']') + comment_inline section_body = Group(ZeroOrMore(item)) section = Group(section_header + Optional(section_body, [])) parser = Group(ZeroOrMore(section)) + Optional(comment_header) parser.leaveWhitespace() def parse_INIFile(filename): info = {} secs, _ = parser.parseFile(filename) for _, sec, _, items in secs: sec = unicode(sec) info[sec] = set() for item in items: _, v, _ = item v = unicode(v) info[sec].add(v) return info