2014-04-27 00:07:13 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from requests.compat import is_windows
|
|
|
|
|
|
|
|
from httpie.config import DEFAULT_CONFIG_DIR, Config
|
|
|
|
|
|
|
|
|
|
|
|
class Environment(object):
|
|
|
|
"""
|
|
|
|
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 = is_windows
|
|
|
|
config_dir = DEFAULT_CONFIG_DIR
|
|
|
|
stdin = sys.stdin
|
|
|
|
stdin_isatty = stdin.isatty()
|
|
|
|
stdin_encoding = None
|
|
|
|
stdout = sys.stdout
|
|
|
|
stdout_isatty = stdout.isatty()
|
|
|
|
stdout_encoding = None
|
|
|
|
stderr = sys.stderr
|
|
|
|
stderr_isatty = stderr.isatty()
|
2014-10-20 14:40:55 +02:00
|
|
|
colors = 256
|
2014-09-08 07:46:53 +02:00
|
|
|
if not is_windows:
|
|
|
|
import curses
|
2014-10-20 14:40:55 +02:00
|
|
|
try:
|
|
|
|
curses.setupterm()
|
2015-01-23 22:19:02 +01:00
|
|
|
try:
|
|
|
|
colors = curses.tigetnum('colors')
|
|
|
|
except TypeError:
|
|
|
|
# pypy3 (2.4.0)
|
|
|
|
colors = curses.tigetnum(b'colors')
|
2014-10-20 14:40:55 +02:00
|
|
|
except curses.error:
|
|
|
|
pass
|
2014-09-08 07:46:53 +02:00
|
|
|
del curses
|
|
|
|
else:
|
2014-04-27 00:07:13 +02:00
|
|
|
# noinspection PyUnresolvedReferences
|
2014-09-08 07:46:53 +02:00
|
|
|
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
|
2014-04-27 00:07:13 +02:00
|
|
|
|
|
|
|
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_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:
|
2014-09-08 07:46:53 +02:00
|
|
|
# noinspection PyUnresolvedReferences
|
2014-04-27 00:07:13 +02:00
|
|
|
from colorama import AnsiToWin32
|
|
|
|
if isinstance(self.stdout, AnsiToWin32):
|
|
|
|
actual_stdout = self.stdout.wrapped
|
|
|
|
self.stdout_encoding = getattr(
|
|
|
|
actual_stdout, 'encoding', None) or 'utf8'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
if not hasattr(self, '_config'):
|
|
|
|
self._config = Config(directory=self.config_dir)
|
|
|
|
if self._config.is_new():
|
|
|
|
self._config.save()
|
|
|
|
else:
|
|
|
|
self._config.load()
|
|
|
|
return self._config
|