def find_references(self): """Get the array of references. Check each references before returning. :raises: :class:`~doorstop.common.DoorstopError` when no reference is found :return: Array of tuples: ( relative path to file or None (when no reference set), line number (when found in file) or None (when found as filename) or None (when no reference set) ) """ if not self.references: log.debug("no external reference to search for") return [] if not settings.CACHE_PATHS: pyficache.clear_file_cache() references = [] for ref_item in self.references: path = ref_item["path"] keyword = ref_item["keyword"] if "keyword" in ref_item else None reference = self.reference_finder.find_file_reference( path, self.root, self.tree, path, keyword) references.append(reference) return references
def find_ref(self): """Get the external file reference and line number. :raises: :class:`~doorstop.common.DoorstopError` when no reference is found :return: relative path to file or None (when no reference set), line number (when found in file) or None (when found as filename) or None (when no reference set) """ # Return immediately if no external reference if not self.ref: log.debug("no external reference to search for") return None, None # Update the cache if not settings.CACHE_PATHS: pyficache.clear_file_cache() # Search for the external reference log.debug("seraching for ref '{}'...".format(self.ref)) pattern = r"(\b|\W){}(\b|\W)".format(re.escape(self.ref)) log.trace("regex: {}".format(pattern)) regex = re.compile(pattern) for path, filename, relpath in self.tree.vcs.paths: # Skip the item's file while searching if path == self.path: continue # Check for a matching filename if filename == self.ref: return relpath, None # Skip extensions that should not be considered text if os.path.splitext(filename)[-1] in settings.SKIP_EXTS: continue # Search for the reference in the file lines = pyficache.getlines(path) if lines is None: log.trace("unable to read lines from: {}".format(path)) continue for lineno, line in enumerate(lines, start=1): if regex.search(line): log.debug("found ref: {}".format(relpath)) return relpath, lineno msg = "external reference not found: {}".format(self.ref) raise DoorstopError(msg)
def find_ref(self): """Get the external file reference and line number. :raises: :class:`~doorstop.common.DoorstopError` when no reference is found :return: relative path to file or None (when no reference set), line number (when found in file) or None (when found as filename) or None (when no reference set) """ # Return immediately if no external reference if not self.ref: log.debug("no external reference to search for") return None, None # Update the cache if not settings.CACHE_PATHS: pyficache.clear_file_cache() # Search for the external reference return self.reference_finder.find_ref(self.ref, self.tree, self.path)
def test_clear_file_cache(self): pyficache.update_cache(__file__) pyficache.clear_file_format_cache() pyficache.clear_file_cache() self.assertEqual([], pyficache.cached_files()) return
def setUp(self): pyficache.clear_file_cache() return