Robot Framework Integrated Development Environment (RIDE)
resourcefactory.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 from robotide import utils, robotapi
18 
19 
21 
24  _IGNORE_RESOURCE_DIRECTORY_SETTING_NAME = 'ignored resource directory'
25 
26  def __init__(self, settings):
27  self.cachecache = {}
28  self.python_path_cachepython_path_cache = {}
29  self._excludes_excludes = settings.excludes
30  self.check_path_from_excludescheck_path_from_excludes = self._excludes_excludes.contains
31  # print("DEBUG: ResourceFactory init path_excludes %s\n" % self.check_path_from_excludes)
32 
33  def _with_separator(self, dir):
34  return os.path.abspath(dir) + os.path.sep
35 
36  def get_resource(self, directory, name, report_status=True):
37  path = self._build_path_build_path(directory, name)
38  res = self._get_resource_get_resource(path, report_status=report_status)
39  if res:
40  return res
41  path_from_pythonpath = self._get_python_path_get_python_path(name)
42  if path_from_pythonpath:
43  return self._get_resource_get_resource(path_from_pythonpath,
44  report_status=report_status)
45  return None
46 
47  def _build_path(self, directory, name):
48  path = os.path.join(directory, name) if directory else name
49  return os.path.abspath(path)
50 
51  def get_resource_from_import(self, import_, retriever_context):
52  resolved_name = retriever_context.vars.replace_variables(import_.name)
53  result = self.get_resourceget_resource(import_.directory, resolved_name)
54  # print("""
55  # DEBUG Resource Factory: get_resource_from_import importdir: %s
56  # resolved_name: %s :result: %s
57  # """ % (import_.directory, resolved_name, result))
58  return result
59 
60  def new_resource(self, directory, name):
61  path = os.path.join(directory, name) if directory else name
62  resource = robotapi.ResourceFile(source=path)
63  self.cachecache[self._normalize_normalize(path)] = resource
64  return resource
65 
66  def resource_filename_changed(self, old_name, new_name):
67  self.cachecache[self._normalize_normalize(new_name)] = self._get_resource_get_resource(old_name,
68  report_status=True)
69  del self.cachecache[self._normalize_normalize(old_name)]
70 
71  def _get_python_path(self, name):
72  if name not in self.python_path_cachepython_path_cache:
73  path_from_pythonpath = utils.find_from_pythonpath(name)
74  self.python_path_cachepython_path_cache[name] = path_from_pythonpath
75  return self.python_path_cachepython_path_cache[name]
76 
77  def _get_resource(self, path, report_status):
78  normalized = self._normalize_normalize(path)
79  if self.check_path_from_excludescheck_path_from_excludes(path) or self.check_path_from_excludescheck_path_from_excludes(normalized):
80  return None
81  if normalized not in self.cachecache:
82  try:
83  self.cachecache[normalized] = self._load_resource_load_resource(path, report_status=report_status)
84  except Exception as e:
85  # print("DEBUG Resource Factory: exception %s" % str(e))
86  self.cachecache[normalized] = None
87  return None
88  return self.cachecache[normalized]
89 
90  def _load_resource(self, path, report_status):
91  r = robotapi.ResourceFile(path)
92  if os.stat(path)[6] != 0 and report_status:
93  return r.populate()
94  robotapi.FromFilePopulator(r).populate(r.source, resource=True)
95  return r
96 
97  def _normalize(self, path):
98  return os.path.normcase(os.path.normpath(os.path.abspath(path)))
The parsed resource file object.
Definition: model.py:254
def resource_filename_changed(self, old_name, new_name)
def get_resource(self, directory, name, report_status=True)
def get_resource_from_import(self, import_, retriever_context)