This repository was archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurlrc.py
executable file
·242 lines (203 loc) · 6.73 KB
/
curlrc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Treat curl configuration files as commands.
"""
from __future__ import print_function
import argparse
import collections
import glob
import json
import os
import os.path
import re
import sys
__version__ = '0.2.1'
CURL_HOME = os.getenv('CURL_HOME', os.path.expanduser('~/.curl'))
CURLRC_EXTENSION = '.rc'
# Split option lines on this pattern.
CURLRC_OPTION_EXPR = re.compile(r'\s?[\s=:]\s?')
# Match %{variable} strings in format.
CURLRC_FORMAT_STRING_EXPR = re.compile(r'%{[\w]+}')
OUTPUT_FORMATS = ('csv', 'json', 'table')
class CurlConfig(object):
"""
A curl configuration.
"""
def __init__(self, name, path=None, description=None, **options):
self.name = name
self.path = path
self.description = description
self.options = options
@classmethod
def from_file(cls, path):
"""
Load a curl configuration from a file.
Args:
path: path to curl configuration file
Returns: CurlConfig
"""
name = os.path.basename(path).replace(CURLRC_EXTENSION, '')
description = None
options = {}
with open(path) as config:
# Extract description from first line if it is a comment.
first_line = config.readline().strip()
if first_line.startswith('#'):
description = first_line.split('#')[1].strip()
# Collect options as a dictionary.
options = dict(cls.split_line(line.strip())
for line in config if not line.startswith('#'))
return cls(name, path, description, **options)
@property
def template(self):
"""
Retrieve the format string specified in the configuration file.
Returns: str or None
"""
return self.options.get('-w', self.options.get('--write-out'))
@staticmethod
def split_line(line):
"""
Split curl option lines into key-value pairs.
Args:
line: curl configuration option line
Returns: list of [option, value]
"""
option = re.split(CURLRC_OPTION_EXPR, line, maxsplit=1)
# Handle boolean flags (e.g., -s).
if len(option) < 2:
option.append(True)
return option
class CurlTemplate(object):
"""
A curl output template.
"""
def __init__(self, replacements):
self._map = replacements
@classmethod
def from_str(cls, template):
"""
Load the template from a template string.
"""
chars_to_remove = '%{}'
_map = collections.OrderedDict()
for tmpl in re.findall(CURLRC_FORMAT_STRING_EXPR, template):
_map[''.join(c for c in tmpl if c not in chars_to_remove)] = tmpl
return cls(_map)
def as_csv(self, pretty=True):
"""
Output the template as comma-separated values.
"""
output = ''
if pretty:
output = ','.join(self._map.keys())
output += '\n'
output += ','.join(self._map.values())
return output + '\n'
def as_json(self, pretty=True):
"""
Output the template as a JSON hash.
"""
indent = 2 if pretty else None
return json.dumps(self._map, indent=indent) + '\n'
def as_table(self, pretty=True):
"""
Output the template as a tab-separated table.
"""
lines = []
for field, value in self._map.items():
if pretty:
lines.append('{}\t{}'.format(field, value))
else:
lines.append(value)
output = '\n'.join(lines)
return output + '\n'
def curl_configs(path=None, pattern=None):
"""
Locate curl configurations in a directory.
Args:
path: directory containing configuration files (default: CURL_HOME)
pattern: glob pattern to match (default: *.rc)
Returns: list of files
"""
path = path if path else CURL_HOME
pattern = pattern if pattern else '*{}'.format(CURLRC_EXTENSION)
return glob.glob(os.path.join(path, pattern))
def parse_args(argv):
"""
Parse command-line arguments.
Returns: argparse.Namespace
"""
usage_template = '{} {} [OPTION...] -- [CURL ARGS...]'.format
parser = argparse.ArgumentParser(
usage=usage_template('%(prog)s', 'COMMAND'),
version='%(prog)s {}'.format(__version__),
)
# Define common arguments for subcommands.
common = argparse.ArgumentParser(add_help=False)
common.add_argument(
'-f', '--format',
choices=OUTPUT_FORMATS,
help='output format',
)
# Control pretty-printing.
pretty = common.add_mutually_exclusive_group()
pretty.add_argument(
'--pretty',
action='store_true', default=True,
help='pretty-print output [%(default)s]',
)
pretty.add_argument(
'--no-pretty',
action='store_false', default=False, dest='pretty',
help='do not pretty-print output [%(default)s]',
)
# Pass the rest of the arguments to curl.
common.add_argument(
'curl_args',
nargs='*', metavar='CURL ARGS',
help='arguments passed to curl',
)
# Extract command names from .curlrc files.
subparsers = parser.add_subparsers(
title='commands', dest='command',
# hide {command1,command2} output
metavar='',
)
# Python 3.3 introduced a regression that makes subparsers optional:
# <http://bugs.python.org/issue9253>
# This is a no-op in Python 2.
subparsers.required = True
commands = [CurlConfig.from_file(c) for c in curl_configs()]
for command in commands:
subparsers.add_parser(
command.name,
help=command.description, parents=[common],
# %(prog)s evaluates to the top-level parser's usage string.
usage=usage_template(os.path.basename(sys.argv[0]), command.name)
)
return parser.parse_args(argv)
def main(argv=None):
argv = argv if argv else sys.argv[1:]
args = parse_args(argv)
config = CurlConfig.from_file(
os.path.join(CURL_HOME, '{}{}'.format(args.command, CURLRC_EXTENSION))
)
# If the user specified a format, override the format specified
# in the configuration file.
if args.format and config.template:
tmpl = CurlTemplate.from_str(config.template)
formats = {
'csv': tmpl.as_csv,
'json': tmpl.as_json,
'table': tmpl.as_table,
}
override_format = formats[args.format](args.pretty)
curl_args = ['-w', override_format]
curl_args.extend(args.curl_args)
else:
curl_args = args.curl_args
os.execlp('curl', 'curl', '-K', config.path, *curl_args)
if __name__ == '__main__':
main()