PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/robot/output/stdoutlogsplitter.py

https://code.google.com/p/robotframework/
Python | 57 lines | 32 code | 11 blank | 14 comment | 4 complexity | 6e8651ec359dfd3fd7102f33ef8c1bb1 MD5 | raw file
Possible License(s): Apache-2.0
  1. # Copyright 2008-2014 Nokia Solutions and Networks
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. from robot.utils import format_time
  16. from .loggerhelper import Message, LEVELS
  17. class StdoutLogSplitter(object):
  18. """Splits messages logged through stdout (or stderr) into Message objects"""
  19. _split_from_levels = re.compile('^(?:\*'
  20. '(%s|HTML)' # Level
  21. '(:\d+(?:\.\d+)?)?' # Optional timestamp
  22. '\*)' % '|'.join(LEVELS), re.MULTILINE)
  23. def __init__(self, output):
  24. self._messages = list(self._get_messages(output.strip()))
  25. def _get_messages(self, output):
  26. for level, timestamp, msg in self._split_output(output):
  27. if timestamp:
  28. timestamp = self._format_timestamp(timestamp[1:])
  29. yield Message(msg.strip(), level, timestamp=timestamp)
  30. def _split_output(self, output):
  31. tokens = self._split_from_levels.split(output)
  32. tokens = self._add_initial_level_and_time_if_needed(tokens)
  33. for i in xrange(0, len(tokens), 3):
  34. yield tokens[i:i+3]
  35. def _add_initial_level_and_time_if_needed(self, tokens):
  36. if self._output_started_with_level(tokens):
  37. return tokens[1:]
  38. return ['INFO', None] + tokens
  39. def _output_started_with_level(self, tokens):
  40. return tokens[0] == ''
  41. def _format_timestamp(self, millis):
  42. return format_time(float(millis)/1000, millissep='.')
  43. def __iter__(self):
  44. return iter(self._messages)