pal_allowedlist.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import fnmatch
  9. import os
  10. from typing import List
  11. DEFAULT_ALLOWEDLIST_FILE = os.path.join(os.path.dirname(__file__), 'pal_allowedlist.txt')
  12. """The path to the default allowed-list file"""
  13. class PALAllowedlist:
  14. """A utility class used for determining if PAL rules should apply to a particular file path. If a path matches the
  15. allowed-list, then PAL rules should not apply to that path."""
  16. def __init__(self, patterns: List[str]) -> None:
  17. """Creates a new instance of :class:`PALAllowedlist` from a list of glob patterns
  18. :param patterns: a list of glob patterns
  19. """
  20. self.patterns: List[str] = patterns
  21. def is_match(self, path: str) -> bool:
  22. """Determines if a path matches the allowed-list
  23. :param path: the path to match
  24. :return: True if the path matches the allowed-list, and False otherwise
  25. """
  26. for pattern in self.patterns:
  27. if fnmatch.fnmatch(path, pattern):
  28. return True
  29. return False
  30. def load() -> PALAllowedlist:
  31. """Returns an instance of :class:`PALAllowedlist` created from the glob patterns in :const:`DEFAULT_ALLOWEDLIST_FILE`"""
  32. with open(DEFAULT_ALLOWEDLIST_FILE) as fh:
  33. return PALAllowedlist(fh.read().splitlines())