Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

format_control.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from pip._vendor.packaging.utils import canonicalize_name
  2. class FormatControl(object):
  3. """A helper class for controlling formats from which packages are installed.
  4. If a field is falsy, it isn't set. If it is {':all:'}, it should match all
  5. packages except those listed in the other field. Only one field can be set
  6. to {':all:'} at a time. The rest of the time exact package name matches
  7. are listed, with any given package only showing up in one field at a time.
  8. """
  9. def __init__(self, no_binary=None, only_binary=None):
  10. self.no_binary = set() if no_binary is None else no_binary
  11. self.only_binary = set() if only_binary is None else only_binary
  12. def __eq__(self, other):
  13. return self.__dict__ == other.__dict__
  14. def __ne__(self, other):
  15. return not self.__eq__(other)
  16. def __repr__(self):
  17. return "{}({}, {})".format(
  18. self.__class__.__name__,
  19. self.no_binary,
  20. self.only_binary
  21. )
  22. @staticmethod
  23. def handle_mutual_excludes(value, target, other):
  24. new = value.split(',')
  25. while ':all:' in new:
  26. other.clear()
  27. target.clear()
  28. target.add(':all:')
  29. del new[:new.index(':all:') + 1]
  30. # Without a none, we want to discard everything as :all: covers it
  31. if ':none:' not in new:
  32. return
  33. for name in new:
  34. if name == ':none:':
  35. target.clear()
  36. continue
  37. name = canonicalize_name(name)
  38. other.discard(name)
  39. target.add(name)
  40. def get_allowed_formats(self, canonical_name):
  41. result = {"binary", "source"}
  42. if canonical_name in self.only_binary:
  43. result.discard('source')
  44. elif canonical_name in self.no_binary:
  45. result.discard('binary')
  46. elif ':all:' in self.only_binary:
  47. result.discard('source')
  48. elif ':all:' in self.no_binary:
  49. result.discard('binary')
  50. return frozenset(result)
  51. def disallow_binaries(self):
  52. self.handle_mutual_excludes(
  53. ':all:', self.no_binary, self.only_binary,
  54. )