-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmanage.py
More file actions
62 lines (55 loc) · 2.06 KB
/
Copy pathmanage.py
File metadata and controls
62 lines (55 loc) · 2.06 KB
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
#!/usr/bin/env python
from os.path import realpath, dirname
from operator import itemgetter
from sys import exit
from urlparse import urlparse
DIR_NAME = realpath(dirname(realpath(__file__)))
def argparser():
import argparse
argp = argparse.ArgumentParser(description='ListOfGithubLists update script')
argp.add_argument('-f', '--file'
,help = 'Output file - default is README.md'
,metavar = 'FILE'
,default = DIR_NAME + '/README.md'
)
subparsers = argp.add_subparsers(help='sub-command help', dest='action')
parser_add = subparsers.add_parser('add', help='add new link')
parser_add.add_argument('title'
,metavar = 'TITLE'
,help = 'TITLE string'
)
parser_add.add_argument('url'
,metavar = 'URL'
,help = 'URL string'
)
return argp.parse_args()
def __main__():
args = argparser()
#print(args.file)
lines = open(args.file, 'r').readlines()
links = {}
header = ''
for line in lines:
if line.startswith(' * ['):
title, url = line.strip()[3:-1].split('](')
links[url] = title
else:
header += line
if args.action == 'add':
if not args.url in links:
links[args.url] = args.title
else:
print('[!] item already exists')
exit(1)
with open(args.file, 'w') as outfile:
outfile.write(header)
outfile.write('\n'.join(' * [{0}]({1})'.format(title, url)
for url, title
in sorted(links.items(), key=itemgetter(1))))
parsed_url = urlparse(args.url)
_, user, repo = parsed_url.path.split('/', 2)
print('{0} added'.format(args.title))
print('Run `git commit README.md -m "[enh] {0} by @{1}"`'.format(repo, user))
exit(0)
if __name__ == '__main__':
__main__()