Robot Framework
context.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from robot.conf import Languages
17 from robot.utils import normalize_whitespace
18 
19 from .settings import (InitFileSettings, TestCaseFileSettings, ResourceFileSettings,
20  TestCaseSettings, KeywordSettings)
21 from .tokens import Token
22 
23 
25  settings_class = None
26 
27  def __init__(self, settings=None, lang=None):
28  if not settings:
29  self.languageslanguages = lang if isinstance(lang, Languages) else Languages(lang)
30  self.settingssettings = self.settings_classsettings_class(self.languageslanguages)
31  else:
32  self.languageslanguages = settings.languages
33  self.settingssettings = settings
34 
35  def lex_setting(self, statement):
36  self.settingssettings.lex(statement)
37 
38 
40 
41  def __init__(self, settings=None, lang=None):
42  super().__init__(settings, lang)
43 
44  def add_language(self, lang):
45  self.languageslanguages.add_language(lang)
46 
47  def keyword_context(self):
48  return KeywordContext(settings=KeywordSettings(self.languageslanguages))
49 
50  def setting_section(self, statement):
51  return self._handles_section_handles_section(statement, 'Settings')
52 
53  def variable_section(self, statement):
54  return self._handles_section_handles_section(statement, 'Variables')
55 
56  def test_case_section(self, statement):
57  return False
58 
59  def task_section(self, statement):
60  return False
61 
62  def keyword_section(self, statement):
63  return self._handles_section_handles_section(statement, 'Keywords')
64 
65  def comment_section(self, statement):
66  return self._handles_section_handles_section(statement, 'Comments')
67 
68  def lex_invalid_section(self, statement):
69  message, fatal = self._get_invalid_section_error_get_invalid_section_error(statement[0].value)
70  statement[0].set_error(message, fatal)
71  for token in statement[1:]:
72  token.type = Token.COMMENT
73 
74  def _get_invalid_section_error(self, header):
75  raise NotImplementedError
76 
77  def _handles_section(self, statement, header):
78  marker = statement[0].value
79  return (marker[:1] == '*' and
80  self.languageslanguages.headers.get(self._normalize_normalize(marker)) == header)
81 
82  def _normalize(self, marker):
83  return normalize_whitespace(marker).strip('* ').title()
84 
85 
87  settings_class = TestCaseFileSettings
88 
89  def test_case_context(self):
90  return TestCaseContext(settings=TestCaseSettings(self.settingssettings, self.languageslanguages))
91 
92  def test_case_section(self, statement):
93  return self._handles_section_handles_section(statement, 'Test Cases')
94 
95  def task_section(self, statement):
96  return self._handles_section_handles_section(statement, 'Tasks')
97 
98  def _get_invalid_section_error(self, header):
99  return (f"Unrecognized section header '{header}'. Valid sections: "
100  f"'Settings', 'Variables', 'Test Cases', 'Tasks', 'Keywords' "
101  f"and 'Comments'."), False
102 
103 
105  settings_class = ResourceFileSettings
106 
107  def _get_invalid_section_error(self, header):
108  name = self._normalize_normalize(header)
109  if self.languageslanguages.headers.get(name) in ('Test Cases', 'Tasks'):
110  message = f"Resource file with '{name}' section is invalid."
111  fatal = True
112  else:
113  message = (f"Unrecognized section header '{header}'. Valid sections: "
114  f"'Settings', 'Variables', 'Keywords' and 'Comments'.")
115  fatal = False
116  return message, fatal
117 
118 
120  settings_class = InitFileSettings
121 
122  def _get_invalid_section_error(self, header):
123  name = self._normalize_normalize(header)
124  if self.languageslanguages.headers.get(name) in ('Test Cases', 'Tasks'):
125  message = f"'{name}' section is not allowed in suite initialization file."
126  else:
127  message = (f"Unrecognized section header '{header}'. Valid sections: "
128  f"'Settings', 'Variables', 'Keywords' and 'Comments'.")
129  return message, False
130 
131 
133 
134  @property
135  template_set = property
136 
137  def template_set(self):
138  return self.settingssettings.template_set
139 
140 
142 
143  @property
144  template_set = property
145 
146  def template_set(self):
147  return False
Keeps a list of languages and unifies the translations in the properties.
Definition: languages.py:34
def __init__(self, settings=None, lang=None)
Definition: context.py:41
def variable_section(self, statement)
Definition: context.py:53
def task_section(self, statement)
Definition: context.py:59
def test_case_section(self, statement)
Definition: context.py:56
def comment_section(self, statement)
Definition: context.py:65
def _handles_section(self, statement, header)
Definition: context.py:77
def _get_invalid_section_error(self, header)
Definition: context.py:74
def keyword_section(self, statement)
Definition: context.py:62
def setting_section(self, statement)
Definition: context.py:50
def lex_invalid_section(self, statement)
Definition: context.py:68
def _get_invalid_section_error(self, header)
Definition: context.py:122
def lex_setting(self, statement)
Definition: context.py:35
def __init__(self, settings=None, lang=None)
Definition: context.py:27
def normalize_whitespace(string)
Definition: normalizing.py:45