activator.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from __future__ import annotations
  2. import os
  3. from abc import ABC, abstractmethod
  4. from typing import TYPE_CHECKING
  5. if TYPE_CHECKING:
  6. from argparse import ArgumentParser
  7. from pathlib import Path
  8. from python_discovery import PythonInfo
  9. from virtualenv.config.cli.parser import VirtualEnvOptions
  10. from virtualenv.create.creator import Creator
  11. class Activator(ABC):
  12. """Generates activate script for the virtual environment."""
  13. def __init__(self, options: VirtualEnvOptions) -> None:
  14. """Create a new activator generator.
  15. :param options: the parsed options as defined within :meth:`add_parser_arguments`
  16. """
  17. self.flag_prompt = os.path.basename(os.getcwd()) if options.prompt == "." else options.prompt
  18. @classmethod
  19. def supports(cls, interpreter: PythonInfo) -> bool: # noqa: ARG003
  20. """Check if the activation script is supported in the given interpreter.
  21. :param interpreter: the interpreter we need to support
  22. :returns: ``True`` if supported, ``False`` otherwise
  23. """
  24. return True
  25. @classmethod # noqa: B027
  26. def add_parser_arguments(cls, parser: ArgumentParser, interpreter: PythonInfo) -> None:
  27. """Add CLI arguments for this activation script.
  28. :param parser: the CLI parser
  29. :param interpreter: the interpreter this virtual environment is based of
  30. """
  31. @abstractmethod
  32. def generate(self, creator: Creator) -> list[Path]:
  33. """Generate activate script for the given creator.
  34. :param creator: the creator (based of :class:`virtualenv.create.creator.Creator`) we used to create this virtual
  35. environment
  36. """
  37. raise NotImplementedError
  38. __all__ = [
  39. "Activator",
  40. ]