Python pyparsing.ParseResults() Examples
The following are 30
code examples of pyparsing.ParseResults().
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: skydrivelog.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parse each record structure and return an EventObject if applicable. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in ('logline', 'no_header_single_line'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) if key == 'logline': self._ParseLogline(parser_mediator, structure) elif key == 'no_header_single_line': self._ParseNoHeaderSingleLine(parser_mediator, structure)
Example #2
Source File: amply.py From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _addSimpleData(self, data_list, data): if isinstance(data[0], ParseResults): inferred_dimen = len(data[0]) else: inferred_dimen = 1 if self.dimen == None: # infer dimension from records self.dimen = inferred_dimen if self.current_slice == None: self._setSlice(tuple(['*'] * self.dimen)) if len(self.free_indices) == inferred_dimen: for d in data.asList(): self._addValue(data_list, d) elif len(self.free_indices) > 1 and inferred_dimen: for c in chunk(data, len(self.free_indices)): self._addValue(data_list, c) else: raise AmplyError("Dimension of elements (%d) does not match " "declared dimension, (%d)" % (inferred_dimen, self.dimen))
Example #3
Source File: expansions.py From pyjsgf with MIT License | 6 votes |
def _set_matcher_element_attributes(self, element): # Set the ParserElement's action. element.setParseAction(self._parse_action) # Save the element's original postParse function. closure = element.postParse # Set a new function and use the original function for returning values. def postParse(instring, loc, tokenlist): if isinstance(tokenlist, pyparsing.ParseResults): s = " ".join(tokenlist.asList()) elif isinstance(tokenlist, list): s = "".join(tokenlist) elif isinstance(tokenlist, string_types): s = tokenlist else: raise TypeError("postParse received invalid tokenlist %s" % tokenlist) self.matching_slice = slice(loc - len(s), loc) return closure(instring, loc, tokenlist) element.postParse = postParse # Return the element. return element
Example #4
Source File: parse_metadata.py From pybel with MIT License | 6 votes |
def handle_document(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Handle statements like ``SET DOCUMENT X = "Y"``. :raises: InvalidMetadataException :raises: VersionFormatWarning """ key = tokens['key'] value = tokens['value'] if key not in DOCUMENT_KEYS: raise InvalidMetadataException(self.get_line_number(), line, position, key, value) norm_key = DOCUMENT_KEYS[key] if norm_key in self.document_metadata: logger.warning('Tried to overwrite metadata: %s', key) return tokens self.document_metadata[norm_key] = value if norm_key == METADATA_VERSION: self.raise_for_version(line, position, value) return tokens
Example #5
Source File: parse_metadata.py From pybel with MIT License | 6 votes |
def handle_annotations_url(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Handle statements like ``DEFINE ANNOTATION X AS URL "Y"``. :raises: RedefinedAnnotationError """ keyword = tokens['name'] self.raise_for_redefined_annotation(line, position, keyword) url = tokens['url'] self.annotation_url_dict[keyword] = url if self.skip_validation: return tokens self.annotation_to_term[keyword] = self.manager.get_annotation_entry_names(url) return tokens
Example #6
Source File: ast.py From monasca-analytics with Apache License 2.0 | 6 votes |
def make_span(s, l, t): def compute_hi(init_loc, tokens): hi = init_loc for tok in tokens: if isinstance(tok, ASTNode): hi = max(hi, tok.span.hi) elif isinstance(tok, six.string_types): hi += len(tok) elif isinstance(tok, p.ParseResults): hi = max(hi, compute_hi(init_loc, tok)) else: raise exception.BananaGrammarBug( "Couldn't create span for: {}".format(tok) ) return hi if len(t) > 0: span_hi = compute_hi(l, t) return Span(s, l, span_hi) else: return Span(s, l, 2)
Example #7
Source File: ast.py From monasca-analytics with Apache License 2.0 | 6 votes |
def __init__(self, span, expr_tree): """ Construct an expression :type span: Span :param span: Span for the expression. :type expr_tree: p.ParseResults ;:param expr_tree: The tree generated by pyparsing.infixNotation """ super(Expr, self).__init__(span) # We don't use this tree at this point. # During typecheck we will make sure # that the expression can evaluate # Finally during evaluation, we will evaluate # the final result. if isinstance(expr_tree, p.ParseResults): expr_tree = expr_tree.asList() if isinstance(expr_tree, list): for i in range(0, len(expr_tree)): if isinstance(expr_tree[i], list): expr_tree[i] = Expr(span, expr_tree[i]) self.expr_tree = expr_tree else: self.expr_tree = [expr_tree]
Example #8
Source File: winfirewall.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in ('comment', 'logline'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) if key == 'comment': self._ParseCommentRecord(structure) elif key == 'logline': self._ParseLogLine(parser_mediator, structure) # pylint: disable=unused-argument
Example #9
Source File: iis.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): structure parsed from the log file. Raises: ParseError: when the structure type is unknown. """ if key not in ('comment', 'logline'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) if key == 'logline': self._ParseLogLine(parser_mediator, structure) elif key == 'comment': self._ParseComment(structure) # pylint: disable=unused-argument
Example #10
Source File: google_logging.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a matching entry. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): elements parsed from the file. Raises: ParseError: when the structure type is unknown. """ if key not in self._SUPPORTED_KEYS: raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) if key == 'greeting': self._ReadGreeting(structure) elif key == 'log_entry': self._ParseLine(parser_mediator, structure)
Example #11
Source File: mac_securityd.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in ('logline', 'repeated'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) self._ParseLogLine(parser_mediator, structure, key)
Example #12
Source File: vsftpd.py From plaso with Apache License 2.0 | 6 votes |
def _GetTimeElementsTuple(self, structure): """Retrieves a time elements tuple from the structure. Args: structure (pyparsing.ParseResults): structure of tokens derived from a line of a vsftp log file. Returns: tuple: containing: year (int): year. month (int): month, where 1 represents January. day_of_month (int): day of month, where 1 is the first day of the month. hours (int): hours. minutes (int): minutes. seconds (int): seconds. """ time_elements_tuple = self._GetValueFromStructure(structure, 'date_time') _, month, day_of_month, hours, minutes, seconds, year = time_elements_tuple month = timelib.MONTH_DICT.get(month.lower(), 0) return (year, month, day_of_month, hours, minutes, seconds)
Example #13
Source File: setupapi.py From plaso with Apache License 2.0 | 6 votes |
def _GetTimeElements(self, time_structure): """Builds time elements from a setupapi time_stamp field. Args: time_structure (pyparsing.ParseResults): structure of tokens derived from a setupapi time_stamp field. Returns: dfdatetime.TimeElements: date and time extracted from the value or None if the structure does not represent a valid date and time value. """ try: date_time = dfdatetime_time_elements.TimeElementsInMilliseconds( time_elements_tuple=time_structure) # Setupapi logs store date and time values in local time. date_time.is_local_time = True return date_time except ValueError: return None
Example #14
Source File: skydrivelog.py From plaso with Apache License 2.0 | 6 votes |
def _ParseNoHeaderSingleLine(self, parser_mediator, structure): """Parse an isolated header line and store appropriate attributes. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. """ if not self._last_event_data: logger.debug('SkyDrive, found isolated line with no previous events') return event_data = SkyDriveOldLogEventData() event_data.offset = self._last_event_data.offset event_data.text = self._GetValueFromStructure(structure, 'text') event = time_events.DateTimeValuesEvent( self._last_date_time, definitions.TIME_DESCRIPTION_ADDED) parser_mediator.ProduceEventWithEventData(event, event_data) # TODO think to a possible refactoring for the non-header lines. self._last_date_time = None self._last_event_data = None
Example #15
Source File: skydrivelog.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parse each record structure and return an EventObject if applicable. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in ('header', 'logline'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) if key == 'logline': self._ParseLine(parser_mediator, structure) elif key == 'header': self._ParseHeader(parser_mediator, structure)
Example #16
Source File: mac_wifi.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in self._SUPPORTED_KEYS: raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) self._ParseLogLine(parser_mediator, key, structure)
Example #17
Source File: text_parser.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. This function takes as an input a parsed pyparsing structure and produces an EventObject if possible from that structure. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): tokens from a parsed log line. Returns: EventObject: event or None. """ # pylint: disable=arguments-differ,redundant-returns-doc
Example #18
Source File: sophos_av.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key != 'logline': raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) self._ParseLogLine(parser_mediator, structure)
Example #19
Source File: apt_history.py From plaso with Apache License 2.0 | 6 votes |
def _BuildDateTime(time_elements_structure): """Builds time elements from an APT History time stamp. Args: time_elements_structure (pyparsing.ParseResults): structure of tokens derived from an APT History time stamp. Returns: dfdatetime.TimeElements: date and time extracted from the structure or None f the structure does not represent a valid string. """ try: date_time = dfdatetime_time_elements.TimeElements( time_elements_tuple=time_elements_structure) # APT History logs store date and time values in local time. date_time.is_local_time = True return date_time except ValueError: return None
Example #20
Source File: vsftpd.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key != 'logline': raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) self._ParseLogLine(parser_mediator, structure)
Example #21
Source File: mac_appfirewall.py From plaso with Apache License 2.0 | 6 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure and produces events. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): identifier of the structure of tokens. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Raises: ParseError: when the structure type is unknown. """ if key not in ('logline', 'repeated'): raise errors.ParseError( 'Unable to parse record, unknown structure: {0:s}'.format(key)) self._ParseLogLine(parser_mediator, structure, key)
Example #22
Source File: xchatscrollback.py From plaso with Apache License 2.0 | 5 votes |
def ParseRecord(self, parser_mediator, key, structure): """Parses a log record structure. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. key (str): name of the parsed structure. structure (pyparsing.ParseResults): structure parsed from the log file. """ if key != 'logline': logger.warning( 'Unable to parse record, unknown structure: {0:s}'.format(key)) return timestamp = self._GetValueFromStructure(structure, 'timestamp') try: timestamp = int(timestamp, 10) except (TypeError, ValueError): logger.debug('Invalid timestamp {0!s}, skipping record'.format(timestamp)) return try: text = self._GetValueFromStructure(structure, 'text', default_value='') nickname, text = self._StripThenGetNicknameAndText(text) except pyparsing.ParseException: logger.debug('Error parsing entry at offset {0:d}'.format(self._offset)) return event_data = XChatScrollbackEventData() event_data.nickname = nickname event_data.offset = self._offset event_data.text = text date_time = dfdatetime_posix_time.PosixTime(timestamp=timestamp) event = time_events.DateTimeValuesEvent( date_time, definitions.TIME_DESCRIPTION_ADDED) parser_mediator.ProduceEventWithEventData(event, event_data)
Example #23
Source File: parse_control.py From pybel with MIT License | 5 votes |
def handle_annotation_key(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Handle an annotation key before parsing to validate that it's either enumerated or as a regex. :raise: MissingCitationException or UndefinedAnnotationWarning """ key = tokens['key'] self.raise_for_missing_citation(line, position) self.raise_for_undefined_annotation(line, position, key) return tokens
Example #24
Source File: parse_concept.py From pybel with MIT License | 5 votes |
def handle_identifier_fqualified(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Handle parsing a qualified OBO-style identifier.""" return self._handle_identifier(line, position, tokens, key=IDENTIFIER)
Example #25
Source File: vsftpd.py From plaso with Apache License 2.0 | 5 votes |
def _ParseLogLine(self, parser_mediator, structure): """Parses a log line. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. """ time_elements_tuple = self._GetTimeElementsTuple(structure) try: date_time = dfdatetime_time_elements.TimeElements( time_elements_tuple=time_elements_tuple) date_time.is_local_time = True except ValueError: parser_mediator.ProduceExtractionWarning( 'invalid date time value: {0!s}'.format(time_elements_tuple)) return event_data = VsftpdEventData() event_data.text = self._GetValueFromStructure(structure, 'text') event = time_events.DateTimeValuesEvent( date_time, definitions.TIME_DESCRIPTION_ADDED, time_zone=parser_mediator.timezone) parser_mediator.ProduceEventWithEventData(event, event_data)
Example #26
Source File: parse_concept.py From pybel with MIT License | 5 votes |
def handle_identifier_qualified(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Handle parsing a qualified identifier.""" return self._handle_identifier(line, position, tokens, key=NAME)
Example #27
Source File: parse_concept.py From pybel with MIT License | 5 votes |
def _handle_identifier(self, line: str, position: int, tokens: ParseResults, key) -> ParseResults: """Handle parsing a qualified identifier.""" namespace, name = tokens[NAMESPACE], tokens[key] self.raise_for_missing_namespace(line, position, namespace, name) self.raise_for_missing_name(line, position, namespace, name) return tokens
Example #28
Source File: mac_appfirewall.py From plaso with Apache License 2.0 | 5 votes |
def _GetTimeElementsTuple(self, structure): """Retrieves a time elements tuple from the structure. Args: structure (pyparsing.ParseResults): structure of tokens derived from a line of a text file. Returns: tuple: containing: year (int): year. month (int): month, where 1 represents January. day_of_month (int): day of month, where 1 is the first day of the month. hours (int): hours. minutes (int): minutes. seconds (int): seconds. """ time_elements_tuple = self._GetValueFromStructure(structure, 'date_time') # TODO: what if time_elements_tuple is None. month, day, hours, minutes, seconds = time_elements_tuple # Note that dfdatetime_time_elements.TimeElements will raise ValueError # for an invalid month. month = timelib.MONTH_DICT.get(month.lower(), 0) if month != 0 and month < self._last_month: # Gap detected between years. self._year_use += 1 return (self._year_use, month, day, hours, minutes, seconds)
Example #29
Source File: popcontest.py From plaso with Apache License 2.0 | 5 votes |
def _ParseLogLine(self, parser_mediator, structure): """Extracts events from a log line. Args: parser_mediator (ParserMediator): mediates interactions between parsers and other components, such as storage and dfvfs. structure (pyparsing.ParseResults): structure parsed from the log file. """ # Required fields are <mru> and <atime> and we are not interested in # log lines without <mru>. mru = self._GetValueFromStructure(structure, 'mru') if not mru: return event_data = PopularityContestEventData() event_data.mru = mru event_data.package = self._GetValueFromStructure(structure, 'package') event_data.record_tag = self._GetValueFromStructure(structure, 'tag') # The <atime> field (as <ctime>) is always present but could be 0. # In case of <atime> equal to 0, we are in <NOFILES> case, safely return # without logging. access_time = self._GetValueFromStructure(structure, 'atime') if access_time: # TODO: not doing any check on <tag> fields, even if only informative # probably it could be better to check for the expected values. date_time = dfdatetime_posix_time.PosixTime(timestamp=access_time) event = time_events.DateTimeValuesEvent( date_time, definitions.TIME_DESCRIPTION_LAST_ACCESS) parser_mediator.ProduceEventWithEventData(event, event_data) change_time = self._GetValueFromStructure(structure, 'ctime') if change_time: date_time = dfdatetime_posix_time.PosixTime(timestamp=change_time) event = time_events.DateTimeValuesEvent( date_time, definitions.TIME_DESCRIPTION_ENTRY_MODIFICATION) parser_mediator.ProduceEventWithEventData(event, event_data)
Example #30
Source File: parse_control.py From pybel with MIT License | 5 votes |
def handle_unset_evidence(self, line: str, position: int, tokens: ParseResults) -> ParseResults: """Unset the evidence, or throws an exception if it is not already set. The value for ``tokens[EVIDENCE]`` corresponds to which alternate of SupportingText or Evidence was used in the BEL script. :raises: MissingAnnotationKeyWarning """ if self.evidence is None: raise MissingAnnotationKeyWarning(self.get_line_number(), line, position, tokens[EVIDENCE]) self.evidence = None return tokens