PageRenderTime 40ms CodeModel.GetById 4ms RepoModel.GetById 4ms app.codeStats 0ms

/src/robot/parsing/datarow.py

https://code.google.com/p/robotframework/
Python | 107 lines | 86 code | 7 blank | 14 comment | 7 complexity | 9296a54635a0f59222b570a9f0aeb64e 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. class DataRow(object):
  16. _row_continuation_marker = '...'
  17. _whitespace_regexp = re.compile('\s+')
  18. _ye_olde_metadata_prefix = 'meta:'
  19. def __init__(self, cells):
  20. self.cells, self.comments = self._parse(cells)
  21. def _parse(self, row):
  22. data = []
  23. comments = []
  24. for cell in row:
  25. cell = self._collapse_whitespace(cell)
  26. if cell.startswith('#') or comments:
  27. comments.append(cell)
  28. else:
  29. data.append(cell)
  30. return self._purge_empty_cells(data), self._purge_empty_cells(comments)
  31. def _collapse_whitespace(self, cell):
  32. return self._whitespace_regexp.sub(' ', cell).strip()
  33. def _purge_empty_cells(self, row):
  34. while row and not row[-1]:
  35. row.pop()
  36. # Cells with only a single backslash are considered empty
  37. return [cell if cell != '\\' else '' for cell in row]
  38. @property
  39. def head(self):
  40. return self.cells[0] if self.cells else ''
  41. @property
  42. def tail(self):
  43. return self.cells[1:]
  44. @property
  45. def all(self):
  46. return self.cells
  47. @property
  48. def data(self):
  49. if self.is_continuing():
  50. index = self.cells.index(self._row_continuation_marker) + 1
  51. return self.cells[index:]
  52. return self.cells
  53. def dedent(self):
  54. datarow = DataRow([])
  55. datarow.cells = self.tail
  56. datarow.comments = self.comments
  57. return datarow
  58. def handle_old_style_metadata(self):
  59. if self._is_metadata_with_olde_prefix(self.head):
  60. self.cells = self._convert_to_new_style_metadata()
  61. def _is_metadata_with_olde_prefix(self, value):
  62. return value.lower().startswith(self._ye_olde_metadata_prefix)
  63. def _convert_to_new_style_metadata(self):
  64. return ['Metadata'] + [self.head.split(':', 1)[1].strip()] + self.tail
  65. def starts_for_loop(self):
  66. if self.head and self.head.startswith(':'):
  67. return self.head.replace(':', '').replace(' ', '').upper() == 'FOR'
  68. return False
  69. def starts_test_or_user_keyword_setting(self):
  70. head = self.head
  71. return head and head[0] == '[' and head[-1] == ']'
  72. def test_or_user_keyword_setting_name(self):
  73. return self.head[1:-1].strip()
  74. def is_indented(self):
  75. return self.head == ''
  76. def is_continuing(self):
  77. for cell in self.cells:
  78. if cell == self._row_continuation_marker:
  79. return True
  80. if cell:
  81. return False
  82. def is_commented(self):
  83. return bool(not self.cells and self.comments)
  84. def __nonzero__(self):
  85. return bool(self.cells or self.comments)