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
#!/usr/bin/env python

import os, argparse, re, fileinput

parser = argparse.ArgumentParser(description="Search my notes")

parser.add_argument('pattern', help="The search pattern (regexp)")
args = parser.parse_args()

def find_files(root, extension):
    """Return a list of files in root that ends with extension."""
    os.chdir(root)
    notes = []
    for root, dir, files in os.walk('.'):
        for file in files:
            if file.endswith(extension):
                notes.append(os.path.join(root, file))
    return notes


# this assumes that we're storing our notes files in ~/notes with the
# extension .org. Modify this according to your needs.
notes_dir = os.path.join(os.path.expanduser("~"), 'notes')
files = find_files(notes_dir, ".org")
pattern = re.compile(args.pattern)

for line in fileinput.input(files):
    if pattern.search(line):
        print "%s:%s:%s" % (fileinput.filename(), fileinput.filelineno(),
                            line.rstrip())