You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Added configuration file.
The "default_content_type" option can be set to "form". Closes #91.
This commit is contained in:
@@ -1,8 +1,78 @@
|
||||
import os
|
||||
import json
|
||||
import errno
|
||||
|
||||
from . import __version__
|
||||
from requests.compat import is_windows
|
||||
|
||||
__author__ = 'jakub'
|
||||
|
||||
DEFAULT_CONFIG_DIR = (os.path.expanduser('~/.httpie')
|
||||
if not is_windows else
|
||||
os.path.expandvars(r'%APPDATA%\\httpie'))
|
||||
|
||||
|
||||
CONFIG_DIR = (os.path.expanduser('~/.httpie') if not is_windows else
|
||||
os.path.expandvars(r'%APPDATA%\\httpie'))
|
||||
class BaseConfigDict(dict):
|
||||
|
||||
name = None
|
||||
help = None
|
||||
|
||||
def __init__(self, directory=DEFAULT_CONFIG_DIR, seq=None, **kwargs):
|
||||
super(BaseConfigDict, self).__init__(seq or [], **kwargs)
|
||||
self.directory = directory
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self[item]
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
try:
|
||||
os.makedirs(self.directory, mode=0o700)
|
||||
except OSError as e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
return os.path.join(self.directory, self.name + '.json')
|
||||
|
||||
@property
|
||||
def is_new(self):
|
||||
return not os.path.exists(self.path)
|
||||
|
||||
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]' %
|
||||
(type(self).__name__, e.message, self.path)
|
||||
)
|
||||
self.update(data)
|
||||
except IOError as e:
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
|
||||
def save(self):
|
||||
self['__version__'] = __version__
|
||||
with open(self.path, 'w') as f:
|
||||
json.dump(self, f, indent=4, sort_keys=True, ensure_ascii=True)
|
||||
f.write('\n')
|
||||
|
||||
def delete(self):
|
||||
try:
|
||||
os.unlink(self.path)
|
||||
except OSError as e:
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
|
||||
|
||||
class Config(BaseConfigDict):
|
||||
|
||||
name = 'config'
|
||||
|
||||
DEFAULTS = {
|
||||
'default_content_type': 'json',
|
||||
}
|
||||
|
||||
def __init__(self, seq=None, **kwargs):
|
||||
super(Config, self).__init__(seq or [], **kwargs)
|
||||
self.update(self.DEFAULTS)
|
||||
|
Reference in New Issue
Block a user