-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
executable file
·104 lines (93 loc) · 4 KB
/
parser.py
File metadata and controls
executable file
·104 lines (93 loc) · 4 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
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
import re
from colorama import Fore
class Parser(object):
def __init__(self, flags=None):
if flags is not None:
self._flags = flags
def try_open_file(self, f):
try:
return enumerate(open(f))
except IOError:
print('Error: File %s does not appear to exist.' % f)
def matcher(self, f=None):
"""
Method which holds sub-methods for each argument passed:
:param f: file name, default is None
"""
def machine():
"""
Prints a matched patterns in machine format
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test2.txt" -m
file_name:test2.txt no_line:2 start_pos:4 matched_text:12345
"""
enumerate_file = self.try_open_file(f)
if enumerate_file:
for i, line in enumerate_file:
for match in re.finditer(self._flags['pattern'], line):
print('file_name:%s no_line:%s start_pos:%s matched_text:%s' % (f, i + 1, match.start(), match.groups()[0]))
def color():
"""
for highlighting matched string in file contents.
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test2.txt" -c
sdfsf
sada<red 12345 red>
"""
enumerate_file = self.try_open_file(f)
if enumerate_file:
for i, line in enumerate_file:
find = self._flags['pattern']
replace = Fore.RED + r'\1' + Fore.RESET
print(re.sub(find, replace, line.rstrip()))
def underscore():
"""
puts '^' under each matched pattern in file contents
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test2.txt" -u
sdfsf
sada12345
^^^^^
"""
enumerate_file = self.try_open_file(f)
if enumerate_file:
for i, line in enumerate_file:
p = re.compile(self._flags['pattern'])
m = p.search(line.rstrip())
if m:
start, end = m.span()
print(line.rstrip())
print(' ' * start + '^' * (end - start))
else:
print(line.rstrip())
def human():
"""
displaying humanly readable format of matched pattern.
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})" -f "test2.txt" -hu
File name: test2.txt Found on line 2: 12345
"""
enumerate_file = self.try_open_file(f)
if enumerate_file:
for i, line in enumerate_file:
for match in re.finditer(self._flags['pattern'], line):
print('File name: %s Found on line %s: %s' % (f, i + 1, match.groups()[0]))
def str():
"""
triggered when file names are not passed as arguments. in this case, user is asked to input a string, and the
pattern is getting matched inside this string. List of matches is printed.
>>> python2.7 parse_input_by_regex.py -p "(\d{4,5})"
You did not enter file paths. Please give string to be searched in:
>>> sada12345
['12345']
"""
matches = re.findall(self._flags['pattern'], self._flags['str'], re.DOTALL)
print(matches)
# An argument-->method mapping for deciding which method should shoot for particular argument
argument_to_method_mapping = {
'machine': machine,
'color': color,
'underscore': underscore,
'human': human,
'str': str
}
# Iterating over arguments dict and triggering appropriate method
for key, val in self._flags.iteritems():
if (key in argument_to_method_mapping.keys()) and val:
argument_to_method_mapping[key]()