Robot Framework
fileparser.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 import os.path
17 
18 from robot.utils import is_pathlike, is_string
19 
20 from ..lexer import Token
21 from ..model import (File, CommentSection, SettingSection, VariableSection,
22  TestCaseSection, KeywordSection)
23 
24 from .blockparsers import Parser, TestCaseParser, KeywordParser
25 
26 
28 
29  def __init__(self, source=None):
30  super().__init__(File(source=self._get_path_get_path(source)))
31 
32  def _get_path(self, source):
33  if not source:
34  return None
35  if is_string(source) and '\n' not in source and os.path.isfile(source):
36  return source
37  if is_pathlike(source) and source.is_file():
38  return str(source)
39  return None
40 
41  def handles(self, statement):
42  return True
43 
44  def parse(self, statement):
45  parser_class = {
46  Token.SETTING_HEADER: SettingSectionParser,
47  Token.VARIABLE_HEADER: VariableSectionParser,
48  Token.TESTCASE_HEADER: TestCaseSectionParser,
49  Token.TASK_HEADER: TestCaseSectionParser,
50  Token.KEYWORD_HEADER: KeywordSectionParser,
51  Token.COMMENT_HEADER: CommentSectionParser,
52  Token.CONFIG: ImplicitCommentSectionParser,
53  Token.COMMENT: ImplicitCommentSectionParser,
54  Token.ERROR: ImplicitCommentSectionParser,
55  Token.EOL: ImplicitCommentSectionParser
56  }[statement.type]
57  parser = parser_class(statement)
58  self.modelmodel.sections.append(parser.model)
59  return parser
60 
61 
63  model_class = None
64 
65  def __init__(self, header):
66  super().__init__(self.model_classmodel_class(header))
67 
68  def handles(self, statement):
69  return statement.type not in Token.HEADER_TOKENS
70 
71  def parse(self, statement):
72  self.modelmodel.body.append(statement)
73  return None
74 
75 
77  model_class = SettingSection
78 
79 
81  model_class = VariableSection
82 
83 
85  model_class = CommentSection
86 
87 
89 
90  def model_class(self, statement):
91  return CommentSection(body=[statement])
92 
93 
95  model_class = TestCaseSection
96 
97  def parse(self, statement):
98  if statement.type == Token.TESTCASE_NAME:
99  parser = TestCaseParser(statement)
100  self.modelmodel.body.append(parser.model)
101  return parser
102  return SectionParser.parse(self, statement)
103 
104 
106  model_class = KeywordSection
107 
108  def parse(self, statement):
109  if statement.type == Token.KEYWORD_NAME:
110  parser = KeywordParser(statement)
111  self.modelmodel.body.append(parser.model)
112  return parser
113  return SectionParser.parse(self, statement)