forked from mk-fg/fgtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlparse
executable file
·59 lines (47 loc) · 1.81 KB
/
urlparse
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
import itertools as it, operator as op, functools as ft
from collections import OrderedDict
import os, sys, urlparse, pyaml
def dissect_url(url):
url = dict(it.izip(
[ 'scheme', 'netloc', 'path', 'params', 'query',
'fragment', 'username', 'password', 'hostname', 'port' ],
urlparse.urlparse(url) ))
if url.get('query'):
url['query'] = sorted(urlparse.parse_qsl(url['query']))
url = OrderedDict(sorted(url.viewitems()))
# Make "query" always a last element
url['query'] = url.pop('query')
return url
def main():
import argparse
parser = argparse.ArgumentParser(
usage='%(prog)s [options] URL1 [URL2] [ -- <diff options> ]',
description='Process and dump URLs in a human-readable format.')
parser.add_argument('url', help='URL to process.')
parser.add_argument('url2', nargs='?', help='URL to show diff against.')
parser.add_argument('--debug', action='store_true', help='Verbose output.')
argv, diff_optz = sys.argv[1:], list()
try: idx = argv.index('--')
except ValueError: pass
else: argv, diff_optz = argv[:idx], argv[idx+1:]
optz = parser.parse_args(argv)
import logging
logging.basicConfig(level='DEBUG' if optz.debug else 'WARNING')
log = logging.getLogger()
url = dissect_url(optz.url)
if not optz.url2: pyaml.dump(url, sys.stdout)
else:
from tempfile import NamedTemporaryFile
from subprocess import Popen
with NamedTemporaryFile() as tmp1, NamedTemporaryFile() as tmp2:
pyaml.dump(url, tmp1)
url2 = dissect_url(optz.url2)
pyaml.dump(url2, tmp2)
tmp1.flush(), tmp2.flush()
diff_cmd = ['colordiff', '-uw'] + diff_optz + [tmp1.name, tmp2.name]
log.debug('Diff command: {}'.format(' '.join(diff_cmd)))
sys.exit(Popen(diff_cmd).wait())
if __name__ == '__main__': main()