-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygi.py
67 lines (54 loc) · 1.97 KB
/
pygi.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
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import os
import sys
import json
import Levenshtein
try:
from urllib.request import urlopen
except:
from urllib2 import urlopen
CONFIG_PATH = '~/.config/pygitignore'.replace('~', os.environ['HOME'])
API_URL = 'https://www.gitignore.io/api'
def _get_text_from_url(url):
return urlopen(url).read().decode('UTF-8')
def list():
text = _get_text_from_url('{}/list?format=lines'.format(API_URL))
return text.split('\n')[:-1]
def gitignores(*args):
to_send = []
gitignore_list = list()
for arg in set(args):
if arg in gitignore_list:
to_send.append(arg)
elif __name__ == '__main__':
possibles = []
for gitignore in gitignore_list:
if Levenshtein.distance(gitignore, arg) == 1:
possibles.append(gitignore)
print('WARNING: {} is not in gitignore list.'.format(arg), file=sys.stderr, end='')
if possibles:
if len(possibles) == 1:
possible_string = possibles[0]
else:
possible_string = ', '.join(possibles[:-1]) + ' or ' + possibles[-1]
print(' Did you mean {}?'.format(possible_string), file=sys.stderr)
else:
print('', file=sys.stderr)
if not to_send:
return '\n'
text = _get_text_from_url('{}/{}'.format(API_URL, ','.join(to_send)))
return '\n'.join(text.split('\n')[2:])
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--list', help='List available gitignores', action='store_true')
group.add_argument('gitignores', help='gitignores to be included', nargs='*', default=[])
args = parser.parse_args()
if args.list:
print('\n'.join(list()))
else:
print(gitignores(*args.gitignores))
if __name__ == '__main__':
main()