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-23 12:12:15 +02:00
|
|
|
ENV_XDG_CONFIG_HOME = 'XDG_CONFIG_HOME'
|
|
|
|
ENV_HTTPIE_CONFIG_DIR = 'HTTPIE_CONFIG_DIR'
|
|
|
|
DEFAULT_CONFIG_DIRNAME = 'httpie'
|
|
|
|
DEFAULT_RELATIVE_XDG_CONFIG_HOME = Path('.config')
|
|
|
|
DEFAULT_RELATIVE_LEGACY_CONFIG_DIR = Path('.httpie')
|
|
|
|
DEFAULT_WINDOWS_CONFIG_DIR = Path(
|
|
|
|
os.path.expandvars('%APPDATA%')) / DEFAULT_CONFIG_DIRNAME
|
|
|
|
|
|
|
|
|
2020-05-21 15:50:00 +02:00
|
|
|
def get_default_config_dir() -> Path:
|
2020-05-23 12:12:15 +02:00
|
|
|
"""
|
|
|
|
Return the path to the httpie configuration directory.
|
2020-05-21 15:50:00 +02:00
|
|
|
|
|
|
|
This directory isn't guaranteed to exist, and nor are any of its
|
2020-05-23 12:12:15 +02:00
|
|
|
ancestors (only the legacy ~/.httpie, if returned, is guaranteed to exist).
|
|
|
|
|
|
|
|
XDG Base Directory Specification support:
|
|
|
|
|
|
|
|
<https://wiki.archlinux.org/index.php/XDG_Base_Directory>
|
|
|
|
|
|
|
|
$XDG_CONFIG_HOME is supported; $XDG_CONFIG_DIRS is not
|
|
|
|
|
2020-05-21 15:50:00 +02:00
|
|
|
"""
|
2020-05-23 12:12:15 +02:00
|
|
|
# 1. explicitly set through env
|
|
|
|
env_config_dir = os.environ.get(ENV_HTTPIE_CONFIG_DIR)
|
2020-05-21 15:50:00 +02:00
|
|
|
if env_config_dir:
|
|
|
|
return Path(env_config_dir)
|
2020-05-23 12:12:15 +02:00
|
|
|
|
|
|
|
# 2. Windows
|
2020-05-21 15:50:00 +02:00
|
|
|
if is_windows:
|
2020-05-23 12:12:15 +02:00
|
|
|
return DEFAULT_WINDOWS_CONFIG_DIR
|
|
|
|
|
|
|
|
home_dir = Path.home()
|
|
|
|
|
|
|
|
# 3. legacy ~/.httpie
|
|
|
|
legacy_config_dir = home_dir / DEFAULT_RELATIVE_LEGACY_CONFIG_DIR
|
|
|
|
if legacy_config_dir.exists():
|
|
|
|
return legacy_config_dir
|
|
|
|
|
|
|
|
# 4. XDG
|
|
|
|
xdg_config_home_dir = os.environ.get(
|
|
|
|
ENV_XDG_CONFIG_HOME, # 4.1. explicit
|
|
|
|
home_dir / DEFAULT_RELATIVE_XDG_CONFIG_HOME # 4.2. default
|
|
|
|
)
|
|
|
|
return Path(xdg_config_home_dir) / DEFAULT_CONFIG_DIRNAME
|
2020-05-21 15:50:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
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']
|