Python pyparsing.nums() Examples
The following are 14
code examples of pyparsing.nums().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pyparsing
, or try the search function
.
Example #1
Source File: httpServerLogParser.py From phpsploit with GNU General Public License v3.0 | 6 votes |
def getLogLineBNF(): global logLineBNF if logLineBNF is None: integer = Word( nums ) ipAddress = delimitedList( integer, ".", combine=True ) timeZoneOffset = Word("+-",nums) month = Word(string.uppercase, string.lowercase, exact=3) serverDateTime = Group( Suppress("[") + Combine( integer + "/" + month + "/" + integer + ":" + integer + ":" + integer + ":" + integer ) + timeZoneOffset + Suppress("]") ) logLineBNF = ( ipAddress.setResultsName("ipAddr") + Suppress("-") + ("-" | Word( alphas+nums+"@._" )).setResultsName("auth") + serverDateTime.setResultsName("timestamp") + dblQuotedString.setResultsName("cmd").setParseAction(getCmdFields) + (integer | "-").setResultsName("statusCode") + (integer | "-").setResultsName("numBytesSent") + dblQuotedString.setResultsName("referrer").setParseAction(removeQuotes) + dblQuotedString.setResultsName("clientSfw").setParseAction(removeQuotes) ) return logLineBNF
Example #2
Source File: username.py From ccat with GNU General Public License v3.0 | 6 votes |
def _globalParse___username_attributes(line): username_dict = {} username = (Word(printables)) ('user') privilege = (Suppress('privilege') + Word(nums)) ('priv_num') password_type = (Suppress(MatchFirst(['secret', 'password'])) + Word(nums))('pass_type') parse_username = username + Optional(privilege) + password_type + Suppress(restOfLine) result = parse_username.parseString(line) username_dict[result.user] = {} username_dict[result.user]['password_type'] = result.pass_type.asList()[0] try: username_dict[result.user]['privilege'] = result.priv_num.asList()[0] except AttributeError: pass return username_dict
Example #3
Source File: _parser.py From pingparsing with MIT License | 6 votes |
def _parse_duplicate(self, line: str) -> Optional[int]: if not self._is_support_packet_duplicate: return None packet_pattern = ( pp.SkipTo(pp.Word("+" + pp.nums) + pp.Literal("duplicates,")) + pp.Word("+" + pp.nums) + pp.Literal("duplicates,") ) try: duplicate_parse_list = packet_pattern.parseString(_to_unicode(line)) except pp.ParseException: return 0 return int(duplicate_parse_list[-2].strip("+"))
Example #4
Source File: aaa.py From ccat with GNU General Public License v3.0 | 5 votes |
def _globalParse___aaa_attributes(line, type, count_aaa): aaa_dict = {} authentication_list = (Suppress('login') + Word(printables)) ('authent_list') authentication_groups = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('authent_methods') parse_authentication = authentication_list + authentication_groups # parse_authorization_options = MatchFirst(['exec', 'login']) + Word(printables) + OneOrMore(Optional(Suppress('group')) + Word(printables)) accounting_login = (MatchFirst(['exec', 'network', 'connection', 'commands'])) ('acc_login') accounting_list = (Optional(Word(nums)) + Word(printables)) ('acc_list') accounting_record = (MatchFirst(['start-stop', 'stop-only', 'stop'])) ('acc_record') accounting_methods = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('acc_methods') parse_accounting = accounting_login + accounting_list + accounting_record + accounting_methods if type == 'authentication': result = parse_authentication.parseString(line) aaa_dict.update({'login' + str(count_aaa): {}}) aaa_dict['login' + str(count_aaa)]['list'] = result.authent_list[0] aaa_dict['login' + str(count_aaa)]['methods'] = result.authent_methods.asList() # elif type == 'authorization': # result = parse_authorization_options.parseString(line) # aaa_dict.update({'login' + str(count_aaa): {}}) # aaa_dict['login' + str(count_aaa)]['login'] = result.pop(0) # aaa_dict['login' + str(count_aaa)]['list'] = result.pop(0) # aaa_dict['login' + str(count_aaa)]['methods'] = result.asList() elif type == 'accounting': result = parse_accounting.parseString(line) aaa_dict.update({'login' + str(count_aaa): {}}) aaa_dict['login' + str(count_aaa)]['login'] = result.acc_login aaa_dict['login' + str(count_aaa)]['list'] = result.acc_list.asList() aaa_dict['login' + str(count_aaa)]['record'] = result.acc_record aaa_dict['login' + str(count_aaa)]['methods'] = result.acc_methods.asList() return aaa_dict
Example #5
Source File: _qdisc.py From tcconfig with MIT License | 5 votes |
def parse(self, device, text): self._clear() if typepy.is_null_string(text): return [] text = text.strip() for line in text.splitlines(): if typepy.is_null_string(line): continue line = self._to_unicode(line.lstrip()) if re.search("^qdisc netem |^qdisc htb |^qdisc tbf ", line) is None: continue if re.search("^qdisc htb ", line) is not None: self.__parse_direct_qlen(line) continue if re.search("^qdisc netem ", line) is not None: self.__parse_netem_param(line, "parent", pp.hexnums + ":") self.__parsed_param[Tc.Param.DEVICE] = device self.__parse_netem_param(line, "netem", pp.hexnums + ":", "handle") self.__parse_netem_param(line, "delay", pp.nums + ".msu") self.__parse_netem_delay_distro(line) self.__parse_netem_param(line, "loss", pp.nums + ".%") self.__parse_netem_param(line, "duplicate", pp.nums + ".%") self.__parse_netem_param(line, "corrupt", pp.nums + ".%") self.__parse_netem_param(line, "reorder", pp.nums + ".%") self.__parse_bandwidth_rate(line) logger.debug("parse a qdisc entry: {}".format(self.__parsed_param)) Qdisc.insert(Qdisc(**self.__parsed_param)) self._clear()
Example #6
Source File: _qdisc.py From tcconfig with MIT License | 5 votes |
def __parse_netem_delay_distro(self, line): parse_param_name = "delay" pattern = ( pp.SkipTo(parse_param_name, include=True) + pp.Word(pp.nums + ".msu") + pp.Word(pp.nums + ".msu") ) try: parsed_list = pattern.parseString(line) self.__parsed_param[parse_param_name] = parsed_list[2] self.__parsed_param["delay-distro"] = parsed_list[3] except pp.ParseException: pass
Example #7
Source File: jsLiteralParse.py From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License | 4 votes |
def jsParse(inStr): # This disaster is a context-free grammar parser for parsing javascript object literals. # It needs to be able to handle a lot of the definitional messes you find in in-the-wild # javascript object literals. # Unfortunately, Javascript is /way/ more tolerant then JSON when it comes to object literals # so we can't just parse objects using python's `json` library. TRUE = pp.Keyword("true").setParseAction( pp.replaceWith(True) ) FALSE = pp.Keyword("false").setParseAction( pp.replaceWith(False) ) NULL = pp.Keyword("null").setParseAction( pp.replaceWith(None) ) jsonString = pp.quotedString.setParseAction( pp.removeQuotes ) jsonNumber = pp.Combine( pp.Optional('-') + ( '0' | pp.Word('123456789',pp.nums) ) + pp.Optional( '.' + pp.Word(pp.nums) ) + pp.Optional( pp.Word('eE',exact=1) + pp.Word(pp.nums+'+-',pp.nums) ) ) jsonObject = pp.Forward() jsonValue = pp.Forward() jsonDict = pp.Forward() jsonArray = pp.Forward() jsonElements = pp.Forward() rawText = pp.Regex('[a-zA-Z_$][0-9a-zA-Z_$]*') commaToNull = pp.Word(',,', exact=1).setParseAction(pp.replaceWith(None)) jsonElements << pp.ZeroOrMore(commaToNull) + pp.Optional(jsonObject) + pp.ZeroOrMore((pp.Suppress(',') + jsonObject) | commaToNull) jsonValue << ( jsonString | jsonNumber | TRUE | FALSE | NULL ) dictMembers = pp.delimitedList( pp.Group( (rawText | jsonString) + pp.Suppress(':') + (jsonValue | jsonDict | jsonArray))) jsonDict << ( pp.Dict( pp.Suppress('{') + pp.Optional(dictMembers) + pp.ZeroOrMore(pp.Suppress(',')) + pp.Suppress('}') ) ) jsonArray << ( pp.Group(pp.Suppress('[') + pp.Optional(jsonElements) + pp.Suppress(']') ) ) jsonObject << (jsonValue | jsonDict | jsonArray) jsonComment = pp.cppStyleComment jsonObject.ignore( jsonComment ) def convertDict(s, l, toks): return dict(toks.asList()) def convertNumbers(s,l,toks): n = toks[0] try: return int(n) except ValueError: return float(n) jsonNumber.setParseAction(convertNumbers) jsonDict.setParseAction(convertDict) # jsonObject.setDebug() jsonObject.parseString('"inStr"').pop() return jsonObject.parseString(inStr).pop() # Stolen from http://stackoverflow.com/a/12017573/268006
Example #8
Source File: shparsers.py From stash with MIT License | 4 votes |
def __init__(self, debug=False): self.debug = debug self.logger = logging.getLogger('StaSh.Parser') escaped = pp.Combine("\\" + pp.Word(pp.printables + ' ', exact=1)).setParseAction(self.escaped_action) escaped_oct = pp.Combine("\\" + pp.Word('01234567', max=3)).setParseAction(self.escaped_oct_action) escaped_hex = pp.Combine("\\x" + pp.Word('0123456789abcdefABCDEF', exact=2)).setParseAction(self.escaped_hex_action) # Some special uq_word is needed, e.g. &3 for file descriptor of Pythonista interactive prompt uq_word = (pp.Literal('&3') | pp.Word(_WORD_CHARS)).setParseAction(self.uq_word_action) bq_word = pp.QuotedString('`', escChar='\\', unquoteResults=False).setParseAction(self.bq_word_action) dq_word = pp.QuotedString('"', escChar='\\', unquoteResults=False).setParseAction(self.dq_word_action) sq_word = pp.QuotedString("'", escChar='\\', unquoteResults=False).setParseAction(self.sq_word_action) # The ^ operator means longest match (as opposed to | which means first match) word = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ uq_word ^ bq_word ^ dq_word ^ sq_word))\ .setParseAction(self.word_action) identifier = pp.Word(pp.alphas + '_', pp.alphas + pp.nums + '_').setParseAction(self.identifier_action) assign_op = pp.Literal('=').setParseAction(self.assign_op_action) assignment_word = pp.Combine(identifier + assign_op + word).setParseAction(self.assignment_word_action) punctuator = pp.oneOf('; &').setParseAction(self.punctuator_action) pipe_op = pp.Literal('|').setParseAction(self.pipe_op_action) io_redirect_op = pp.oneOf('>> >').setParseAction(self.io_redirect_op_action) io_redirect = (io_redirect_op + word)('io_redirect') # The optional ' ' is a workaround to a possible bug in pyparsing. # The position of cmd_word after cmd_prefix is always reported 1 character ahead # of the correct value. cmd_prefix = (pp.OneOrMore(assignment_word) + pp.Optional(' '))('cmd_prefix') cmd_suffix = (pp.OneOrMore(word)('args') + pp.Optional(io_redirect)) ^ io_redirect modifier = pp.oneOf('! \\') cmd_word = (pp.Combine(pp.Optional(modifier) + word) ^ word)('cmd_word').setParseAction(self.cmd_word_action) simple_command = \ (cmd_prefix + pp.Optional(cmd_word) + pp.Optional(cmd_suffix)) \ | (cmd_word + pp.Optional(cmd_suffix)) simple_command = pp.Group(simple_command) pipe_sequence = simple_command + pp.ZeroOrMore(pipe_op + simple_command) pipe_sequence = pp.Group(pipe_sequence) complete_command = pp.Optional(pipe_sequence + pp.ZeroOrMore(punctuator + pipe_sequence) + pp.Optional(punctuator)) # --- special parser for inside double quotes uq_word_in_dq = pp.Word(pp.printables.replace('`', ' ').replace('\\', ''))\ .setParseAction(self.uq_word_action) word_in_dq = pp.Combine(pp.OneOrMore(escaped ^ escaped_oct ^ escaped_hex ^ bq_word ^ uq_word_in_dq)) # --- self.parser = complete_command.parseWithTabs().ignore(pp.pythonStyleComment) self.parser_within_dq = word_in_dq.leaveWhitespace() self.next_word_type = ShParser._NEXT_WORD_CMD self.tokens = [] self.parts = []
Example #9
Source File: evaluator.py From manila with Apache License 2.0 | 4 votes |
def _def_parser(): # Enabling packrat parsing greatly speeds up the parsing. pyparsing.ParserElement.enablePackrat() alphas = pyparsing.alphas Combine = pyparsing.Combine Forward = pyparsing.Forward nums = pyparsing.nums oneOf = pyparsing.oneOf opAssoc = pyparsing.opAssoc operatorPrecedence = pyparsing.operatorPrecedence Word = pyparsing.Word integer = Word(nums) real = Combine(Word(nums) + '.' + Word(nums)) variable = Word(alphas + '_' + '.') number = real | integer expr = Forward() fn = Word(alphas + '_' + '.') operand = number | variable | fn signop = oneOf('+ -') addop = oneOf('+ -') multop = oneOf('* /') comparisonop = oneOf(' '.join(EvalComparisonOp.operations.keys())) ternaryop = ('?', ':') boolandop = oneOf('AND and &&') boolorop = oneOf('OR or ||') negateop = oneOf('NOT not !') operand.setParseAction(EvalConstant) expr = operatorPrecedence(operand, [ (fn, 1, opAssoc.RIGHT, EvalFunction), ("^", 2, opAssoc.RIGHT, EvalPowerOp), (signop, 1, opAssoc.RIGHT, EvalSignOp), (multop, 2, opAssoc.LEFT, EvalMultOp), (addop, 2, opAssoc.LEFT, EvalAddOp), (negateop, 1, opAssoc.RIGHT, EvalNegateOp), (comparisonop, 2, opAssoc.LEFT, EvalComparisonOp), (ternaryop, 3, opAssoc.LEFT, EvalTernaryOp), (boolandop, 2, opAssoc.LEFT, EvalBoolAndOp), (boolorop, 2, opAssoc.LEFT, EvalBoolOrOp), (',', 2, opAssoc.RIGHT, EvalCommaSeperator), ]) return expr
Example #10
Source File: searchparser.py From patzilla with GNU Affero General Public License v3.0 | 4 votes |
def parser(self): """ This function returns a parser. The grammar should be like most full text search engines (Google, Tsearch, Lucene). Grammar: - a query consists of alphanumeric words, with an optional '*' wildcard at the end of a word - a sequence of words between quotes is a literal string - words can be used together by using operators ('and' or 'or') - words with operators can be grouped with parenthesis - a word or group of words can be preceded by a 'not' operator - the 'and' operator precedes an 'or' operator - if an operator is missing, use an 'and' operator """ operatorOr = Forward() operatorWord = Word(wordchars).setResultsName('value') operatorQuotesContent = Forward() operatorQuotesContent << ( (operatorWord + operatorQuotesContent) | operatorWord ) operatorQuotes = Group( Suppress('"') + operatorQuotesContent + Suppress('"') ).setResultsName("quotes") | operatorWord prefix = (Word(alphanums).setResultsName('index') + Word('=').setResultsName('binop')) operatorParenthesis = Group( Optional(prefix) + (Suppress("(") + operatorOr + Suppress(")")) ).setResultsName("parenthesis") | Group(prefix + operatorQuotes).setResultsName('term') | operatorQuotes operatorNot = Forward() operatorNot << (Group( Suppress(Keyword("not", caseless=True)) + operatorNot ).setResultsName("not") | operatorParenthesis) operatorAnd = Forward() operatorAnd << (Group( operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd ).setResultsName("and") | Group( operatorNot + OneOrMore(~oneOf("and or", caseless=True) + operatorAnd) ).setResultsName("and") | operatorNot) operatorProximity = Forward() operatorProximity << (Group( operatorParenthesis + Suppress(Literal("near,")) + Word(nums).setResultsName('distance') + operatorParenthesis ).setResultsName("near") | Group( operatorParenthesis + Suppress(Literal("span,")) + Word(nums).setResultsName('distance') + operatorParenthesis ).setResultsName("span") | operatorAnd) operatorOr << (Group( operatorProximity + Suppress(Keyword("or", caseless=True)) + operatorOr ).setResultsName("or") | operatorProximity) return operatorOr.parseString
Example #11
Source File: _parser.py From pingparsing with MIT License | 4 votes |
def parse(self, ping_message: List[str]) -> PingStats: icmp_replies = self._parse_icmp_reply(ping_message) stats_headline, packet_info_line, body_line_list = self._preprocess_parse_stats( lines=ping_message ) packet_pattern = ( pp.Word(pp.nums) + pp.Literal("packets transmitted,") + pp.Word(pp.nums) + pp.Literal("received,") ) destination = self._parse_destination(stats_headline) duplicates = self._parse_duplicate(packet_info_line) parse_list = packet_pattern.parseString(_to_unicode(packet_info_line)) packet_transmit = int(parse_list[0]) packet_receive = int(parse_list[2]) is_valid_data = True try: rtt_line = body_line_list[1] except IndexError: is_valid_data = False if not is_valid_data or typepy.is_null_string(rtt_line): return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, icmp_replies=icmp_replies, ) rtt_pattern = ( pp.Literal("rtt min/avg/max/mdev =") + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + pp.Word(pp.nums + "ms") ) parse_list = rtt_pattern.parseString(_to_unicode(rtt_line)) return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, rtt_min=float(parse_list[1]), rtt_avg=float(parse_list[3]), rtt_max=float(parse_list[5]), rtt_mdev=float(parse_list[7]), icmp_replies=icmp_replies, )
Example #12
Source File: _parser.py From pingparsing with MIT License | 4 votes |
def parse(self, ping_message: List[str]) -> PingStats: icmp_replies = self._parse_icmp_reply(ping_message) stats_headline, packet_info_line, body_line_list = self._preprocess_parse_stats( lines=ping_message ) packet_pattern = ( pp.Literal("Packets: Sent = ") + pp.Word(pp.nums) + pp.Literal(", Received = ") + pp.Word(pp.nums) ) destination = self._parse_destination(stats_headline) duplicates = self._parse_duplicate(packet_info_line) parse_list = packet_pattern.parseString(_to_unicode(packet_info_line)) packet_transmit = int(parse_list[1]) packet_receive = int(parse_list[3]) is_valid_data = True try: rtt_line = body_line_list[2].strip() except IndexError: is_valid_data = False if not is_valid_data or typepy.is_null_string(rtt_line): return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, icmp_replies=icmp_replies, ) rtt_pattern = ( pp.Literal("Minimum = ") + pp.Word(pp.nums) + pp.Literal("ms, Maximum = ") + pp.Word(pp.nums) + pp.Literal("ms, Average = ") + pp.Word(pp.nums) ) parse_list = rtt_pattern.parseString(_to_unicode(rtt_line)) return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, rtt_min=float(parse_list[1]), rtt_avg=float(parse_list[5]), rtt_max=float(parse_list[3]), icmp_replies=icmp_replies, )
Example #13
Source File: _parser.py From pingparsing with MIT License | 4 votes |
def parse(self, ping_message: List[str]) -> PingStats: icmp_replies = self._parse_icmp_reply(ping_message) stats_headline, packet_info_line, body_line_list = self._preprocess_parse_stats( lines=ping_message ) packet_pattern = ( pp.Word(pp.nums) + pp.Literal("packets transmitted,") + pp.Word(pp.nums) + pp.Literal("packets received,") ) destination = self._parse_destination(stats_headline) duplicates = self._parse_duplicate(packet_info_line) parse_list = packet_pattern.parseString(_to_unicode(packet_info_line)) packet_transmit = int(parse_list[0]) packet_receive = int(parse_list[2]) is_valid_data = True try: rtt_line = body_line_list[1] except IndexError: is_valid_data = False if not is_valid_data or typepy.is_null_string(rtt_line): return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, icmp_replies=icmp_replies, ) rtt_pattern = ( pp.Literal("round-trip min/avg/max/stddev =") + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + pp.Word(pp.nums + "ms") ) parse_list = rtt_pattern.parseString(_to_unicode(rtt_line)) return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, rtt_min=float(parse_list[1]), rtt_avg=float(parse_list[3]), rtt_max=float(parse_list[5]), rtt_mdev=float(parse_list[7]), icmp_replies=icmp_replies, )
Example #14
Source File: _parser.py From pingparsing with MIT License | 4 votes |
def parse(self, ping_message: List[str]) -> PingStats: icmp_replies = self._parse_icmp_reply(ping_message) stats_headline, packet_info_line, body_line_list = self._preprocess_parse_stats( lines=ping_message ) packet_pattern = ( pp.Word(pp.nums) + pp.Literal("packets transmitted,") + pp.Word(pp.nums) + pp.Literal("packets received,") ) destination = self._parse_destination(stats_headline) duplicates = self._parse_duplicate(packet_info_line) parse_list = packet_pattern.parseString(_to_unicode(packet_info_line)) packet_transmit = int(parse_list[0]) packet_receive = int(parse_list[2]) is_valid_data = True try: rtt_line = body_line_list[1] except IndexError: is_valid_data = False if not is_valid_data or typepy.is_null_string(rtt_line): return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, icmp_replies=icmp_replies, ) rtt_pattern = ( pp.Literal("round-trip min/avg/max =") + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + "/" + pp.Word(pp.nums + ".") + pp.Word(pp.nums + "ms") ) parse_list = rtt_pattern.parseString(_to_unicode(rtt_line)) return PingStats( destination=destination, packet_transmit=packet_transmit, packet_receive=packet_receive, duplicates=duplicates, rtt_min=float(parse_list[1]), rtt_avg=float(parse_list[3]), rtt_max=float(parse_list[5]), icmp_replies=icmp_replies, )