filter.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from rope.base import exceptions
  2. def resources(project, rules):
  3. """Find python files in the `project` matching `rules`
  4. `rules` is a multi-line `str`; each line starts with either a '+'
  5. or '-'. Each '+' means include the file (or its children if it's
  6. a folder) that comes after it. '-' has the same meaning for
  7. exclusion.
  8. """
  9. all = set(project.pycore.get_python_files())
  10. files = None
  11. for line in rules.splitlines():
  12. if not line.strip():
  13. continue
  14. first, path = (line[0], line[1:])
  15. if first not in '+-':
  16. continue
  17. try:
  18. resource = project.get_resource(path.strip())
  19. except exceptions.ResourceNotFoundError:
  20. continue
  21. if resource.is_folder():
  22. matches = set(filter(lambda item: resource.contains(item), all))
  23. else:
  24. matches = set([resource])
  25. if first == '+':
  26. if files is None:
  27. files = set()
  28. files.update(matches)
  29. if first == '-':
  30. if files is None:
  31. files = set(all)
  32. files -= matches
  33. if files is None:
  34. return all
  35. return files