Robot Framework Integrated Development Environment (RIDE)
xmlreaders.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
17 import sys
18 
19 from .. import context, robotapi, utils
20 from ..utils.versioncomparator import cmp_versions
21 from .iteminfo import _XMLKeywordContent
22 
23 
25 
26  def __init__(self, directories=None):
27  self._directories_directories = directories or []
28  self._directories_directories.append(context.LIBRARY_XML_DIRECTORY)
29 
30  def init_from_spec(self, name):
31  specfile = self._find_from_pythonpath_find_from_pythonpath(name) or \
32  self._find_from_library_xml_directories_find_from_library_xml_directories(name)
33  return self._init_from_specfile_init_from_specfile(specfile, name)
34 
36  for directory in self._directories_directories:
37  path = self._find_from_library_xml_directory_find_from_library_xml_directory(directory, name)
38  if path:
39  return path
40  return None
41 
42  def _find_from_library_xml_directory(self, directory, name):
43  current_xml_file = None
44  for xml_file in self._list_xml_files_in_list_xml_files_in(directory):
45  name_from_xml = get_name_from_xml(xml_file)
46  if name_from_xml == name:
47  current_xml_file = self._get_newest_xml_file_get_newest_xml_file(
48  xml_file, current_xml_file)
49  return current_xml_file
50 
51  def _get_newest_xml_file(self, xml_file, current_xml_file):
52  version1 = self._get_version_get_version(xml_file)
53  version2 = self._get_version_get_version(current_xml_file)
54  if cmp_versions(version1, version2) == 1:
55  return xml_file
56  return current_xml_file
57 
58  def _get_version(self, xml_file):
59  try:
60  return utils.ET.parse(xml_file).getroot().find('version').text
61  except:
62  return None
63 
64  def _list_xml_files_in(self, directory):
65  for filename in os.listdir(directory):
66  path = os.path.join(directory, filename)
67  if path.endswith('.xml') and os.path.isfile(path):
68  yield path
69 
70  def _find_from_pythonpath(self, name):
71  return utils.find_from_pythonpath(name + '.xml')
72 
73  def _init_from_specfile(self, specfile, name):
74  if not specfile:
75  return []
76  try:
77  return _parse_xml(specfile, name)
78  except Exception:
79  # TODO: which exception to catch?
80  return []
81 
82 
83 def _parse_xml(file, name):
84  root = utils.ET.parse(file).getroot()
85  if root.tag != 'keywordspec':
86  # TODO: XML validation errors should be logged
87  return [], ''
88  kw_nodes = root.findall('keywords/kw') + root.findall('kw')
89  source_type = root.get('type')
90  doc_format = root.get('format')
91  if source_type == 'resource':
92  source_type += ' file'
93  return [_XMLKeywordContent(node, name, source_type, doc_format) for node in kw_nodes]
94 
95 
96 def get_path(name, basedir):
97  if not _is_library_by_path(name):
98  return name.replace(' ', '')
99  return _resolve_path(name.replace('/', os.sep), basedir)
100 
101 
103  return path.lower().endswith(('.py', '.java', '.class', '/', os.sep))
104 
105 
106 def _resolve_path(path, basedir):
107  for base in [basedir] + sys.path:
108  if not (base and os.path.isdir(base)):
109  continue
110  ret = os.path.join(base, path)
111  if os.path.isfile(ret):
112  return ret
113  if os.path.isdir(ret) and \
114  os.path.isfile(os.path.join(ret, '__init__.py')):
115  return ret
116  raise robotapi.DataError
117 
118 
120  if os.path.exists(name):
121  return name
122  return name.replace(' ', '')
123 
124 
126  try:
127  root = utils.ET.parse(path).getroot()
128  name = root.get('name')
129  return name
130  except:
131  return None
Used when variable does not exist.
Definition: errors.py:67
def _init_from_specfile(self, specfile, name)
Definition: xmlreaders.py:73
def _find_from_library_xml_directories(self, name)
Definition: xmlreaders.py:35
def _find_from_library_xml_directory(self, directory, name)
Definition: xmlreaders.py:42
def _get_newest_xml_file(self, xml_file, current_xml_file)
Definition: xmlreaders.py:51
def __init__(self, directories=None)
Definition: xmlreaders.py:26
def _list_xml_files_in(self, directory)
Definition: xmlreaders.py:64
def _parse_xml(file, name)
Definition: xmlreaders.py:83
def _resolve_path(path, basedir)
Definition: xmlreaders.py:106
def _is_library_by_path(path)
Definition: xmlreaders.py:102
def get_path(name, basedir)
Definition: xmlreaders.py:96
def cmp_versions(version1, version2)