MCPcopy Index your code
hub / github.com/httpie/cli / LazyChoices

Class LazyChoices

httpie/cli/utils.py:28–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26
27
28class LazyChoices(argparse.Action, Generic[T]):
29 def __init__(
30 self,
31 *args,
32 getter: Callable[[], Iterable[T]],
33 help_formatter: Optional[Callable[[T, bool], str]] = None,
34 sort: bool = False,
35 cache: bool = True,
36 isolation_mode: bool = False,
37 **kwargs
38 ) -> None:
39 self.getter = getter
40 self.help_formatter = help_formatter
41 self.sort = sort
42 self.cache = cache
43 self.isolation_mode = isolation_mode
44 self._help: Optional[str] = None
45 self._obj: Optional[Iterable[T]] = None
46 super().__init__(*args, **kwargs)
47 self.choices = self
48
49 def load(self) -> T:
50 if self._obj is None or not self.cache:
51 self._obj = self.getter()
52
53 assert self._obj is not None
54 return self._obj
55
56 @property
57 def help(self) -> str:
58 if self._help is None and self.help_formatter is not None:
59 self._help = self.help_formatter(
60 self.load(),
61 isolation_mode=self.isolation_mode
62 )
63 return self._help
64
65 @help.setter
66 def help(self, value: Any) -> None:
67 self._help = value
68
69 def __contains__(self, item: Any) -> bool:
70 return item in self.load()
71
72 def __iter__(self) -> Iterator[T]:
73 if self.sort:
74 return iter(sorted(self.load()))
75 else:
76 return iter(self.load())
77
78 def __call__(self, parser, namespace, values, option_string=None):
79 setattr(namespace, self.dest, values)

Callers 1

serializeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected