forked from akhilpampana/LDpredChrByChr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwecho
executable file
·111 lines (92 loc) · 2.88 KB
/
wecho
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
#!/usr/bin/env python3
"""
Copy input multiline string to stdout, with the ability to filter comments.
@Author: [email protected]
Usage:
wecho.py <string> [-d commendDelimeter] [-l linecomment] [-b linebreaker]
wecho.py -h | --help | -v | --version | -f | --format
Notes:
1. Copy input string argument to stdout.
2. Remove newline character, combine mutilines to a single line.
3. Remove comments from input string, comments was surrounded by %, see example by -f.
Single line comment can be started by linecomment characters, default #.
Options:
-d string Replace default pair comment delimter(%%) as 'string'.
-l string String for declearing a linecomment, default '#' or '//'.
-b string String for linebreaker, start a new line after these characters, default !.
-h --help Show this screen.
-v --version Show version.
-f --format Show input/output file format example.
"""
import sys
from docopt import docopt
import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
def ShowFormat():
'''File format example'''
print('''
#Input example
------------------------
"
123 %% comments %%
456 %% multiple %%
line
# comments
# line comments
789
!new line
123 %% comments%% 456 789"
#Output example
------------------------
123 456 789
''');
if __name__ == '__main__':
args = docopt(__doc__, version='3.0')
#version 2.0
#1. add function for line comments.
commentDelimeter = '%%'
# linecomment = '//'
linecomment = '#'
linebreaker = '!'
if args['-d']:
commentDelimeter = args['-d']
if args['-l']:
linecomment = args['-l']
if args['-b']:
linebreaker = args['-b']
#print(args)
if(args['--format']):
ShowFormat()
sys.exit(-1)
#split by lines.
import re
lines = args['<string>'].strip()
lines = lines.split(commentDelimeter)
if len(lines) %2 == 0:
sys.stderr.write('Error: Comment delimter should be appeared in pair!.\n')
sys.exit(-1)
#skip comments
arr2 = []
for i in range(0,len(lines),2):
arr2.append(lines[i])
#remove line breakers.
arr1 = []
for x in arr2:
if x:
for y in x.splitlines():
y = y.strip()
# y.startswith('//') make it compatible with old scripts.
if not (y.startswith(linecomment) or y.startswith('//')): #skip line commented line.
arr1.append(y)
out = ' '.join(arr1)
out = re.sub(r'\s+',' ',out)
out = out.split(linebreaker)
for x in out:
x = x.strip()
sys.stdout.write('%s\n'%(x))
# if len(lines) == 1:
# lines = re.split('\\\\n',lines[0])
sys.stdout.flush()
sys.stdout.close()
sys.stderr.flush()
sys.stderr.close()