-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgrep
executable file
·173 lines (142 loc) · 5.22 KB
/
bgrep
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
# -*- coding: utf-8 -*-
############################################################################
# Copyright (C) 2010 by Nestor Aguirre #
# nfaguirre@imaff.cfmac.csic.es #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
import os
import sys
PIAMOD_HOME = os.getenv("PIAMOD_HOME")
if( PIAMOD_HOME == None ):
print "### Error ###: Environment variable PIAMOD_HOME not found !!!"
quit()
sys.path.append(PIAMOD_HOME+"/src")
from ParserTextBlock import *
class BGrep:
@staticmethod
def systemOptions():
output=[]
previous=""
current=""
for i in range(1,len(sys.argv)):
option=""
value=""
if( re.match( "-{1,2}[\D]+[\d]*", sys.argv[i] ) ):
option = sys.argv[i]
if( i+1 < len(sys.argv) ):
if( re.match( "([-]{0,1}[\d]+.[\d]+$|^[\^\w\d\"\(.*\[\\\]+.*$)", sys.argv[i+1] ) ):
value = sys.argv[i+1]
else:
value = None
else:
value = None
if( option != "" ):
output.append( (option,value) )
return output
@staticmethod
def usage():
print "Usage:"
print " $ bgrep [OPTIONS] FILE"
print ""
print "OPTIONS"
print ""
print " -b regExp"
print " Regular expression for matching in the begin of the block"
print " (default=\".*\", equivalento to begin of file)"
print " -e regExp"
print " Regular expression for matching in the end of the block"
print " (default=\"EOF$$\", equivalent to end of file)"
print " -f regExp"
print " Regular expression for filter in the end of the block"
print " (default=\".*\", equivalent to no filter)"
print " -nh"
print " Activates the flag to include not the header"
print " -nf"
print " Activates the flag to include not the footer"
print " -p Integer"
print " Extracts a specific block number p>0"
quit()
@staticmethod
def main():
includeHeader = True
includeFooter = True
reBegin = ".*"
reEnd = "EOF$$"
lineFilter = ".*"
position = -1
remove = False
# Se procesan las opciones
for option in BGrep.systemOptions():
#print option
if( option[0] == "-b" ):
reBegin = option[1]
elif( option[0] == "-e" ):
reEnd = option[1]
elif( option[0] == "-f" ):
lineFilter = option[1]
if( option[0] == "-nh" ):
includeHeader = False
elif( option[0] == "-nf" ):
includeFooter = False
elif( option[0] == "-p" ):
position = option[1]
if( int(position) < 1 ):
print "## ERROR ## The number of block should be greater than zero (-p option)"
quit()
elif( option[0] == "-r" ):
remove = True
# Verifica las opciones obligatorias
if( sys.argv[-1][-5:] == "bgrep" ):
BGrep.usage()
return 0
# Se selecciona la ultima opción para el archivo de entrada
ifile = file( sys.argv[-1] )
ifile.close()
if( ifile != None ):
parser = ParserTextBlock( sys.argv[-1] )
#print "#", reBegin, reEnd, lineFilter, position
parser.extractBlock( reBegin, reEnd, lineFilter )
block = parser.extractBlock( reBegin, reEnd, lineFilter, int(position) )
output = ""
if( includeHeader and len(block.header) > 0 ):
output += block.header
if( includeHeader and len(block.header) > 0 and len(block.content) > 0 ):
output += "\n"
if( len(block.content) > 1 ):
output += block.content
if( includeFooter and len(block.content) > 0 and len(block.footer) > 0 ):
output += "\n"
if( includeFooter and len(block.footer) > 0 ):
output += block.footer
print output
#else:
## Si el archivo no existe, se intenta obtener
## la información desde entrada estandar
#ofile = file( ".cin~", "w" )
#ofile.write( sys.stdin.read(0) )
#ofile.close()
#parser = ParserTextBlock( ".cin~" )
#block = parser.extractBlock( reBegin, reEnd, filter )
#if( block.content != "" ):
#output = ""
#output += block.header+"\n"
#output += block.content+"\n"
#output += block.footer
#print output
if __name__ == "__main__":
BGrep.main()