1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/httpie/context.py

105 lines
3.2 KiB
Python
Raw Normal View History

import sys
from pathlib import Path
2019-08-29 13:39:42 +02:00
from typing import Union, IO, Optional
2016-09-06 12:07:52 +02:00
try:
import curses
except ImportError:
curses = None # Compiled w/o curses
2015-02-24 08:39:26 +02:00
from httpie.compat import is_windows
from httpie.config import DEFAULT_CONFIG_DIR, Config
2019-08-31 18:00:03 +02:00
from httpie.utils import repr_dict
2016-03-04 19:42:13 +02:00
class Environment:
"""
Information about the execution context
(standard streams, config directory, etc).
By default, it represents the actual environment.
All of the attributes can be overwritten though, which
is used by the test suite to simulate various scenarios.
"""
is_windows: bool = is_windows
config_dir: Path = DEFAULT_CONFIG_DIR
2019-08-29 13:39:42 +02:00
stdin: Optional[IO] = sys.stdin # `None` when closed fd (#791)
stdin_isatty: bool = stdin.isatty() if stdin else False
stdin_encoding: str = None
stdout: IO = sys.stdout
stdout_isatty: bool = stdout.isatty()
stdout_encoding: str = None
stderr: IO = sys.stderr
stderr_isatty: bool = stderr.isatty()
colors = 256
if not is_windows:
2016-09-06 12:07:52 +02:00
if curses:
2015-01-23 23:19:02 +02:00
try:
2016-09-06 12:07:52 +02:00
curses.setupterm()
2015-01-23 23:19:02 +02:00
colors = curses.tigetnum('colors')
2016-09-06 12:07:52 +02:00
except curses.error:
pass
else:
# noinspection PyUnresolvedReferences
import colorama.initialise
stdout = colorama.initialise.wrap_stream(
stdout, convert=None, strip=None,
autoreset=True, wrap=True
)
stderr = colorama.initialise.wrap_stream(
stderr, convert=None, strip=None,
autoreset=True, wrap=True
)
del colorama
def __init__(self, **kwargs):
"""
Use keyword arguments to overwrite
any of the class attributes for this instance.
"""
assert all(hasattr(type(self), attr) for attr in kwargs.keys())
self.__dict__.update(**kwargs)
# Keyword arguments > stream.encoding > default utf8
if self.stdin and self.stdin_encoding is None:
self.stdin_encoding = getattr(
self.stdin, 'encoding', None) or 'utf8'
if self.stdout_encoding is None:
actual_stdout = self.stdout
if is_windows:
# noinspection PyUnresolvedReferences
from colorama import AnsiToWin32
if isinstance(self.stdout, AnsiToWin32):
# noinspection PyUnresolvedReferences
actual_stdout = self.stdout.wrapped
self.stdout_encoding = getattr(
actual_stdout, 'encoding', None) or 'utf8'
@property
def config(self) -> Config:
if not hasattr(self, '_config'):
self._config = Config(directory=self.config_dir)
if self._config.is_new():
self._config.save(fail_silently=True)
else:
self._config.load()
return self._config
2016-03-04 19:42:13 +02:00
def __str__(self):
defaults = dict(type(self).__dict__)
actual = dict(defaults)
actual.update(self.__dict__)
actual['config'] = self.config
2019-08-31 18:00:03 +02:00
return repr_dict({
key: value
2016-03-04 19:42:13 +02:00
for key, value in actual.items()
2019-08-31 18:00:03 +02:00
if not key.startswith('_')
})
2016-03-04 19:42:13 +02:00
def __repr__(self):
2019-08-31 15:17:10 +02:00
return f'<{type(self).__name__} {self}>'