Robot Framework Integrated Development Environment (RIDE)
excludes_class.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 fnmatch import fnmatch
18 
19 
20 class Excludes():
21 
22  def __init__(self, directory):
23  self._settings_directory_settings_directory = directory
24  self._exclude_file_path_exclude_file_path = os.path.join(self._settings_directory_settings_directory, 'excludes')
25 
26  def get_excludes(self, separator='\n'):
27  return separator.join(self._get_excludes_get_excludes())
28 
29  def _get_excludes(self):
30  with self._get_exclude_file_get_exclude_file('r') as exclude_file:
31  if not exclude_file:
32  return set()
33  return set(exclude_file.read().split())
34 
35  def remove_path(self, path):
36  path = self._normalize_normalize(path)
37  excludes = self._get_excludes_get_excludes()
38  self.write_excludeswrite_excludes(set([e for e in excludes if e != path]))
39 
40  def write_excludes(self, excludes):
41  excludes = [self._normalize_normalize(e) for e in excludes]
42  with self._get_exclude_file_get_exclude_file(read_write='w') as exclude_file:
43  for exclude in excludes:
44  if not exclude:
45  continue
46  exclude_file.write("%s\n" % exclude)
47  # print("DEBUG:real excluded self._get_excludes()=%s\n" % self._get_excludes())
48 
49  def update_excludes(self, new_excludes):
50  excludes = self._get_excludes_get_excludes()
51  self.write_excludeswrite_excludes(excludes.union(new_excludes))
52  # print("DEBUG: Excludes, excluded, union %s, %s, %s\n" % (excludes, new_excludes,
53  # excludes.union(new_excludes)))
54 
55  def _get_exclude_file(self, read_write):
56  if not os.path.exists(self._exclude_file_path_exclude_file_path) and read_write.startswith('r'):
57  if not os.path.isdir(self._settings_directory_settings_directory):
58  os.makedirs(self._settings_directory_settings_directory)
59  return open(self._exclude_file_path_exclude_file_path, 'w+')
60  if os.path.isdir(self._exclude_file_path_exclude_file_path):
61  raise NameError('"%s" is a directory, not file' % self._exclude_file_path_exclude_file_path)
62  try:
63  return open(self._exclude_file_path_exclude_file_path, read_write)
64  except IOError as e:
65  raise e # TODO FIXME
66 
67  def contains(self, path, excludes=None):
68  if not path:
69  return False
70  excludes = excludes or self._get_excludes_get_excludes()
71  if len(excludes) < 1:
72  return False
73  path = self._normalize_normalize(path)
74  excludes = [self._normalize_normalize(e) for e in excludes]
75  # print("DEBUG: excludes contains %s path %s\n"
76  # "any: %s\n" % (excludes[0], path, any(self._match(path, e) for e in excludes)) )
77  return any(self._match_match(path, e) for e in excludes)
78 
79  @staticmethod
80  def _match(path, e):
81  return fnmatch(path, e) or path.startswith(e)
82 
83  @staticmethod
84  def _normalize(path):
85  if not (path or path.strip()):
86  return None
87  path = os.path.normcase(os.path.normpath(path))
88  ext = os.path.splitext(path)[1]
89  if not ext and not path.endswith(('*', '?', ']')):
90  path += os.sep
91  if '*' in path or '?' in path or ']' in path:
92  path += '*'
93  return path
def contains(self, path, excludes=None)