# Path: xmanager/xm/pattern_matching.py
# R = TypeVar('R')
# class Case(Generic[R]):
#   def __init__(
#       self,
#       kind: Iterable[Type[Any]],
#       handler: Callable[..., R],
#   ) -> None:
#   def matches(self, *values: Any) -> bool:
# def _deduce_types(handler: Callable[..., Any]) -> Tuple[Type[Any]]:
#   def get_arg_type(arg: str):
# def match(*handlers: Union[Case[R], Callable[..., R]]) -> Callable[..., R]:
#   def apply(*values: Any) -> R:
# def async_match(*handlers: Callable[..., R]) -> Callable[..., Awaitable[R]]:


import abc
import asyncio
import enum
import functools
import os
import shlex
import sys
import attr
from typing import Any, Callable, TypeVar
from absl import flags
from xmanager.xm import pattern_matching



FLAGS = flags.FLAGS
flags.DEFINE_string(
   'xm_launch_script', None, 'Path to the launch script that is using '
   'XManager Launch API')

ReturnType = TypeVar('ReturnType')


class SpecialArg(abc.ABC):
  """A base class for arguments with special handling on serialization."""


@attr.s(auto_attribs=True)
class ShellSafeArg(SpecialArg):
  """Command line argument that shouldn't be escaped.

  Normally all arguments would be passed to the binary as is. To let shell
  substitutions (such as environment variable expansion) to happen the argument
  must be wrapped with this structure.
  """

  arg: str


ARG_ESCAPER = pattern_matching.match(
    pattern_matching.Case([ShellSafeArg], lambda v: v.arg),
    pattern_matching.Case([enum.Enum], lambda v: shlex.quote(str(v.name))),

# Path: xmanager/xm/pattern_matching.py
# R = TypeVar('R')
# class Case(Generic[R]):
#   def __init__(
#       self,
#       kind: Iterable[Type[Any]],
#       handler: Callable[..., R],
#   ) -> None:
#   def matches(self, *values: Any) -> bool:
# def _deduce_types(handler: Callable[..., Any]) -> Tuple[Type[Any]]:
#   def get_arg_type(arg: str):
# def match(*handlers: Union[Case[R], Callable[..., R]]) -> Callable[..., R]:
#   def apply(*values: Any) -> R:
# def async_match(*handlers: Callable[..., R]) -> Callable[..., Awaitable[R]]:


# Path: xmanager/xm/utils.py
import abc
import asyncio
import enum
import functools
import os
import shlex
import sys
import attr
from typing import Any, Callable, TypeVar
from absl import flags
from xmanager.xm import pattern_matching




FLAGS = flags.FLAGS
flags.DEFINE_string(
   'xm_launch_script', None, 'Path to the launch script that is using '
   'XManager Launch API')

ReturnType = TypeVar('ReturnType')


class SpecialArg(abc.ABC):
  """A base class for arguments with special handling on serialization."""


@attr.s(auto_attribs=True)
class ShellSafeArg(SpecialArg):
  """Command line argument that shouldn't be escaped.

  Normally all arguments would be passed to the binary as is. To let shell
  substitutions (such as environment variable expansion) to happen the argument
  must be wrapped with this structure.
  """

  arg: str


ARG_ESCAPER = pattern_matching.match(
    pattern_matching.Case([ShellSafeArg], lambda v: v.arg),
    pattern_matching.Case([enum.Enum], lambda v: shlex.quote(str(v.name))),

    pattern_matching.Case([Any], lambda v: shlex.quote(str(v))),