2012-09-17 00:37:36 +02:00
|
|
|
import errno
|
2019-08-30 11:32:14 +02:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Union
|
2012-09-17 00:37:36 +02:00
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
from httpie import __version__
|
|
|
|
from httpie.compat import is_windows
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
2020-05-21 15:50:00 +02:00
|
|
|
def get_default_config_dir() -> Path:
|
|
|
|
"""Return the path to the httpie configuration directory.
|
|
|
|
|
|
|
|
This directory isn't guaranteed to exist, and nor are any of its
|
|
|
|
ancestors.
|
|
|
|
"""
|
|
|
|
env_config_dir = os.environ.get('HTTPIE_CONFIG_DIR')
|
|
|
|
if env_config_dir:
|
|
|
|
return Path(env_config_dir)
|
|
|
|
if is_windows:
|
|
|
|
return Path(os.path.expandvars(r'%APPDATA%\httpie'))
|
|
|
|
legacy_config_dir = os.path.expanduser('~/.httpie')
|
|
|
|
if os.path.exists(legacy_config_dir):
|
|
|
|
return Path(legacy_config_dir)
|
|
|
|
xdg_config_dir = os.environ.get('XDG_CONFIG_HOME')
|
|
|
|
if not xdg_config_dir or not os.path.isabs(xdg_config_dir):
|
|
|
|
xdg_config_dir = os.path.expanduser('~/.config')
|
|
|
|
httpie_config_dir = os.path.join(xdg_config_dir, 'httpie')
|
|
|
|
return Path(httpie_config_dir)
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_CONFIG_DIR = get_default_config_dir()
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
|
2019-12-02 17:43:16 +01:00
|
|
|
class ConfigFileError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
class BaseConfigDict(dict):
|
|
|
|
name = None
|
2013-05-13 14:47:44 +02:00
|
|
|
helpurl = None
|
2012-12-01 18:16:00 +01:00
|
|
|
about = None
|
2012-09-17 00:37:36 +02:00
|
|
|
|
2019-12-02 17:43:16 +01:00
|
|
|
def __init__(self, path: Path):
|
|
|
|
super().__init__()
|
|
|
|
self.path = path
|
2013-05-13 14:47:44 +02:00
|
|
|
|
2019-12-02 17:43:16 +01:00
|
|
|
def ensure_directory(self):
|
2012-09-17 00:37:36 +02:00
|
|
|
try:
|
2019-12-02 17:43:16 +01:00
|
|
|
self.path.parent.mkdir(mode=0o700, parents=True)
|
2012-09-17 00:37:36 +02:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2019-08-30 11:32:14 +02:00
|
|
|
def is_new(self) -> bool:
|
2019-12-02 17:43:16 +01:00
|
|
|
return not self.path.exists()
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
def load(self):
|
2019-12-02 17:43:16 +01:00
|
|
|
config_type = type(self).__name__.lower()
|
2012-09-17 00:37:36 +02:00
|
|
|
try:
|
2019-12-02 17:43:16 +01:00
|
|
|
with self.path.open('rt') as f:
|
2012-09-17 00:37:36 +02:00
|
|
|
try:
|
|
|
|
data = json.load(f)
|
|
|
|
except ValueError as e:
|
2019-12-02 17:43:16 +01:00
|
|
|
raise ConfigFileError(
|
|
|
|
f'invalid {config_type} file: {e} [{self.path}]'
|
2012-09-17 00:37:36 +02:00
|
|
|
)
|
|
|
|
self.update(data)
|
|
|
|
except IOError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
2019-12-02 17:43:16 +01:00
|
|
|
raise ConfigFileError(f'cannot read {config_type} file: {e}')
|
2012-09-17 00:37:36 +02:00
|
|
|
|
2019-08-29 14:05:00 +02:00
|
|
|
def save(self, fail_silently=False):
|
2012-12-01 18:16:00 +01:00
|
|
|
self['__meta__'] = {
|
|
|
|
'httpie': __version__
|
|
|
|
}
|
2013-05-13 14:47:44 +02:00
|
|
|
if self.helpurl:
|
|
|
|
self['__meta__']['help'] = self.helpurl
|
2012-12-01 18:16:00 +01:00
|
|
|
|
|
|
|
if self.about:
|
|
|
|
self['__meta__']['about'] = self.about
|
|
|
|
|
2019-12-02 17:43:16 +01:00
|
|
|
self.ensure_directory()
|
|
|
|
|
2019-08-29 14:05:00 +02:00
|
|
|
try:
|
2019-12-02 17:43:16 +01:00
|
|
|
with self.path.open('w') as f:
|
|
|
|
json.dump(
|
|
|
|
obj=self,
|
|
|
|
fp=f,
|
|
|
|
indent=4,
|
|
|
|
sort_keys=True,
|
|
|
|
ensure_ascii=True,
|
|
|
|
)
|
2019-08-29 14:05:00 +02:00
|
|
|
f.write('\n')
|
|
|
|
except IOError:
|
|
|
|
if not fail_silently:
|
|
|
|
raise
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
try:
|
2019-12-02 17:43:16 +01:00
|
|
|
self.path.unlink()
|
2012-09-17 00:37:36 +02:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
class Config(BaseConfigDict):
|
2019-12-02 17:43:16 +01:00
|
|
|
FILENAME = 'config.json'
|
2012-09-17 00:37:36 +02:00
|
|
|
DEFAULTS = {
|
2012-09-17 02:15:00 +02:00
|
|
|
'default_options': []
|
2012-09-17 00:37:36 +02:00
|
|
|
}
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2019-08-30 11:32:14 +02:00
|
|
|
def __init__(self, directory: Union[str, Path] = DEFAULT_CONFIG_DIR):
|
|
|
|
self.directory = Path(directory)
|
2019-12-02 17:43:16 +01:00
|
|
|
super().__init__(path=self.directory / self.FILENAME)
|
|
|
|
self.update(self.DEFAULTS)
|
2019-08-30 11:32:14 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def default_options(self) -> list:
|
|
|
|
return self['default_options']
|