2012-08-17 23:23:02 +02:00
|
|
|
import os
|
2012-09-17 00:37:36 +02:00
|
|
|
import json
|
|
|
|
import errno
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-01-02 18:07:00 +01:00
|
|
|
DEFAULT_CONFIG_DIR = str(os.environ.get(
|
2012-09-17 02:12:16 +02:00
|
|
|
'HTTPIE_CONFIG_DIR',
|
|
|
|
os.path.expanduser('~/.httpie') if not is_windows else
|
|
|
|
os.path.expandvars(r'%APPDATA%\\httpie')
|
2016-01-02 18:07:00 +01:00
|
|
|
))
|
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
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return self[item]
|
|
|
|
|
2013-05-13 14:47:44 +02:00
|
|
|
def _get_path(self):
|
|
|
|
"""Return the config file path without side-effects."""
|
2014-04-27 23:12:48 +02:00
|
|
|
raise NotImplementedError()
|
2013-05-13 14:47:44 +02:00
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
@property
|
|
|
|
def path(self):
|
2013-05-13 14:47:44 +02:00
|
|
|
"""Return the config file path creating basedir, if needed."""
|
|
|
|
path = self._get_path()
|
2012-09-17 00:37:36 +02:00
|
|
|
try:
|
2013-05-13 14:47:44 +02:00
|
|
|
os.makedirs(os.path.dirname(path), mode=0o700)
|
2012-09-17 00:37:36 +02:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2013-05-13 14:47:44 +02:00
|
|
|
return path
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
def is_new(self):
|
2013-05-13 14:47:44 +02:00
|
|
|
return not os.path.exists(self._get_path())
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
try:
|
|
|
|
with open(self.path, 'rt') as f:
|
|
|
|
try:
|
|
|
|
data = json.load(f)
|
|
|
|
except ValueError as e:
|
|
|
|
raise ValueError(
|
|
|
|
'Invalid %s JSON: %s [%s]' %
|
2014-10-20 14:40:55 +02:00
|
|
|
(type(self).__name__, str(e), self.path)
|
2012-09-17 00:37:36 +02:00
|
|
|
)
|
|
|
|
self.update(data)
|
|
|
|
except IOError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
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-08-29 14:05:00 +02:00
|
|
|
try:
|
|
|
|
with open(self.path, 'w') as f:
|
|
|
|
json.dump(self, f, indent=4, sort_keys=True, ensure_ascii=True)
|
|
|
|
f.write('\n')
|
|
|
|
except IOError:
|
|
|
|
if not fail_silently:
|
|
|
|
raise
|
2012-09-17 00:37:36 +02:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
try:
|
|
|
|
os.unlink(self.path)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
class Config(BaseConfigDict):
|
|
|
|
|
|
|
|
name = 'config'
|
2018-09-07 19:10:04 +02:00
|
|
|
helpurl = 'https://httpie.org/doc#config'
|
2012-12-01 18:16:00 +01:00
|
|
|
about = 'HTTPie configuration file'
|
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
|
|
|
|
2014-04-27 23:12:48 +02:00
|
|
|
def __init__(self, directory=DEFAULT_CONFIG_DIR):
|
|
|
|
super(Config, self).__init__()
|
2012-09-17 00:37:36 +02:00
|
|
|
self.update(self.DEFAULTS)
|
2014-04-27 23:12:48 +02:00
|
|
|
self.directory = directory
|
|
|
|
|
|
|
|
def _get_path(self):
|
|
|
|
return os.path.join(self.directory, self.name + '.json')
|