-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
executable file
·63 lines (53 loc) · 1.57 KB
/
Copy pathsearch.py
File metadata and controls
executable file
·63 lines (53 loc) · 1.57 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
#! /usr/bin/env python3
import xml.etree.ElementTree as ET
import sys
if len(sys.argv) == 1:
print("Usage: ./search.py <XML_FILE> [option(s)]")
print()
print("Options:")
print(" --keyword Search messages by keyword")
print(" --exact Use exact matching on name/keyword")
print()
print("Note: Default mode uses substring matching to match names/keywords")
sys.exit(1)
# Parse options
exact_match = "--exact" in sys.argv
keyword = None
if "--keyword" in sys.argv:
keyword = input("Enter keyword to search: ")
else:
name = input("Enter name to search: ")
# Prepare xml file for parsing
f = sys.argv[1]
tree = ET.parse(f)
root = tree.getroot()
# Pretty-printing
HEADER = '\033[95m'
GREEN = '\033[92m'
BLUE = '\033[94m'
END = '\x1b[0m'
def print_message(child):
message = HEADER + "{}\n " + END + "{}: {}{}"
contact = child.get('contact_name')
date = child.get('readable_date')
msg = child.get('body')
if child.get('type') == '1':
print(message.format(date, contact, GREEN, msg))
else:
print(message.format(date, "Me", BLUE, msg))
# Print all matching messages
for child in root:
contact = child.get('contact_name')
message = child.get('body')
if keyword:
if exact_match:
if keyword in message.split():
print_message(child)
elif keyword in message:
print_message(child)
else:
if exact_match:
if name in contact.split():
print_message(child)
elif name in contact:
print_message(child)