| 62 | |
| 63 | |
| 64 | class LazyPager: |
| 65 | # Spin up a new process only in case it has been called or its stdin |
| 66 | # has been called |
| 67 | def __init__(self, popen, **kwargs): |
| 68 | self._popen = popen |
| 69 | self._popen_kwargs = kwargs |
| 70 | self._process = None |
| 71 | self.stdin = LazyStdin(self) |
| 72 | |
| 73 | def initialize(self): |
| 74 | if self._process is None: |
| 75 | self._process = self._do_popen() |
| 76 | return self._process |
| 77 | |
| 78 | def __getattr__(self, item): |
| 79 | return getattr(self.initialize(), item) |
| 80 | |
| 81 | def communicate(self, *args, **kwargs): |
| 82 | # if pager process has not been created yet it means we didn't |
| 83 | # write to its stdin and there is no reason to create it just |
| 84 | # to call `communicate` so we can ignore this call |
| 85 | if self._process is not None or args or kwargs: |
| 86 | return getattr(self.initialize(), 'communicate')(*args, **kwargs) |
| 87 | return None, None |
| 88 | |
| 89 | def _do_popen(self): |
| 90 | try: |
| 91 | return self._popen(**self._popen_kwargs) |
| 92 | except FileNotFoundError as e: |
| 93 | raise PagerInitializationException(e) |
| 94 | |
| 95 | |
| 96 | class IMDSRegionProvider(BaseProvider): |
no outgoing calls