code to search for tags in the header of a python script
# ==================================================================================
# code search_tag.py
# Description: search for tags in the header section of my python scripts
# usage:
# 1 - cd in the directory you want to search from (it will recursively look
# for *.py files)
# 2- search_tag tag1 tag2 tag3 ...
#
# this useful in conjunction to this vim snippet called "intro" (to add to .vim/snippet/python.snippets)
# snippet intro
# #!/home/me/epd/bin/python
# # -*- coding: utf-8 -*-
# # ==================================================================================
# # code ${1:`Filename('$1.py', 'foo.py')`}
# # Description: ${2: purpose}
# # TAGS:${3: tags}:${4: tags}:${5: tags}
# # created on `strftime("%Y-%m-%d")`
# # Nicolas Fauchereau <Nicolas.Fauchereau@gmail.com>
# # ==================================================================================
#
#
# created on 2011-12-02
# Nicolas Fauchereau <Nicolas.Fauchereau@gmail.com>
# ==================================================================================
import os
import sys
import fnmatch
tag_list = []
for tag in sys.argv[1:]:
tag_list.append(str(tag))
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
count = 0
for file in find_files('.', '*.py'):
try:
f = open(file)
header = f.readlines()[0:11]
f.close()
for linehead in header:
if linehead[0:7] == '# TAGS:':
linesplit = [i.strip('\n') for i in linehead.split(':')]
if (len(tag_list) == len(list(set(linesplit) & set(tag_list)))):
print "# ======================================"
print file, " is tagged: ",linehead[7:-1]
count += 1
except:
pass
if count == 0:
print "\n# ======================================"
print "! No file found with tags: ", ", ".join(repr(i) for i in tag_list)
print "# ======================================\n"