-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
74 lines (57 loc) · 1.44 KB
/
convert.py
File metadata and controls
74 lines (57 loc) · 1.44 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
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import re
import sys
import getopt
#test if file exists
def openFile(name, o_type):
try:
#open a file
f = open(name, o_type)
except IOError as e:
print "I/O error({0}): {1} {2}".format(e.errno, name, e.strerror)
sys.exit(2)
except:
print "Unexpected error"
sys.exit(2)
return f
#convert function
def convert(source, result):
for line in source:
start = re.search('http://.*$',line).start()
stop = len(line)
headline = line[:start]
headline = re.sub(' - $','',headline)
url = re.sub('\n','',line[start:stop]);
item = '<span><a href="%s">%s</a></span><br />\n' % (url, headline)
result.write("%s\n" % item)
#main function
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'convert.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'convert.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
if inputfile == '':
print 'convert.py -i <inputfile> -o <outputfile>'
sys.exit(2)
if outputfile == '':
print 'convert.py -i <inputfile> -o <outputfile>'
sys.exit(2)
source = openFile(inputfile, 'r')
result = openFile(outputfile, 'w')
convert(source,result)
source.close()
result.close()
sys.exit()
#main function
main(sys.argv[1:])