Coverage for src/robotide/namespace/resourcefactory.py: 100%
63 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
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.
16import os 1aS
17from robotide import utils, robotapi 1aS
20class ResourceFactory(object): 1aS
21 _IGNORE_RESOURCE_DIRECTORY_SETTING_NAME = 'ignored resource directory' 1aS
23 def __init__(self, settings): 1aS
24 self.cache = {} 2a lbb c o p d q m n hbmbnbobl pbqbr s t u D E F G ibrb
25 self.python_path_cache = {} 2a lbb c o p d q m n hbmbnbobl pbqbr s t u D E F G ibrb
26 self._excludes = settings.excludes 2a lbb c o p d q m n hbmbnbobl pbqbr s t u D E F G ibrb
27 self.check_path_from_excludes = self._excludes.contains 2a lbb c o p d q m n hbmbnbobl pbqbr s t u D E F G ibrb
28 # print("DEBUG: ResourceFactory init path_excludes %s\n" % self.check_path_from_excludes)
30 @staticmethod 1aS
31 def _with_separator(ddir): 1aS
32 return os.path.abspath(ddir) + os.path.sep 2ib
34 def get_resource(self, directory, name, report_status=True): 1aS
35 path = self._build_path(directory, name) 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
36 res = self._get_resource(path, report_status=report_status) 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
37 if res: 2a 1 2 3 j Z b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
38 return res 2a 1 2 3 Z b c o p d q H I J 4 K L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbR r s t u db
39 path_from_pythonpath = self._get_python_path(name) 1ajbcdHIJKTLMNevfwghxyzABCkDEFGi
40 if path_from_pythonpath: 1ajbcdHIJKTLMNevfwghxyzABCkDEFGi
41 return self._get_resource(path_from_pythonpath, 1avfwghxyzABCF
42 report_status=report_status)
43 return None 1ajbcdHIJKTLMNevfwghxyzABCkDEGi
45 @staticmethod 1aS
46 def _build_path(directory, name): 1aS
47 path = os.path.join(directory, name) if directory else name 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
48 return os.path.abspath(path) 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
50 def get_resource_from_import(self, import_, retriever_context): 1aS
51 resolved_name = retriever_context.vars.replace_variables(import_.name) 2a 1 2 3 b c o p d q H I J 4 K T L M 5 N 6 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C R r s t u D E F G dbi
52 result = self.get_resource(import_.directory, resolved_name) 2a 1 2 3 b c o p d q H I J 4 K T L M 5 N 6 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C R r s t u D E F G dbi
53 # print("""
54 # DEBUG Resource Factory: get_resource_from_import importdir: %s
55 # resolved_name: %s :result: %s
56 # """ % (import_.directory, resolved_name, result))
57 return result 2a 1 2 3 b c o p d q H I J 4 K T L M 5 N 6 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C R r s t u D E F G dbi
59 def new_resource(self, directory, name): 1aS
60 path = os.path.join(directory, name) if directory else name 2jbkbhbU V e W X Y
61 resource = robotapi.ResourceFile(source=path) 2jbkbhbU V e W X Y
62 self.cache[self._normalize(path)] = resource 2jbkbhbU V e W X Y
63 return resource 2jbkbhbU V e W X Y
65 def resource_filename_changed(self, old_name, new_name): 1aS
66 self.cache[self._normalize(new_name)] = self._get_resource(old_name, 1abcopdq
67 report_status=True)
68 del self.cache[self._normalize(old_name)] 1abcopdq
70 def _get_python_path(self, name): 1aS
71 if name not in self.python_path_cache: 1ajbcdHIJKTLMNevfwghxyzABCki
72 path_from_pythonpath = utils.find_from_pythonpath(name) 1ajbcdefghki
73 self.python_path_cache[name] = path_from_pythonpath 1ajbcdefghki
74 return self.python_path_cache[name] 1ajbcdHIJKTLMNevfwghxyzABCki
76 def _get_resource(self, path, report_status): 1aS
77 normalized = self._normalize(path) 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
78 if self.check_path_from_excludes(path) or self.check_path_from_excludes(normalized): 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi
79 return None 1DEFG
80 if normalized not in self.cache: 2a 1 2 3 ebj fbZ b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u dbi
81 try: 2a ebj fbZ b c d m n e f O P g l h Q 0 k R r s t u i
82 self.cache[normalized] = self._load_resource(path, report_status=report_status) 2a ebj fbZ b c d m n e f O P g l h Q 0 k R r s t u i
83 except Exception as e: 1ajbcdefghki
84 # print("DEBUG Resource Factory: exception %s" % str(e))
85 print(e) 1ajbcdefghki
86 self.cache[normalized] = None 1ajbcdefghki
87 return None 1ajbcdefghki
88 return self.cache[normalized] 2a 1 2 3 Z b c o p d q H I J 4 K T L M 5 N 6 m n 7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbR r s t u dbi
90 def _load_resource(self, path, report_status): 1aS
91 r = robotapi.ResourceFile(path) 2a ebj fbZ b c d m n e f O P g l h Q 0 k R i
92 if os.stat(path)[6] != 0 and report_status: 2a ebj fbZ b c d m n e f O P g l h Q 0 k R i
93 return r.populate() 2a ebfbZ f O P g l h Q 0 R
94 robotapi.FromFilePopulator(r).populate(r.source, resource=True) 1amn
95 return r 1amn
97 @staticmethod 1aS
98 def _normalize(path): 1aS
99 return os.path.normcase(os.path.normpath(os.path.abspath(path))) 2a 1 2 3 jbebj fbZ kbb c o p d q H I J 4 K T L M 5 N 6 m n hb7 8 9 ! # $ % ' U V e W X Y ( v f ) w O P * + , - . / : ; = ? @ g [ ] l ^ _ ` { | } ~ abbbh Q x y z cbA B C 0 gbk R r s t u D E F G dbi