Coverage for src/robotide/preferences/excludes_class.py: 87%
62 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 1ps
17from fnmatch import fnmatch 1ps
20class Excludes(object): 1ps
22 def __init__(self, directory): 1ps
23 self._settings_directory = directory 2p ubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbu v w x y z TbUbVbA B WbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]bC ^bD E F G H I J _b`bK t b c d e f g r {b|b}b~bacbcccdctbecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c
24 self._exclude_file_path = os.path.join(self._settings_directory, 'excludes') 2p ubvbwbxbybzbAbBbCbDbEbFbGbHbIbJbKbLbMbNbObPbQbRbSbu v w x y z TbUbVbA B WbXbYbZb0b1b2b3b4b5b6b7b8b9b!b#b$b%b'b(b)b*b+b,b-b.b/b:b;b=b?b@b[b]bC ^bD E F G H I J _b`bK t b c d e f g r {b|b}b~bacbcccdctbecfcgchcicjckclcmcncocpcqcrcsctcucvcwcxcyczcAcBcCcDcEcFcGcHcIcJcKcLcMcNcOcPcQcRcScTcUcVcWcXcYcZc0c1c2c3c4c5c6c7c8c9c!c#c
26 def get_excludes(self, separator='\n'): 1ps
27 return separator.join(self._get_excludes())
29 def _get_excludes(self): 1ps
30 with self._get_exclude_file('r') as exclude_file: 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o rbsb
31 if not exclude_file: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o rbsb
32 return set()
33 return set(exclude_file.read().split()) 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o rbsb
35 def remove_path(self, path): 1ps
36 path = self._normalize(path) 1qa
37 excludes = self._get_excludes() 1qa
38 self.write_excludes(set([e for e in excludes if e != path])) 1qa
40 def write_excludes(self, excludes): 1ps
41 excludes = [self._normalize(e) for e in excludes] 1qbcdefgrhijaklmno
42 with self._get_exclude_file(read_write='w') as exclude_file: 1qbcdefgrhijaklmno
43 for exclude in excludes: 1qbcdefgrhijaklmno
44 if not exclude: 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true1qbcdefgrhijaklmno
45 continue
46 exclude_file.write("%s\n" % exclude) 1qbcdefgrhijaklmno
47 # print("DEBUG:real excluded self._get_excludes()=%s\n" % self._get_excludes())
49 def update_excludes(self, new_excludes): 1ps
50 excludes = self._get_excludes() 1qbcdefgrhijaklmno
51 self.write_excludes(excludes.union(new_excludes)) 1qbcdefgrhijaklmno
52 # print("DEBUG: Excludes, excluded, union %s, %s, %s\n" % (excludes, new_excludes,
53 # excludes.union(new_excludes)))
55 def _get_exclude_file(self, read_write): 1ps
56 if not os.path.exists(self._exclude_file_path) and read_write.startswith('r'): 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o tbrbsb
57 if not os.path.isdir(self._settings_directory): 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true1Ltbcdefgrhijaklmno
58 os.makedirs(self._settings_directory)
59 return open(self._exclude_file_path, 'w+', encoding='utf-8') 1Ltbcdefgrhijaklmno
60 if os.path.isdir(self._exclude_file_path): 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o tbrbsb
61 raise NameError('"%s" is a directory, not file' % self._exclude_file_path) 2tb
62 try: 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o rbsb
63 return open(self._exclude_file_path, read_write, encoding='utf-8') 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g r h i j a k l m n o rbsb
64 except IOError as e:
65 print(e)
66 raise e # DEBUG: TODO FIXME
68 def contains(self, path, excludes=None): 1ps
69 if not path: 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g h i $cj a k l m n o rbsb
70 return False 2$c
71 excludes = excludes or self._get_excludes() 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g h i j a k l m n o rbsb
72 if len(excludes) < 1: 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t b c d e f g h i j a k l m n o rbsb
73 return False 2p L M N O P Q R S q u v w x y z T U V W X Y Z 0 1 2 3 A B 4 5 6 7 8 9 ! # $ % ' ( ) * + , - . / : ; = ? @ [ ] ^ _ ` { | } C ~ abbbcbdbebfbgbhbibjbkbD E F G H lbI J mbnbobpbqbK t rbsb
74 path = self._normalize(path) 1bcdefghijaklmno
75 excludes = [self._normalize(e) for e in excludes] 1bcdefghijaklmno
76 # print("DEBUG: excludes contains %s path %s\n"
77 # "any: %s\n" % (excludes[0], path, any(self._match(path, e) for e in excludes)) )
78 return any(self._match(path, e) for e in excludes) 1bcdefghijaklmno
80 @staticmethod 1ps
81 def _match(path, e): 1ps
82 return fnmatch(path, e) or path.startswith(e) 1bcdefghijaklmno
84 @staticmethod 1ps
85 def _normalize(path): 1ps
86 if not (path or path.strip()): 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true1qbcdefgrhijaklmno
87 return None
88 path = os.path.normcase(os.path.normpath(path)) 1qbcdefgrhijaklmno
89 ext = os.path.splitext(path)[1] 1qbcdefgrhijaklmno
90 if not ext and not path.endswith(('*', '?', ']')): 1qbcdefgrhijaklmno
91 path += os.sep 1qbcdefgrhijaklmno
92 if '*' in path or '?' in path or ']' in path: 1qbcdefgrhijaklmno
93 path += '*' 1hijk
94 return path 1qbcdefgrhijaklmno